Skip to content

Commit

Permalink
Fix read chapters deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
Koitharu committed Oct 13, 2024
1 parent 702ee70 commit b810639
Showing 1 changed file with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@ import kotlinx.coroutines.launch
import org.koitharu.kotatsu.core.model.findById
import org.koitharu.kotatsu.core.model.ids
import org.koitharu.kotatsu.core.model.isLocal
import org.koitharu.kotatsu.core.parser.MangaRepository
import org.koitharu.kotatsu.core.util.ext.printStackTraceDebug
import org.koitharu.kotatsu.history.data.HistoryRepository
import org.koitharu.kotatsu.local.data.LocalMangaRepository
import org.koitharu.kotatsu.local.data.LocalStorageChanges
import org.koitharu.kotatsu.local.domain.model.LocalManga
import org.koitharu.kotatsu.parsers.model.Manga
import org.koitharu.kotatsu.parsers.model.MangaChapter
import org.koitharu.kotatsu.parsers.util.recoverCatchingCancellable
import org.koitharu.kotatsu.parsers.util.runCatchingCancellable
import javax.inject.Inject

class DeleteReadChaptersUseCase @Inject constructor(
private val localMangaRepository: LocalMangaRepository,
private val historyRepository: HistoryRepository,
private val mangaRepositoryFactory: MangaRepository.Factory,
@LocalStorageChanges private val localStorageChanges: MutableSharedFlow<LocalManga?>,
) {

Expand Down Expand Up @@ -68,8 +72,8 @@ class DeleteReadChaptersUseCase @Inject constructor(

private suspend fun getDeletionTask(manga: LocalManga): DeletionTask? {
val history = historyRepository.getOne(manga.manga) ?: return null
val chapters = manga.manga.chapters ?: localMangaRepository.getDetails(manga.manga).chapters
if (chapters.isNullOrEmpty()) {
val chapters = getAllChapters(manga)
if (chapters.isEmpty()) {
return null
}
val branch = (chapters.findById(history.chapterId) ?: return null).branch
Expand All @@ -89,6 +93,21 @@ class DeleteReadChaptersUseCase @Inject constructor(
localStorageChanges.emit(subject.copy(manga = updated))
}

private suspend fun getAllChapters(manga: LocalManga): List<MangaChapter> = runCatchingCancellable {
val remoteManga = checkNotNull(localMangaRepository.getRemoteManga(manga.manga))
checkNotNull(mangaRepositoryFactory.create(remoteManga.source).getDetails(remoteManga).chapters)
}.recoverCatchingCancellable {
checkNotNull(
manga.manga.chapters.let {
if (it.isNullOrEmpty()) {
localMangaRepository.getDetails(manga.manga).chapters
} else {
it
}
},
)
}.getOrDefault(manga.manga.chapters.orEmpty())

private class DeletionTask(
val manga: LocalManga,
val chaptersIds: Set<Long>,
Expand Down

0 comments on commit b810639

Please sign in to comment.