mirror of
https://github.com/null2264/yokai.git
synced 2025-06-21 10:44:42 +00:00
chore: More storio to sqldelight migration
- insert manga - update manga
This commit is contained in:
parent
28e551de36
commit
5ed2934b73
15 changed files with 204 additions and 17 deletions
|
@ -70,7 +70,7 @@ class MangaPutResolver : DefaultPutResolver<Manga>() {
|
|||
put(COL_CHAPTER_FLAGS, obj.chapter_flags)
|
||||
put(COL_DATE_ADDED, obj.date_added)
|
||||
put(COL_FILTERED_SCANLATORS, obj.filtered_scanlators)
|
||||
put(COL_UPDATE_STRATEGY, obj.update_strategy.let(updateStrategyAdapter::encode))
|
||||
put(COL_UPDATE_STRATEGY, obj.update_strategy.let(updateStrategyAdapter::encode).toInt())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -94,9 +94,9 @@ interface BaseMangaGetResolver {
|
|||
hide_title = cursor.getInt(cursor.getColumnIndex(COL_HIDE_TITLE)) == 1
|
||||
date_added = cursor.getLong(cursor.getColumnIndex(COL_DATE_ADDED))
|
||||
filtered_scanlators = cursor.getString(cursor.getColumnIndex(COL_FILTERED_SCANLATORS))
|
||||
update_strategy = cursor.getInt(cursor.getColumnIndex(COL_UPDATE_STRATEGY)).let(
|
||||
updateStrategyAdapter::decode,
|
||||
)
|
||||
update_strategy = cursor.getInt(cursor.getColumnIndex(COL_UPDATE_STRATEGY)).let {
|
||||
updateStrategyAdapter.decode(it.toLong())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -77,7 +77,7 @@ data class LibraryManga(
|
|||
this.chapter_flags = chapterFlags.toInt()
|
||||
this.date_added = dateAdded ?: 0L
|
||||
this.filtered_scanlators = filteredScanlators
|
||||
this.update_strategy = updateStrategy.toInt().let(updateStrategyAdapter::decode)
|
||||
this.update_strategy = updateStrategy.let(updateStrategyAdapter::decode)
|
||||
this.read = readCount.roundToInt()
|
||||
this.unread = maxOf((total - readCount).roundToInt(), 0)
|
||||
this.totalChapters = readCount.roundToInt()
|
||||
|
|
|
@ -419,7 +419,7 @@ interface Manga : SManga {
|
|||
this.hide_title = hideTitle > 0
|
||||
this.date_added = dateAdded ?: 0L
|
||||
this.filtered_scanlators = filteredScanlators
|
||||
this.update_strategy = updateStrategy.toInt().let(updateStrategyAdapter::decode)
|
||||
this.update_strategy = updateStrategy.let(updateStrategyAdapter::decode)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -91,8 +91,6 @@ interface MangaQueries : DbProvider {
|
|||
|
||||
fun insertManga(manga: Manga) = db.put().`object`(manga).prepare()
|
||||
|
||||
fun insertMangas(mangas: List<Manga>) = db.put().objects(mangas).prepare()
|
||||
|
||||
fun updateChapterFlags(manga: Manga) = db.put()
|
||||
.`object`(manga)
|
||||
.withPutResolver(MangaFlagsPutResolver(MangaTable.COL_CHAPTER_FLAGS, Manga::chapter_flags))
|
||||
|
|
|
@ -60,6 +60,8 @@ import uy.kohesive.injekt.injectLazy
|
|||
import yokai.domain.category.interactor.GetCategories
|
||||
import yokai.domain.chapter.interactor.GetChapters
|
||||
import yokai.domain.manga.interactor.GetLibraryManga
|
||||
import yokai.domain.manga.interactor.UpdateManga
|
||||
import yokai.domain.manga.models.MangaUpdate
|
||||
import yokai.util.isLewd
|
||||
import java.util.*
|
||||
import java.util.concurrent.*
|
||||
|
@ -86,6 +88,7 @@ class LibraryPresenter(
|
|||
private val getCategories: GetCategories by injectLazy()
|
||||
private val getLibraryManga: GetLibraryManga by injectLazy()
|
||||
private val getChapters: GetChapters by injectLazy()
|
||||
private val updateManga: UpdateManga by injectLazy()
|
||||
|
||||
private val libraryManga: List<LibraryManga> = emptyList()
|
||||
|
||||
|
@ -1131,9 +1134,9 @@ class LibraryPresenter(
|
|||
presenterScope.launch {
|
||||
// Create a set of the list
|
||||
val mangaToDelete = mangas.distinctBy { it.id }
|
||||
mangaToDelete.forEach { it.favorite = false }
|
||||
.mapNotNull { if (it.id != null) MangaUpdate(it.id!!, favorite = false) else null }
|
||||
|
||||
db.insertMangas(mangaToDelete).executeOnIO()
|
||||
withIOContext { updateManga.updateAll(mangaToDelete) }
|
||||
getLibrary()
|
||||
}
|
||||
}
|
||||
|
@ -1168,11 +1171,11 @@ class LibraryPresenter(
|
|||
fun reAddMangas(mangas: List<Manga>) {
|
||||
presenterScope.launch {
|
||||
val mangaToAdd = mangas.distinctBy { it.id }
|
||||
mangaToAdd.forEach { it.favorite = true }
|
||||
db.insertMangas(mangaToAdd).executeOnIO()
|
||||
.mapNotNull { if (it.id != null) MangaUpdate(it.id!!, favorite = true) else null }
|
||||
|
||||
withIOContext { updateManga.updateAll(mangaToAdd) }
|
||||
(view as? FilteredLibraryController)?.updateStatsPage()
|
||||
getLibrary()
|
||||
mangaToAdd.forEach { db.insertManga(it).executeAsBlocking() }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -30,6 +30,8 @@ import yokai.domain.library.custom.interactor.GetCustomManga
|
|||
import yokai.domain.library.custom.interactor.RelinkCustomManga
|
||||
import yokai.domain.manga.MangaRepository
|
||||
import yokai.domain.manga.interactor.GetLibraryManga
|
||||
import yokai.domain.manga.interactor.InsertManga
|
||||
import yokai.domain.manga.interactor.UpdateManga
|
||||
|
||||
class DomainModule : InjektModule {
|
||||
override fun InjektRegistrar.registerInjectables() {
|
||||
|
@ -51,6 +53,8 @@ class DomainModule : InjektModule {
|
|||
|
||||
addSingletonFactory<MangaRepository> { MangaRepositoryImpl(get()) }
|
||||
addFactory { GetLibraryManga(get()) }
|
||||
addFactory { InsertManga(get()) }
|
||||
addFactory { UpdateManga(get()) }
|
||||
|
||||
addSingletonFactory<ChapterRepository> { ChapterRepositoryImpl(get()) }
|
||||
addFactory { GetAvailableScanlators(get()) }
|
||||
|
|
|
@ -1,10 +1,14 @@
|
|||
package yokai.data.manga
|
||||
|
||||
import co.touchlab.kermit.Logger
|
||||
import eu.kanade.tachiyomi.data.database.models.LibraryManga
|
||||
import eu.kanade.tachiyomi.data.database.models.Manga
|
||||
import eu.kanade.tachiyomi.util.system.toInt
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import yokai.data.DatabaseHandler
|
||||
import yokai.data.updateStrategyAdapter
|
||||
import yokai.domain.manga.MangaRepository
|
||||
import yokai.domain.manga.models.MangaUpdate
|
||||
|
||||
class MangaRepositoryImpl(private val handler: DatabaseHandler) : MangaRepository {
|
||||
override suspend fun getManga(): List<Manga> =
|
||||
|
@ -18,4 +22,77 @@ class MangaRepositoryImpl(private val handler: DatabaseHandler) : MangaRepositor
|
|||
|
||||
override fun getLibraryMangaAsFlow(): Flow<List<LibraryManga>> =
|
||||
handler.subscribeToList { library_viewQueries.findAll(LibraryManga::mapper) }
|
||||
|
||||
override suspend fun update(update: MangaUpdate): Boolean {
|
||||
return try {
|
||||
partialUpdate(update)
|
||||
true
|
||||
} catch (e: Exception) {
|
||||
Logger.e { "Failed to update manga with id '${update.id}'" }
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun updateAll(updates: List<MangaUpdate>): Boolean {
|
||||
return try {
|
||||
partialUpdate(*updates.toTypedArray())
|
||||
true
|
||||
} catch (e: Exception) {
|
||||
Logger.e(e) { "Failed to bulk update manga" }
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun partialUpdate(vararg updates: MangaUpdate) {
|
||||
handler.await(inTransaction = true) {
|
||||
updates.forEach { update ->
|
||||
mangasQueries.update(
|
||||
source = update.source,
|
||||
url = update.url,
|
||||
artist = update.artist,
|
||||
author = update.author,
|
||||
description = update.description,
|
||||
genre = update.genres?.joinToString(),
|
||||
title = update.title,
|
||||
status = update.status?.toLong(),
|
||||
thumbnailUrl = update.thumbnailUrl,
|
||||
favorite = update.favorite?.toInt()?.toLong(),
|
||||
lastUpdate = update.lastUpdate,
|
||||
initialized = update.initialized,
|
||||
viewer = update.viewerFlags?.toLong(),
|
||||
hideTitle = update.hideTitle?.toInt()?.toLong(),
|
||||
chapterFlags = update.chapterFlags?.toLong(),
|
||||
dateAdded = update.dateAdded,
|
||||
filteredScanlators = update.filteredScanlators,
|
||||
updateStrategy = update.updateStrategy?.let(updateStrategyAdapter::encode),
|
||||
mangaId = update.id,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun insert(manga: Manga) =
|
||||
handler.awaitOneOrNullExecutable(inTransaction = true) {
|
||||
mangasQueries.insert(
|
||||
source = manga.source,
|
||||
url = manga.url,
|
||||
artist = manga.artist,
|
||||
author = manga.author,
|
||||
description = manga.description,
|
||||
genre = manga.genre,
|
||||
title = manga.title,
|
||||
status = manga.status.toLong(),
|
||||
thumbnailUrl = manga.thumbnail_url,
|
||||
favorite = manga.favorite.toInt().toLong(),
|
||||
lastUpdate = manga.last_update,
|
||||
initialized = manga.initialized,
|
||||
viewer = manga.viewer_flags.toLong(),
|
||||
hideTitle = manga.hide_title.toInt().toLong(),
|
||||
chapterFlags = manga.chapter_flags.toLong(),
|
||||
dateAdded = manga.date_added,
|
||||
filteredScanlators = manga.filtered_scanlators,
|
||||
updateStrategy = manga.update_strategy.let(updateStrategyAdapter::encode),
|
||||
)
|
||||
mangasQueries.selectLastInsertedRowId()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,10 +3,14 @@ package yokai.domain.manga
|
|||
import eu.kanade.tachiyomi.data.database.models.LibraryManga
|
||||
import eu.kanade.tachiyomi.data.database.models.Manga
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import yokai.domain.manga.models.MangaUpdate
|
||||
|
||||
interface MangaRepository {
|
||||
suspend fun getManga(): List<Manga>
|
||||
fun getMangaAsFlow(): Flow<List<Manga>>
|
||||
suspend fun getLibraryManga(): List<LibraryManga>
|
||||
fun getLibraryMangaAsFlow(): Flow<List<LibraryManga>>
|
||||
suspend fun update(update: MangaUpdate): Boolean
|
||||
suspend fun updateAll(updates: List<MangaUpdate>): Boolean
|
||||
suspend fun insert(manga: Manga): Long?
|
||||
}
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
package yokai.domain.manga.interactor
|
||||
|
||||
import eu.kanade.tachiyomi.data.database.models.Manga
|
||||
import yokai.domain.manga.MangaRepository
|
||||
|
||||
class InsertManga (
|
||||
private val mangaRepository: MangaRepository,
|
||||
) {
|
||||
suspend fun await(manga: Manga) = mangaRepository.insert(manga)
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package yokai.domain.manga.interactor
|
||||
|
||||
import yokai.domain.manga.MangaRepository
|
||||
import yokai.domain.manga.models.MangaUpdate
|
||||
|
||||
class UpdateManga (
|
||||
private val mangaRepository: MangaRepository,
|
||||
) {
|
||||
suspend fun update(update: MangaUpdate) = mangaRepository.update(update)
|
||||
suspend fun updateAll(updates: List<MangaUpdate>) = mangaRepository.updateAll(updates)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue