Use domain layer for almost all SY code

This commit is contained in:
Jobobby04
2022-07-04 18:37:30 -04:00
parent 141b0477e7
commit 6954c299b5
14 changed files with 99 additions and 41 deletions
@@ -1,15 +1,14 @@
package exh.eh
import android.content.Context
import eu.kanade.data.DatabaseHandler
import eu.kanade.data.chapter.chapterMapper
import eu.kanade.data.history.historyMapper
import eu.kanade.domain.category.interactor.GetCategories
import eu.kanade.domain.category.interactor.SetMangaCategories
import eu.kanade.domain.chapter.interactor.GetChapterByMangaId
import eu.kanade.domain.chapter.interactor.GetChapterByUrl
import eu.kanade.domain.chapter.model.Chapter
import eu.kanade.domain.chapter.model.ChapterUpdate
import eu.kanade.domain.chapter.repository.ChapterRepository
import eu.kanade.domain.history.interactor.GetHistoryByMangaId
import eu.kanade.domain.history.interactor.RemoveHistoryById
import eu.kanade.domain.history.interactor.UpsertHistory
import eu.kanade.domain.history.model.History
@@ -35,7 +34,7 @@ class EHentaiUpdateHelper(context: Context) {
File(context.filesDir, "exh-plt.maftable"),
GalleryEntry.Serializer(),
)
private val handler: DatabaseHandler by injectLazy()
private val getChapterByUrl: GetChapterByUrl by injectLazy()
private val getChapterByMangaId: GetChapterByMangaId by injectLazy()
private val getManga: GetManga by injectLazy()
private val updateManga: UpdateManga by injectLazy()
@@ -44,6 +43,7 @@ class EHentaiUpdateHelper(context: Context) {
private val chapterRepository: ChapterRepository by injectLazy()
private val upsertHistory: UpsertHistory by injectLazy()
private val removeHistoryById: RemoveHistoryById by injectLazy()
private val getHistoryByMangaId: GetHistoryByMangaId by injectLazy()
/**
* @param chapters Cannot be an empty list!
@@ -55,8 +55,7 @@ class EHentaiUpdateHelper(context: Context) {
val chainsFlow = flowOf(chapters)
.map { chapterList ->
chapterList.flatMap { chapter ->
handler.awaitList { chaptersQueries.getChapterByUrl(chapter.url, chapterMapper) }
.map { it.mangaId }
getChapterByUrl.await(chapter.url).map { it.mangaId }
}.distinct()
}
.map { mangaIds ->
@@ -70,7 +69,7 @@ class EHentaiUpdateHelper(context: Context) {
getChapterByMangaId.await(mangaId)
}
val history = async(Dispatchers.IO) {
handler.awaitList { historyQueries.getHistoryByMangaId(mangaId, historyMapper) }
getHistoryByMangaId.await(mangaId)
}
ChapterChain(
manga.await() ?: return@coroutineScope null,
@@ -139,8 +138,8 @@ class EHentaiUpdateHelper(context: Context) {
// Copy categories from all chains to accepted manga
val newCategories = rootsToMutate.flatMap {
getCategories.await(it.manga.id).map { it.id }
val newCategories = rootsToMutate.flatMap { chapterChain ->
getCategories.await(chapterChain.manga.id).map { it.id }
}.distinct()
rootsToMutate.forEach {
setMangaCategories.await(it.manga.id, newCategories)
@@ -3,10 +3,12 @@ package exh.favorites
import android.content.Context
import android.net.wifi.WifiManager
import android.os.PowerManager
import eu.kanade.data.DatabaseHandler
import eu.kanade.domain.category.interactor.GetCategories
import eu.kanade.domain.category.interactor.InsertCategory
import eu.kanade.domain.category.interactor.SetMangaCategories
import eu.kanade.domain.category.interactor.UpdateCategory
import eu.kanade.domain.category.model.Category
import eu.kanade.domain.category.model.CategoryUpdate
import eu.kanade.domain.manga.interactor.GetLibraryManga
import eu.kanade.domain.manga.interactor.GetManga
import eu.kanade.domain.manga.interactor.UpdateManga
@@ -48,12 +50,13 @@ import kotlin.time.Duration.Companion.seconds
// TODO only apply database changes after sync
class FavoritesSyncHelper(val context: Context) {
private val handler: DatabaseHandler by injectLazy()
private val getLibraryManga: GetLibraryManga by injectLazy()
private val getCategories: GetCategories by injectLazy()
private val getManga: GetManga by injectLazy()
private val updateManga: UpdateManga by injectLazy()
private val setMangaCategories: SetMangaCategories by injectLazy()
private val insertCategory: InsertCategory by injectLazy()
private val updateCategory: UpdateCategory by injectLazy()
private val prefs: PreferencesHelper by injectLazy()
@@ -203,28 +206,25 @@ class FavoritesSyncHelper(val context: Context) {
private suspend fun applyRemoteCategories(categories: List<String>) {
val localCategories = getCategories.await()
val newLocalCategories = localCategories.toMutableList()
categories.forEachIndexed { index, remote ->
val local = localCategories.getOrElse(index) {
val newCategoryId = handler.awaitOne(true) {
categoriesQueries.insert(remote, index.toLong(), 0L, emptyList())
categoriesQueries.selectLastInsertedRowId()
when (val insertCategoryResult = insertCategory.await(remote, index.toLong())) {
is InsertCategory.Result.Error -> throw insertCategoryResult.error
is InsertCategory.Result.Success -> Category(insertCategoryResult.id, remote, index.toLong(), 0L, emptyList())
}
Category(newCategoryId, remote, index.toLong(), 0L, emptyList())
.also { newLocalCategories += it }
}
// Ensure consistent ordering and naming
if (local.name != remote || local.order != index.toLong()) {
handler.await {
categoriesQueries.update(
categoryId = local.id,
val result = updateCategory.await(
CategoryUpdate(
id = local.id,
order = index.toLong().takeIf { it != local.order },
name = remote.takeIf { it != local.name },
flags = null,
mangaOrder = null,
)
),
)
if (result is UpdateCategory.Result.Error) {
throw result.error
}
}
}