Add favorite entry alternative handling, allowing parennt versions to take priority for favorites sync

This commit is contained in:
Jobobby04
2023-05-12 22:50:23 -04:00
parent e9a3463455
commit 282a0c4e16
12 changed files with 333 additions and 34 deletions
@@ -52,6 +52,7 @@ import tachiyomi.domain.manga.interactor.InsertFlatMetadata
import tachiyomi.domain.manga.interactor.InsertMergedReference
import tachiyomi.domain.manga.interactor.SetCustomMangaInfo
import tachiyomi.domain.manga.interactor.SetMangaFilteredScanlators
import tachiyomi.domain.manga.interactor.InsertFavoriteEntryAlternative
import tachiyomi.domain.manga.interactor.UpdateMergedSettings
import tachiyomi.domain.manga.repository.CustomMangaRepository
import tachiyomi.domain.manga.repository.FavoritesEntryRepository
@@ -140,6 +141,7 @@ class SYDomainModule : InjektModule {
addFactory { GetFavoriteEntries(get()) }
addFactory { InsertFavoriteEntries(get()) }
addFactory { DeleteFavoriteEntries(get()) }
addFactory { InsertFavoriteEntryAlternative(get()) }
addSingletonFactory<SavedSearchRepository> { SavedSearchRepositoryImpl(get()) }
addFactory { GetSavedSearchById(get()) }
@@ -2,6 +2,7 @@ package exh.eh
import android.content.Context
import eu.kanade.domain.manga.interactor.UpdateManga
import exh.metadata.metadata.EHentaiSearchMetadata
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
@@ -18,6 +19,8 @@ import tachiyomi.domain.history.interactor.UpsertHistory
import tachiyomi.domain.history.model.History
import tachiyomi.domain.history.model.HistoryUpdate
import tachiyomi.domain.manga.interactor.GetManga
import tachiyomi.domain.manga.interactor.InsertFavoriteEntryAlternative
import tachiyomi.domain.manga.model.FavoriteEntryAlternative
import tachiyomi.domain.manga.model.Manga
import tachiyomi.domain.manga.model.MangaUpdate
import uy.kohesive.injekt.injectLazy
@@ -41,6 +44,7 @@ class EHentaiUpdateHelper(context: Context) {
private val upsertHistory: UpsertHistory by injectLazy()
private val removeHistory: RemoveHistory by injectLazy()
private val getHistoryByMangaId: GetHistoryByMangaId by injectLazy()
private val insertFavoriteEntryAlternative: InsertFavoriteEntryAlternative by injectLazy()
/**
* @param chapters Cannot be an empty list!
@@ -123,6 +127,12 @@ class EHentaiUpdateHelper(context: Context) {
upsertHistory.await(it)
}
// Update favorites entry database
val favoriteEntryUpdate = getFavoriteEntryAlternative(accepted, toDiscard)
if (favoriteEntryUpdate != null) {
insertFavoriteEntryAlternative.await(favoriteEntryUpdate)
}
// Copy categories from all chains to accepted manga
val newCategories = rootsToMutate.flatMap { chapterChain ->
@@ -145,7 +155,24 @@ class EHentaiUpdateHelper(context: Context) {
}
}
fun getHistory(
private fun getFavoriteEntryAlternative(
accepted: ChapterChain,
toDiscard: List<ChapterChain>,
): FavoriteEntryAlternative? {
val favorite = toDiscard.find { it.manga.favorite } ?: return null
val gid = EHentaiSearchMetadata.galleryId(accepted.manga.url)
val token = EHentaiSearchMetadata.galleryToken(accepted.manga.url)
return FavoriteEntryAlternative(
otherGid = gid,
otherToken = token,
gid = EHentaiSearchMetadata.galleryId(favorite.manga.url),
token = EHentaiSearchMetadata.galleryToken(favorite.manga.url),
)
}
private fun getHistory(
currentChapters: List<Chapter>,
chainsAsChapters: List<Chapter>,
chainsAsHistory: List<History>,
@@ -19,14 +19,16 @@ import tachiyomi.domain.manga.interactor.GetFavorites
import tachiyomi.domain.manga.interactor.InsertFavoriteEntries
import tachiyomi.domain.manga.model.FavoriteEntry
import tachiyomi.domain.manga.model.Manga
import uy.kohesive.injekt.injectLazy
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
class LocalFavoritesStorage {
private val getFavorites: GetFavorites by injectLazy()
private val getCategories: GetCategories by injectLazy()
private val deleteFavoriteEntries: DeleteFavoriteEntries by injectLazy()
private val getFavoriteEntries: GetFavoriteEntries by injectLazy()
private val insertFavoriteEntries: InsertFavoriteEntries by injectLazy()
class LocalFavoritesStorage(
private val getFavorites: GetFavorites = Injekt.get(),
private val getCategories: GetCategories = Injekt.get(),
private val deleteFavoriteEntries: DeleteFavoriteEntries = Injekt.get(),
private val getFavoriteEntries: GetFavoriteEntries = Injekt.get(),
private val insertFavoriteEntries: InsertFavoriteEntries = Injekt.get(),
) {
suspend fun getChangedDbEntries() = getFavorites.await()
.asFlow()
@@ -67,27 +69,29 @@ class LocalFavoritesStorage {
val databaseEntries = getFavoriteEntries.await()
val added = terminated.filter {
queryListForEntry(databaseEntries, it) == null
}
val added = terminated.groupBy { it.gid to it.token }
.filter { (_, values) ->
values.all { queryListForEntry(databaseEntries, it) == null }
}
.map { it.value.first() }
val removed = databaseEntries
.filter {
queryListForEntry(terminated, it) == null
} /*.map {
todo see what this does
realm.copyFromRealm(it)
}*/
.groupBy { it.gid to it.token }
.filter { (_, values) ->
values.all { queryListForEntry(terminated, it) == null }
}
.map { it.value.first() }
return ChangeSet(added, removed)
}
private fun FavoriteEntry.urlEquals(other: FavoriteEntry) = (gid == other.gid && token == other.token) ||
(otherGid != null && otherToken != null && (otherGid == other.gid && otherToken == other.token)) ||
(other.otherGid != null && other.otherToken != null && (gid == other.otherGid && token == other.otherToken)) ||
(otherGid != null && otherToken != null && other.otherGid != null && other.otherToken != null && otherGid == other.otherGid && otherToken == other.otherToken)
private fun queryListForEntry(list: List<FavoriteEntry>, entry: FavoriteEntry) =
list.find {
it.gid == entry.gid &&
it.token == entry.token &&
it.category == entry.category
}
list.find { it.urlEquals(entry) && it.category == entry.category }
private suspend fun Flow<Manga>.loadDbCategories(): Flow<Pair<Int, Manga>> {
val dbCategories = getCategories.await()