refactor: Properly relink custom manga info

This commit is contained in:
Ahmad Ansori Palembani 2024-06-07 13:36:13 +07:00
parent dabb2c622a
commit 9967647217
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6
7 changed files with 43 additions and 8 deletions

View file

@ -15,6 +15,7 @@ import dev.yokai.domain.library.custom.CustomMangaRepository
import dev.yokai.domain.library.custom.interactor.CreateCustomManga import dev.yokai.domain.library.custom.interactor.CreateCustomManga
import dev.yokai.domain.library.custom.interactor.DeleteCustomManga import dev.yokai.domain.library.custom.interactor.DeleteCustomManga
import dev.yokai.domain.library.custom.interactor.GetCustomManga import dev.yokai.domain.library.custom.interactor.GetCustomManga
import dev.yokai.domain.library.custom.interactor.RelinkCustomManga
import dev.yokai.domain.manga.MangaRepository import dev.yokai.domain.manga.MangaRepository
import dev.yokai.domain.manga.interactor.GetLibraryManga import dev.yokai.domain.manga.interactor.GetLibraryManga
import uy.kohesive.injekt.api.InjektModule import uy.kohesive.injekt.api.InjektModule
@ -39,6 +40,7 @@ class DomainModule : InjektModule {
addFactory { CreateCustomManga(get()) } addFactory { CreateCustomManga(get()) }
addFactory { DeleteCustomManga(get()) } addFactory { DeleteCustomManga(get()) }
addFactory { GetCustomManga(get()) } addFactory { GetCustomManga(get()) }
addFactory { RelinkCustomManga(get()) }
addSingletonFactory<MangaRepository> { MangaRepositoryImpl(get()) } addSingletonFactory<MangaRepository> { MangaRepositoryImpl(get()) }
addFactory { GetLibraryManga(get()) } addFactory { GetLibraryManga(get()) }

View file

@ -63,6 +63,9 @@ class CustomMangaRepositoryImpl(private val handler: DatabaseHandler) : CustomMa
} }
} }
override suspend fun relinkCustomManga(oldId: Long, newId: Long) =
handler.await { custom_manga_infoQueries.relink(newId, oldId) }
private fun mapCustomMangaInfo( private fun mapCustomMangaInfo(
mangaId: Long, mangaId: Long,
title: String?, title: String?,

View file

@ -28,4 +28,5 @@ interface CustomMangaRepository {
suspend fun insertBulkCustomManga(mangaList: List<CustomMangaInfo>) suspend fun insertBulkCustomManga(mangaList: List<CustomMangaInfo>)
suspend fun deleteCustomManga(mangaId: Long) suspend fun deleteCustomManga(mangaId: Long)
suspend fun deleteBulkCustomManga(mangaIds: List<Long>) suspend fun deleteBulkCustomManga(mangaIds: List<Long>)
suspend fun relinkCustomManga(oldId: Long, newId: Long)
} }

View file

@ -0,0 +1,9 @@
package dev.yokai.domain.library.custom.interactor
import dev.yokai.domain.library.custom.CustomMangaRepository
class RelinkCustomManga(
private val customMangaRepository: CustomMangaRepository,
) {
suspend fun await(oldId: Long, newId: Long) = customMangaRepository.relinkCustomManga(oldId, newId)
}

View file

@ -9,6 +9,7 @@ import dev.yokai.core.metadata.copyFromComicInfo
import dev.yokai.domain.library.custom.interactor.CreateCustomManga import dev.yokai.domain.library.custom.interactor.CreateCustomManga
import dev.yokai.domain.library.custom.interactor.DeleteCustomManga import dev.yokai.domain.library.custom.interactor.DeleteCustomManga
import dev.yokai.domain.library.custom.interactor.GetCustomManga import dev.yokai.domain.library.custom.interactor.GetCustomManga
import dev.yokai.domain.library.custom.interactor.RelinkCustomManga
import dev.yokai.domain.library.custom.model.CustomMangaInfo import dev.yokai.domain.library.custom.model.CustomMangaInfo
import dev.yokai.domain.library.custom.model.CustomMangaInfo.Companion.getMangaInfo import dev.yokai.domain.library.custom.model.CustomMangaInfo.Companion.getMangaInfo
import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.Manga
@ -39,6 +40,7 @@ class CustomMangaManager(val context: Context) {
private val createCustomManga: CreateCustomManga by injectLazy() private val createCustomManga: CreateCustomManga by injectLazy()
private val deleteCustomManga: DeleteCustomManga by injectLazy() private val deleteCustomManga: DeleteCustomManga by injectLazy()
private val getCustomManga: GetCustomManga by injectLazy() private val getCustomManga: GetCustomManga by injectLazy()
private val relinkCustomManga: RelinkCustomManga by injectLazy()
init { init {
scope.launch { scope.launch {
@ -112,15 +114,28 @@ class CustomMangaManager(val context: Context) {
saveCustomInfo { jsonFile.delete() } saveCustomInfo { jsonFile.delete() }
} }
private val CustomMangaInfo.shouldDelete
get() = (
title == null &&
author == null &&
artist == null &&
description == null &&
genre == null &&
(status ?: -1) == -1
)
suspend fun updateMangaInfo(oldId: Long?, newId: Long?, manga: CustomMangaInfo) {
if (oldId == null || newId == null) return
if (manga.shouldDelete) {
deleteCustomInfo(manga.mangaId)
} else {
relinkCustomManga.await(oldId, newId)
}
}
suspend fun saveMangaInfo(manga: CustomMangaInfo) { suspend fun saveMangaInfo(manga: CustomMangaInfo) {
val mangaId = manga.mangaId val mangaId = manga.mangaId
if (manga.title == null && if (manga.shouldDelete) {
manga.author == null &&
manga.artist == null &&
manga.description == null &&
manga.genre == null &&
(manga.status ?: -1) == -1
) {
deleteCustomInfo(mangaId) deleteCustomInfo(mangaId)
} else { } else {
addCustomInfo(manga) addCustomInfo(manga)

View file

@ -231,7 +231,7 @@ class MigrationProcessAdapter(
customMangaManager.getManga(prevManga)?.let { customManga -> customMangaManager.getManga(prevManga)?.let { customManga ->
customManga.id = manga.id!! customManga.id = manga.id!!
launchNow { launchNow {
customMangaManager.saveMangaInfo(customManga.getMangaInfo()) customMangaManager.updateMangaInfo(prevManga.id, manga.id, customManga.getMangaInfo())
} }
} }
} }

View file

@ -32,3 +32,8 @@ WHERE manga_id = :manga_id;
delete: delete:
DELETE FROM custom_manga_info DELETE FROM custom_manga_info
WHERE manga_id = :manga_id; WHERE manga_id = :manga_id;
relink:
UPDATE custom_manga_info
SET manga_id = :new_id
WHERE manga_id = :old_id;