From cd0294b1b697b5dedb79374a1007ad81d90781c9 Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 29 May 2022 12:12:06 -0400 Subject: [PATCH] Replace history query with actual upsert --- app/build.gradle.kts | 7 ++++++ .../data/history/HistoryRepositoryImpl.kt | 22 +++++-------------- .../data/database/queries/HistoryQueries.kt | 10 --------- app/src/main/sqldelight/data/history.sq | 16 +++++++------- 4 files changed, 21 insertions(+), 34 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index aec2a984a..921e6183b 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -136,6 +136,13 @@ android { kotlinOptions { jvmTarget = JavaVersion.VERSION_1_8.toString() } + + sqldelight { + database("Database") { + packageName = "eu.kanade.tachiyomi" + dialect = "sqlite:3.24" + } + } } dependencies { diff --git a/app/src/main/java/eu/kanade/data/history/HistoryRepositoryImpl.kt b/app/src/main/java/eu/kanade/data/history/HistoryRepositoryImpl.kt index 50223330b..190896305 100644 --- a/app/src/main/java/eu/kanade/data/history/HistoryRepositoryImpl.kt +++ b/app/src/main/java/eu/kanade/data/history/HistoryRepositoryImpl.kt @@ -93,22 +93,12 @@ class HistoryRepositoryImpl( override suspend fun upsertHistory(historyUpdate: HistoryUpdate) { try { - try { - handler.await { - historyQueries.insert( - historyUpdate.chapterId, - historyUpdate.readAt, - historyUpdate.sessionReadDuration, - ) - } - } catch (e: Exception) { - handler.await { - historyQueries.update( - historyUpdate.readAt, - historyUpdate.sessionReadDuration, - historyUpdate.chapterId, - ) - } + handler.await { + historyQueries.upsert( + historyUpdate.chapterId, + historyUpdate.readAt, + historyUpdate.sessionReadDuration, + ) } } catch (e: Exception) { logcat(LogPriority.ERROR, throwable = e) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/HistoryQueries.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/HistoryQueries.kt index 86ee8d604..9e54cc1d1 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/HistoryQueries.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/queries/HistoryQueries.kt @@ -30,16 +30,6 @@ interface HistoryQueries : DbProvider { ) .prepare() - /** - * Updates the history last read. - * Inserts history object if not yet in database - * @param history history object - */ - fun upsertHistoryLastRead(history: History) = db.put() - .`object`(history) - .withPutResolver(HistoryUpsertResolver()) - .prepare() - /** * Updates the history last read. * Inserts history object if not yet in database diff --git a/app/src/main/sqldelight/data/history.sq b/app/src/main/sqldelight/data/history.sq index a6d6b912f..5b505bba6 100644 --- a/app/src/main/sqldelight/data/history.sq +++ b/app/src/main/sqldelight/data/history.sq @@ -36,12 +36,12 @@ removeResettedHistory: DELETE FROM history WHERE last_read = 0; -insert: +upsert: INSERT INTO history(chapter_id, last_read, time_read) -VALUES (:chapterId, :readAt, :readDuration); - -update: -UPDATE history -SET last_read = :readAt, - time_read = time_read + :sessionReadDuration -WHERE chapter_id = :chapterId; +VALUES (:chapterId, :readAt, :time_read) +ON CONFLICT(chapter_id) +DO UPDATE +SET + last_read = :readAt, + time_read = time_read + :time_read +WHERE chapter_id = :chapterId; \ No newline at end of file