mirror of
https://github.com/null2264/yokai.git
synced 2025-06-21 10:44:42 +00:00
refactor(db): Replace some more StorIO queries with SQLDelight
- Manga: db.getManga -> getManga.await* - History - Chapter: db.updateChapter* -> updateChapter.await; db.getChapter -> getChapter.await*
This commit is contained in:
parent
822cfa56a6
commit
5378d2a99b
8 changed files with 81 additions and 33 deletions
|
@ -3,7 +3,6 @@ package eu.kanade.tachiyomi.data.database.queries
|
||||||
import com.pushtorefresh.storio.sqlite.queries.Query
|
import com.pushtorefresh.storio.sqlite.queries.Query
|
||||||
import eu.kanade.tachiyomi.data.database.DbProvider
|
import eu.kanade.tachiyomi.data.database.DbProvider
|
||||||
import eu.kanade.tachiyomi.data.database.models.Chapter
|
import eu.kanade.tachiyomi.data.database.models.Chapter
|
||||||
import eu.kanade.tachiyomi.data.database.resolvers.ChapterProgressPutResolver
|
|
||||||
import eu.kanade.tachiyomi.data.database.tables.ChapterTable
|
import eu.kanade.tachiyomi.data.database.tables.ChapterTable
|
||||||
import eu.kanade.tachiyomi.domain.manga.models.Manga
|
import eu.kanade.tachiyomi.domain.manga.models.Manga
|
||||||
|
|
||||||
|
@ -22,24 +21,6 @@ interface ChapterQueries : DbProvider {
|
||||||
)
|
)
|
||||||
.prepare()
|
.prepare()
|
||||||
|
|
||||||
// FIXME: Migrate to SQLDelight, on halt: in StorIO transaction
|
|
||||||
fun getChapter(id: Long) = db.get()
|
|
||||||
.`object`(Chapter::class.java)
|
|
||||||
.withQuery(
|
|
||||||
Query.builder()
|
|
||||||
.table(ChapterTable.TABLE)
|
|
||||||
.where("${ChapterTable.COL_ID} = ?")
|
|
||||||
.whereArgs(id)
|
|
||||||
.build(),
|
|
||||||
)
|
|
||||||
.prepare()
|
|
||||||
|
|
||||||
// FIXME: Migrate to SQLDelight, on halt: in StorIO transaction
|
// FIXME: Migrate to SQLDelight, on halt: in StorIO transaction
|
||||||
fun insertChapters(chapters: List<Chapter>) = db.put().objects(chapters).prepare()
|
fun insertChapters(chapters: List<Chapter>) = db.put().objects(chapters).prepare()
|
||||||
|
|
||||||
// FIXME: Migrate to SQLDelight, on halt: in StorIO transaction
|
|
||||||
fun updateChapterProgress(chapter: Chapter) = db.put()
|
|
||||||
.`object`(chapter)
|
|
||||||
.withPutResolver(ChapterProgressPutResolver())
|
|
||||||
.prepare()
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,7 +48,6 @@ import eu.kanade.tachiyomi.util.chapter.updateTrackChapterRead
|
||||||
import eu.kanade.tachiyomi.util.isLocal
|
import eu.kanade.tachiyomi.util.isLocal
|
||||||
import eu.kanade.tachiyomi.util.storage.DiskUtil
|
import eu.kanade.tachiyomi.util.storage.DiskUtil
|
||||||
import eu.kanade.tachiyomi.util.system.ImageUtil
|
import eu.kanade.tachiyomi.util.system.ImageUtil
|
||||||
import eu.kanade.tachiyomi.util.system.e
|
|
||||||
import eu.kanade.tachiyomi.util.system.launchIO
|
import eu.kanade.tachiyomi.util.system.launchIO
|
||||||
import eu.kanade.tachiyomi.util.system.launchNonCancellableIO
|
import eu.kanade.tachiyomi.util.system.launchNonCancellableIO
|
||||||
import eu.kanade.tachiyomi.util.system.localeContext
|
import eu.kanade.tachiyomi.util.system.localeContext
|
||||||
|
@ -82,6 +81,8 @@ import yokai.domain.chapter.interactor.InsertChapter
|
||||||
import yokai.domain.chapter.interactor.UpdateChapter
|
import yokai.domain.chapter.interactor.UpdateChapter
|
||||||
import yokai.domain.chapter.models.ChapterUpdate
|
import yokai.domain.chapter.models.ChapterUpdate
|
||||||
import yokai.domain.download.DownloadPreferences
|
import yokai.domain.download.DownloadPreferences
|
||||||
|
import yokai.domain.history.interactor.GetHistory
|
||||||
|
import yokai.domain.history.interactor.UpsertHistory
|
||||||
import yokai.domain.manga.interactor.GetManga
|
import yokai.domain.manga.interactor.GetManga
|
||||||
import yokai.domain.manga.interactor.InsertManga
|
import yokai.domain.manga.interactor.InsertManga
|
||||||
import yokai.domain.manga.interactor.UpdateManga
|
import yokai.domain.manga.interactor.UpdateManga
|
||||||
|
@ -110,6 +111,8 @@ class ReaderViewModel(
|
||||||
private val getManga: GetManga by injectLazy()
|
private val getManga: GetManga by injectLazy()
|
||||||
private val insertManga: InsertManga by injectLazy()
|
private val insertManga: InsertManga by injectLazy()
|
||||||
private val updateManga: UpdateManga by injectLazy()
|
private val updateManga: UpdateManga by injectLazy()
|
||||||
|
private val getHistory: GetHistory by injectLazy()
|
||||||
|
private val upsertHistory: UpsertHistory by injectLazy()
|
||||||
|
|
||||||
private val mutableState = MutableStateFlow(State())
|
private val mutableState = MutableStateFlow(State())
|
||||||
val state = mutableState.asStateFlow()
|
val state = mutableState.asStateFlow()
|
||||||
|
@ -241,7 +244,7 @@ class ReaderViewModel(
|
||||||
if (!needsInit()) return Result.success(true)
|
if (!needsInit()) return Result.success(true)
|
||||||
return withIOContext {
|
return withIOContext {
|
||||||
try {
|
try {
|
||||||
val manga = db.getManga(mangaId).executeAsBlocking()
|
val manga = getManga.awaitById(mangaId)
|
||||||
if (manga != null) {
|
if (manga != null) {
|
||||||
mutableState.update { it.copy(manga = manga) }
|
mutableState.update { it.copy(manga = manga) }
|
||||||
if (chapterId == -1L) {
|
if (chapterId == -1L) {
|
||||||
|
@ -621,12 +624,10 @@ class ReaderViewModel(
|
||||||
*/
|
*/
|
||||||
private fun saveReadingProgress(readerChapter: ReaderChapter) {
|
private fun saveReadingProgress(readerChapter: ReaderChapter) {
|
||||||
viewModelScope.launchNonCancellableIO {
|
viewModelScope.launchNonCancellableIO {
|
||||||
db.inTransaction {
|
|
||||||
saveChapterProgress(readerChapter)
|
saveChapterProgress(readerChapter)
|
||||||
saveChapterHistory(readerChapter)
|
saveChapterHistory(readerChapter)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fun saveCurrentChapterReadingProgress() = getCurrentChapter()?.let { saveReadingProgress(it) }
|
fun saveCurrentChapterReadingProgress() = getCurrentChapter()?.let { saveReadingProgress(it) }
|
||||||
|
|
||||||
|
@ -634,31 +635,36 @@ class ReaderViewModel(
|
||||||
* Saves this [readerChapter]'s progress (last read page and whether it's read).
|
* Saves this [readerChapter]'s progress (last read page and whether it's read).
|
||||||
* If incognito mode isn't on or has at least 1 tracker
|
* If incognito mode isn't on or has at least 1 tracker
|
||||||
*/
|
*/
|
||||||
// FIXME: Migrate to SQLDelight, on halt: in StorIO transaction
|
|
||||||
private suspend fun saveChapterProgress(readerChapter: ReaderChapter) {
|
private suspend fun saveChapterProgress(readerChapter: ReaderChapter) {
|
||||||
readerChapter.requestedPage = readerChapter.chapter.last_page_read
|
readerChapter.requestedPage = readerChapter.chapter.last_page_read
|
||||||
db.getChapter(readerChapter.chapter.id!!).executeAsBlocking()?.let { dbChapter ->
|
getChapter.awaitById(readerChapter.chapter.id!!)?.let { dbChapter ->
|
||||||
readerChapter.chapter.bookmark = dbChapter.bookmark
|
readerChapter.chapter.bookmark = dbChapter.bookmark
|
||||||
}
|
}
|
||||||
if (!preferences.incognitoMode().get() || hasTrackers) {
|
if (!preferences.incognitoMode().get() || hasTrackers) {
|
||||||
db.updateChapterProgress(readerChapter.chapter).executeAsBlocking()
|
updateChapter.await(
|
||||||
|
ChapterUpdate(
|
||||||
|
id = readerChapter.chapter.id!!,
|
||||||
|
bookmark = readerChapter.chapter.bookmark,
|
||||||
|
lastPageRead = readerChapter.chapter.last_page_read.toLong(),
|
||||||
|
pagesLeft = readerChapter.chapter.pages_left.toLong(),
|
||||||
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves this [readerChapter] last read history.
|
* Saves this [readerChapter] last read history.
|
||||||
*/
|
*/
|
||||||
// FIXME: Migrate to SQLDelight, on halt: in StorIO transaction
|
|
||||||
private suspend fun saveChapterHistory(readerChapter: ReaderChapter) {
|
private suspend fun saveChapterHistory(readerChapter: ReaderChapter) {
|
||||||
if (!preferences.incognitoMode().get()) {
|
if (!preferences.incognitoMode().get()) {
|
||||||
val readAt = Date().time
|
val readAt = Date().time
|
||||||
val sessionReadDuration = chapterReadStartTime?.let { readAt - it } ?: 0
|
val sessionReadDuration = chapterReadStartTime?.let { readAt - it } ?: 0
|
||||||
val oldTimeRead = db.getHistoryByChapterUrl(readerChapter.chapter.url).executeAsBlocking()?.time_read ?: 0
|
val oldTimeRead = getHistory.awaitByChapterUrl(readerChapter.chapter.url)?.time_read ?: 0
|
||||||
val history = History.create(readerChapter.chapter).apply {
|
val history = History.create(readerChapter.chapter).apply {
|
||||||
last_read = readAt
|
last_read = readAt
|
||||||
time_read = sessionReadDuration + oldTimeRead
|
time_read = sessionReadDuration + oldTimeRead
|
||||||
}
|
}
|
||||||
db.upsertHistoryLastRead(history).executeAsBlocking()
|
upsertHistory.await(history)
|
||||||
chapterReadStartTime = null
|
chapterReadStartTime = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -731,7 +737,7 @@ class ReaderViewModel(
|
||||||
|
|
||||||
mutableState.update {
|
mutableState.update {
|
||||||
it.copy(
|
it.copy(
|
||||||
manga = db.getManga(manga.id!!).executeAsBlocking(),
|
manga = getManga.awaitById(manga.id!!),
|
||||||
viewerChapters = currChapters,
|
viewerChapters = currChapters,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -766,7 +772,7 @@ class ReaderViewModel(
|
||||||
if (currChapters != null) {
|
if (currChapters != null) {
|
||||||
mutableState.update {
|
mutableState.update {
|
||||||
it.copy(
|
it.copy(
|
||||||
manga = db.getManga(manga.id!!).executeAsBlocking(),
|
manga = getManga.awaitById(manga.id!!),
|
||||||
viewerChapters = currChapters,
|
viewerChapters = currChapters,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,8 @@ import yokai.domain.extension.repo.interactor.GetExtensionRepoCount
|
||||||
import yokai.domain.extension.repo.interactor.ReplaceExtensionRepo
|
import yokai.domain.extension.repo.interactor.ReplaceExtensionRepo
|
||||||
import yokai.domain.extension.repo.interactor.UpdateExtensionRepo
|
import yokai.domain.extension.repo.interactor.UpdateExtensionRepo
|
||||||
import yokai.domain.history.HistoryRepository
|
import yokai.domain.history.HistoryRepository
|
||||||
|
import yokai.domain.history.interactor.GetHistory
|
||||||
|
import yokai.domain.history.interactor.UpsertHistory
|
||||||
import yokai.domain.library.custom.CustomMangaRepository
|
import yokai.domain.library.custom.CustomMangaRepository
|
||||||
import yokai.domain.library.custom.interactor.CreateCustomManga
|
import yokai.domain.library.custom.interactor.CreateCustomManga
|
||||||
import yokai.domain.library.custom.interactor.DeleteCustomManga
|
import yokai.domain.library.custom.interactor.DeleteCustomManga
|
||||||
|
@ -67,6 +69,8 @@ fun domainModule() = module {
|
||||||
factory { UpdateChapter(get()) }
|
factory { UpdateChapter(get()) }
|
||||||
|
|
||||||
single<HistoryRepository> { HistoryRepositoryImpl(get()) }
|
single<HistoryRepository> { HistoryRepositoryImpl(get()) }
|
||||||
|
factory { GetHistory(get()) }
|
||||||
|
factory { UpsertHistory(get()) }
|
||||||
|
|
||||||
factory { GetRecents(get(), get()) }
|
factory { GetRecents(get(), get()) }
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,21 @@
|
||||||
package yokai.data.history
|
package yokai.data.history
|
||||||
|
|
||||||
|
import eu.kanade.tachiyomi.data.database.models.History
|
||||||
import eu.kanade.tachiyomi.data.database.models.MangaChapterHistory
|
import eu.kanade.tachiyomi.data.database.models.MangaChapterHistory
|
||||||
import eu.kanade.tachiyomi.util.system.toInt
|
import eu.kanade.tachiyomi.util.system.toInt
|
||||||
import yokai.data.DatabaseHandler
|
import yokai.data.DatabaseHandler
|
||||||
import yokai.domain.history.HistoryRepository
|
import yokai.domain.history.HistoryRepository
|
||||||
|
|
||||||
class HistoryRepositoryImpl(private val handler: DatabaseHandler) : HistoryRepository {
|
class HistoryRepositoryImpl(private val handler: DatabaseHandler) : HistoryRepository {
|
||||||
|
override suspend fun upsert(chapterId: Long, lastRead: Long, timeRead: Long) =
|
||||||
|
handler.awaitOneOrNullExecutable(true) {
|
||||||
|
historyQueries.upsert(chapterId, lastRead, timeRead)
|
||||||
|
historyQueries.selectLastInsertedRowId()
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun getByChapterUrl(chapterUrl: String): History? =
|
||||||
|
handler.awaitOneOrNull { historyQueries.getByChapterUrl(chapterUrl, History::mapper) }
|
||||||
|
|
||||||
override suspend fun getRecentsUngrouped(
|
override suspend fun getRecentsUngrouped(
|
||||||
filterScanlators: Boolean,
|
filterScanlators: Boolean,
|
||||||
search: String,
|
search: String,
|
||||||
|
|
|
@ -1,8 +1,12 @@
|
||||||
package yokai.domain.history
|
package yokai.domain.history
|
||||||
|
|
||||||
|
import eu.kanade.tachiyomi.data.database.models.History
|
||||||
import eu.kanade.tachiyomi.data.database.models.MangaChapterHistory
|
import eu.kanade.tachiyomi.data.database.models.MangaChapterHistory
|
||||||
|
|
||||||
interface HistoryRepository {
|
interface HistoryRepository {
|
||||||
|
suspend fun upsert(chapterId: Long, lastRead: Long, timeRead: Long): Long?
|
||||||
|
suspend fun getByChapterUrl(chapterUrl: String): History?
|
||||||
|
|
||||||
suspend fun getRecentsUngrouped(filterScanlators: Boolean, search: String = "", limit: Long = 25L, offset: Long = 0L): List<MangaChapterHistory>
|
suspend fun getRecentsUngrouped(filterScanlators: Boolean, search: String = "", limit: Long = 25L, offset: Long = 0L): List<MangaChapterHistory>
|
||||||
suspend fun getRecentsBySeries(filterScanlators: Boolean, search: String = "", limit: Long = 25L, offset: Long = 0L): List<MangaChapterHistory>
|
suspend fun getRecentsBySeries(filterScanlators: Boolean, search: String = "", limit: Long = 25L, offset: Long = 0L): List<MangaChapterHistory>
|
||||||
suspend fun getRecentsAll(includeRead: Boolean, filterScanlators: Boolean, search: String = "", limit: Long = 25L, offset: Long = 0L): List<MangaChapterHistory>
|
suspend fun getRecentsAll(includeRead: Boolean, filterScanlators: Boolean, search: String = "", limit: Long = 25L, offset: Long = 0L): List<MangaChapterHistory>
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
package yokai.domain.history.interactor
|
||||||
|
|
||||||
|
import yokai.domain.history.HistoryRepository
|
||||||
|
|
||||||
|
class GetHistory(
|
||||||
|
private val historyRepository: HistoryRepository
|
||||||
|
) {
|
||||||
|
suspend fun awaitByChapterUrl(chapterUrl: String) = historyRepository.getByChapterUrl(chapterUrl)
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
package yokai.domain.history.interactor
|
||||||
|
|
||||||
|
import eu.kanade.tachiyomi.data.database.models.History
|
||||||
|
import yokai.domain.history.HistoryRepository
|
||||||
|
|
||||||
|
class UpsertHistory(
|
||||||
|
private val historyRepository: HistoryRepository
|
||||||
|
) {
|
||||||
|
suspend fun await(chapterId: Long, lastRead: Long, timeRead: Long) =
|
||||||
|
historyRepository.upsert(chapterId, lastRead, timeRead)
|
||||||
|
|
||||||
|
suspend fun await(history: History) =
|
||||||
|
historyRepository.upsert(history.chapter_id, history.last_read, history.time_read)
|
||||||
|
}
|
|
@ -9,6 +9,26 @@ CREATE TABLE history(
|
||||||
|
|
||||||
CREATE INDEX history_history_chapter_id_index ON history(history_chapter_id);
|
CREATE INDEX history_history_chapter_id_index ON history(history_chapter_id);
|
||||||
|
|
||||||
|
upsert:
|
||||||
|
INSERT INTO history(history_chapter_id, history_last_read, history_time_read)
|
||||||
|
VALUES(:historyChapterId, :historyLastRead, :historyTimeRead)
|
||||||
|
ON CONFLICT(history_chapter_id)
|
||||||
|
DO UPDATE
|
||||||
|
SET
|
||||||
|
history_last_read = :historyLastRead,
|
||||||
|
history_time_read = :historyTimeRead
|
||||||
|
WHERE history_chapter_id = history_chapter_id;
|
||||||
|
|
||||||
|
selectLastInsertedRowId:
|
||||||
|
SELECT last_insert_rowid();
|
||||||
|
|
||||||
|
getByChapterUrl:
|
||||||
|
SELECT history.*
|
||||||
|
FROM history
|
||||||
|
JOIN chapters
|
||||||
|
ON history.history_chapter_id = chapters._id
|
||||||
|
WHERE chapters.url = :chapterUrl AND history.history_chapter_id = chapters._id;
|
||||||
|
|
||||||
getRecentsUngrouped:
|
getRecentsUngrouped:
|
||||||
SELECT
|
SELECT
|
||||||
M.*,
|
M.*,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue