refactor(db): Handle the addition from SQL directly instead from code

This commit is contained in:
Ahmad Ansori Palembani 2024-11-29 08:06:36 +07:00
parent 39428f8c79
commit 97eacbbaea
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6
6 changed files with 5 additions and 9 deletions

View file

@ -18,6 +18,7 @@ import eu.kanade.tachiyomi.util.system.launchNow
import kotlin.math.max
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import yokai.data.DatabaseHandler
import yokai.domain.category.interactor.GetCategories
import yokai.domain.chapter.interactor.GetChapter
import yokai.domain.chapter.interactor.InsertChapter
@ -33,6 +34,7 @@ import yokai.domain.track.interactor.GetTrack
class MangaBackupRestorer(
private val db: DatabaseHelper = Injekt.get(),
private val customMangaManager: CustomMangaManager = Injekt.get(),
private val handler: DatabaseHandler = Injekt.get(),
private val getCategories: GetCategories = Injekt.get(),
private val getChapter: GetChapter = Injekt.get(),
private val insertChapter: InsertChapter = Injekt.get(),
@ -208,7 +210,7 @@ class MangaBackupRestorer(
// List containing history to be updated
val historyToBeUpdated = ArrayList<History>(history.size)
for ((url, lastRead, readDuration) in history) {
val dbHistory = getHistory.awaitByChapterUrl(url)
val dbHistory = handler.awaitOneOrNull { historyQueries.getByChapterUrl(url, History::mapper) }
// Check if history already in database and update
if (dbHistory != null) {
dbHistory.apply {

View file

@ -659,10 +659,9 @@ class ReaderViewModel(
if (!preferences.incognitoMode().get()) {
val readAt = Date().time
val sessionReadDuration = chapterReadStartTime?.let { readAt - it } ?: 0
val oldTimeRead = getHistory.awaitByChapterUrl(readerChapter.chapter.url)?.time_read ?: 0
val history = History.create(readerChapter.chapter).apply {
last_read = readAt
time_read = sessionReadDuration + oldTimeRead
time_read = sessionReadDuration
}
upsertHistory.await(history)
chapterReadStartTime = null

View file

@ -24,9 +24,6 @@ class HistoryRepositoryImpl(private val handler: DatabaseHandler) : HistoryRepos
}
}
override suspend fun getByChapterUrl(chapterUrl: String): History? =
handler.awaitOneOrNull { historyQueries.getByChapterUrl(chapterUrl, History::mapper) }
override suspend fun getByMangaId(mangaId: Long): History? =
handler.awaitOneOrNull { historyQueries.getByMangaId(mangaId, History::mapper) }

View file

@ -6,7 +6,6 @@ import eu.kanade.tachiyomi.data.database.models.MangaChapterHistory
interface HistoryRepository {
suspend fun upsert(chapterId: Long, lastRead: Long, timeRead: Long): Long?
suspend fun bulkUpsert(histories: List<History>)
suspend fun getByChapterUrl(chapterUrl: String): History?
suspend fun getByMangaId(mangaId: Long): History?
suspend fun getAllByMangaId(mangaId: Long): List<History>

View file

@ -5,7 +5,6 @@ import yokai.domain.history.HistoryRepository
class GetHistory(
private val historyRepository: HistoryRepository
) {
suspend fun awaitByChapterUrl(chapterUrl: String) = historyRepository.getByChapterUrl(chapterUrl)
suspend fun awaitByMangaId(mangaId: Long) = historyRepository.getByMangaId(mangaId)
suspend fun awaitAllByMangaId(mangaId: Long) = historyRepository.getAllByMangaId(mangaId)
}

View file

@ -16,7 +16,7 @@ ON CONFLICT(history_chapter_id)
DO UPDATE
SET
history_last_read = :historyLastRead,
history_time_read = :historyTimeRead
history_time_read = ifnull(history_time_read, 0) + :historyTimeRead
WHERE history_chapter_id = history_chapter_id;
selectLastInsertedRowId: