From 0657a5292404e9bc79a847faa244d9b2552f5bfa Mon Sep 17 00:00:00 2001 From: AntsyLich <59261191+AntsyLich@users.noreply.github.com> Date: Fri, 28 Oct 2022 04:03:52 +0600 Subject: [PATCH] Cleanup history repository (#8297) * Cleanup history repository * Lint --- .../data/history/HistoryRepositoryImpl.kt | 43 ------------------ .../java/eu/kanade/domain/DomainModule.kt | 2 +- .../history/interactor/GetNextChapter.kt | 45 ++++++++++++++++--- .../history/repository/HistoryRepository.kt | 3 -- 4 files changed, 40 insertions(+), 53 deletions(-) diff --git a/app/src/main/java/eu/kanade/data/history/HistoryRepositoryImpl.kt b/app/src/main/java/eu/kanade/data/history/HistoryRepositoryImpl.kt index 44cd13b53..15aa46de0 100644 --- a/app/src/main/java/eu/kanade/data/history/HistoryRepositoryImpl.kt +++ b/app/src/main/java/eu/kanade/data/history/HistoryRepositoryImpl.kt @@ -1,13 +1,9 @@ package eu.kanade.data.history import eu.kanade.data.DatabaseHandler -import eu.kanade.data.chapter.chapterMapper -import eu.kanade.data.manga.mangaMapper -import eu.kanade.domain.chapter.model.Chapter import eu.kanade.domain.history.model.HistoryUpdate import eu.kanade.domain.history.model.HistoryWithRelations import eu.kanade.domain.history.repository.HistoryRepository -import eu.kanade.domain.manga.model.Manga import eu.kanade.tachiyomi.util.system.logcat import kotlinx.coroutines.flow.Flow import logcat.LogPriority @@ -28,45 +24,6 @@ class HistoryRepositoryImpl( } } - override suspend fun getNextChapter(mangaId: Long, chapterId: Long): Chapter? { - val chapter = handler.awaitOne { chaptersQueries.getChapterById(chapterId, chapterMapper) } - val manga = handler.awaitOne { mangasQueries.getMangaById(mangaId, mangaMapper) } - - if (!chapter.read) { - return chapter - } - - val sortFunction: (Chapter, Chapter) -> Int = when (manga.sorting) { - Manga.CHAPTER_SORTING_SOURCE -> { c1, c2 -> c2.sourceOrder.compareTo(c1.sourceOrder) } - Manga.CHAPTER_SORTING_NUMBER -> { c1, c2 -> c1.chapterNumber.compareTo(c2.chapterNumber) } - Manga.CHAPTER_SORTING_UPLOAD_DATE -> { c1, c2 -> c1.dateUpload.compareTo(c2.dateUpload) } - else -> throw NotImplementedError("Unknown sorting method") - } - - val chapters = handler.awaitList { chaptersQueries.getChaptersByMangaId(mangaId, chapterMapper) } - .sortedWith(sortFunction) - - val currChapterIndex = chapters.indexOfFirst { chapter.id == it.id } - return when (manga.sorting) { - Manga.CHAPTER_SORTING_SOURCE -> chapters.getOrNull(currChapterIndex + 1) - Manga.CHAPTER_SORTING_NUMBER -> { - val chapterNumber = chapter.chapterNumber - - ((currChapterIndex + 1) until chapters.size) - .map { chapters[it] } - .firstOrNull { - it.chapterNumber > chapterNumber && - it.chapterNumber <= chapterNumber + 1 - } - } - Manga.CHAPTER_SORTING_UPLOAD_DATE -> { - chapters.drop(currChapterIndex + 1) - .firstOrNull { it.dateUpload >= chapter.dateUpload } - } - else -> throw NotImplementedError("Unknown sorting method") - } - } - override suspend fun resetHistory(historyId: Long) { try { handler.await { historyQueries.resetHistoryById(historyId) } diff --git a/app/src/main/java/eu/kanade/domain/DomainModule.kt b/app/src/main/java/eu/kanade/domain/DomainModule.kt index 751e6f5ea..5b2fab18b 100644 --- a/app/src/main/java/eu/kanade/domain/DomainModule.kt +++ b/app/src/main/java/eu/kanade/domain/DomainModule.kt @@ -93,7 +93,7 @@ class DomainModule : InjektModule { addFactory { GetLibraryManga(get()) } addFactory { GetMangaWithChapters(get(), get()) } addFactory { GetManga(get()) } - addFactory { GetNextChapter(get()) } + addFactory { GetNextChapter(get(), get(), get(), get()) } addFactory { ResetViewerFlags(get()) } addFactory { SetMangaChapterFlags(get()) } addFactory { SetMangaDefaultChapterFlags(get(), get(), get()) } diff --git a/app/src/main/java/eu/kanade/domain/history/interactor/GetNextChapter.kt b/app/src/main/java/eu/kanade/domain/history/interactor/GetNextChapter.kt index 8fa8c8bf0..53e21323e 100644 --- a/app/src/main/java/eu/kanade/domain/history/interactor/GetNextChapter.kt +++ b/app/src/main/java/eu/kanade/domain/history/interactor/GetNextChapter.kt @@ -1,18 +1,51 @@ package eu.kanade.domain.history.interactor +import eu.kanade.domain.chapter.interactor.GetChapter +import eu.kanade.domain.chapter.interactor.GetChapterByMangaId import eu.kanade.domain.chapter.model.Chapter import eu.kanade.domain.history.repository.HistoryRepository +import eu.kanade.domain.manga.interactor.GetManga +import eu.kanade.domain.manga.model.Manga +import eu.kanade.tachiyomi.util.chapter.getChapterSort class GetNextChapter( - private val repository: HistoryRepository, + private val getChapter: GetChapter, + private val getChapterByMangaId: GetChapterByMangaId, + private val getManga: GetManga, + private val historyRepository: HistoryRepository, ) { - suspend fun await(mangaId: Long, chapterId: Long): Chapter? { - return repository.getNextChapter(mangaId, chapterId) + suspend fun await(): Chapter? { + val history = historyRepository.getLastHistory() ?: return null + return await(history.mangaId, history.chapterId) } - suspend fun await(): Chapter? { - val history = repository.getLastHistory() ?: return null - return repository.getNextChapter(history.mangaId, history.chapterId) + suspend fun await(mangaId: Long, chapterId: Long): Chapter? { + val chapter = getChapter.await(chapterId)!! + val manga = getManga.await(mangaId)!! + + if (!chapter.read) return chapter + + val chapters = getChapterByMangaId.await(mangaId) + .sortedWith(getChapterSort(manga, sortDescending = false)) + + val currChapterIndex = chapters.indexOfFirst { chapter.id == it.id } + return when (manga.sorting) { + Manga.CHAPTER_SORTING_SOURCE -> chapters.getOrNull(currChapterIndex + 1) + Manga.CHAPTER_SORTING_NUMBER -> { + val chapterNumber = chapter.chapterNumber + + ((currChapterIndex + 1) until chapters.size) + .map { chapters[it] } + .firstOrNull { + it.chapterNumber > chapterNumber && it.chapterNumber <= chapterNumber + 1 + } + } + Manga.CHAPTER_SORTING_UPLOAD_DATE -> { + chapters.drop(currChapterIndex + 1) + .firstOrNull { it.dateUpload >= chapter.dateUpload } + } + else -> throw NotImplementedError("Invalid chapter sorting method: ${manga.sorting}") + } } } diff --git a/app/src/main/java/eu/kanade/domain/history/repository/HistoryRepository.kt b/app/src/main/java/eu/kanade/domain/history/repository/HistoryRepository.kt index dd18c300a..b20a01f10 100644 --- a/app/src/main/java/eu/kanade/domain/history/repository/HistoryRepository.kt +++ b/app/src/main/java/eu/kanade/domain/history/repository/HistoryRepository.kt @@ -1,6 +1,5 @@ package eu.kanade.domain.history.repository -import eu.kanade.domain.chapter.model.Chapter import eu.kanade.domain.history.model.HistoryUpdate import eu.kanade.domain.history.model.HistoryWithRelations import kotlinx.coroutines.flow.Flow @@ -11,8 +10,6 @@ interface HistoryRepository { suspend fun getLastHistory(): HistoryWithRelations? - suspend fun getNextChapter(mangaId: Long, chapterId: Long): Chapter? - suspend fun resetHistory(historyId: Long) suspend fun resetHistoryByMangaId(mangaId: Long)