From 27f09480a0270c7758d77b08fe626c29f3da12e8 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Wed, 4 Jan 2023 13:11:11 +0200 Subject: [PATCH] Prefetch last manga --- .../details/service/MangaPrefetchService.kt | 34 ++++++++++++++++++- .../koitharu/kotatsu/main/ui/MainActivity.kt | 2 ++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/koitharu/kotatsu/details/service/MangaPrefetchService.kt b/app/src/main/java/org/koitharu/kotatsu/details/service/MangaPrefetchService.kt index cc5f5b974..06880b7cc 100644 --- a/app/src/main/java/org/koitharu/kotatsu/details/service/MangaPrefetchService.kt +++ b/app/src/main/java/org/koitharu/kotatsu/details/service/MangaPrefetchService.kt @@ -10,6 +10,7 @@ import org.koitharu.kotatsu.core.cache.ContentCache import org.koitharu.kotatsu.core.model.parcelable.ParcelableManga import org.koitharu.kotatsu.core.model.parcelable.ParcelableMangaChapters import org.koitharu.kotatsu.core.parser.MangaRepository +import org.koitharu.kotatsu.history.domain.HistoryRepository import org.koitharu.kotatsu.parsers.model.Manga import org.koitharu.kotatsu.parsers.model.MangaChapter import org.koitharu.kotatsu.parsers.model.MangaSource @@ -26,6 +27,9 @@ class MangaPrefetchService : CoroutineIntentService() { @Inject lateinit var cache: ContentCache + @Inject + lateinit var historyRepository: HistoryRepository + override suspend fun processIntent(startId: Int, intent: Intent) { when (intent.action) { ACTION_PREFETCH_DETAILS -> prefetchDetails( @@ -36,6 +40,8 @@ class MangaPrefetchService : CoroutineIntentService() { chapter = intent.getParcelableExtraCompat(EXTRA_CHAPTER) ?.chapters?.singleOrNull() ?: return, ) + + ACTION_PREFETCH_LAST -> prefetchLast() } } @@ -51,12 +57,31 @@ class MangaPrefetchService : CoroutineIntentService() { runCatchingCancellable { source.getPages(chapter) } } + private suspend fun prefetchLast() { + val last = historyRepository.getLastOrNull() ?: return + if (last.source == MangaSource.LOCAL) return + val repo = mangaRepositoryFactory.create(last.source) + val details = runCatchingCancellable { repo.getDetails(last) }.getOrNull() ?: return + val chapters = details.chapters + if (chapters.isNullOrEmpty()) { + return + } + val history = historyRepository.getOne(last) + val chapter = if (history == null) { + chapters.firstOrNull() + } else { + chapters.find { x -> x.id == history.chapterId } ?: chapters.firstOrNull() + } ?: return + runCatchingCancellable { repo.getPages(chapter) } + } + companion object { private const val EXTRA_MANGA = "manga" private const val EXTRA_CHAPTER = "manga" private const val ACTION_PREFETCH_DETAILS = "details" private const val ACTION_PREFETCH_PAGES = "pages" + private const val ACTION_PREFETCH_LAST = "last" fun prefetchDetails(context: Context, manga: Manga) { if (!isPrefetchAvailable(context, manga.source)) return @@ -74,7 +99,14 @@ class MangaPrefetchService : CoroutineIntentService() { context.startService(intent) } - private fun isPrefetchAvailable(context: Context, source: MangaSource): Boolean { + fun prefetchLast(context: Context) { + if (!isPrefetchAvailable(context, null)) return + val intent = Intent(context, MangaPrefetchService::class.java) + intent.action = ACTION_PREFETCH_LAST + context.startService(intent) + } + + private fun isPrefetchAvailable(context: Context, source: MangaSource?): Boolean { if (source == MangaSource.LOCAL) { return false } diff --git a/app/src/main/java/org/koitharu/kotatsu/main/ui/MainActivity.kt b/app/src/main/java/org/koitharu/kotatsu/main/ui/MainActivity.kt index 72fbd1a0f..c267a976f 100644 --- a/app/src/main/java/org/koitharu/kotatsu/main/ui/MainActivity.kt +++ b/app/src/main/java/org/koitharu/kotatsu/main/ui/MainActivity.kt @@ -41,6 +41,7 @@ import org.koitharu.kotatsu.R import org.koitharu.kotatsu.base.ui.BaseActivity import org.koitharu.kotatsu.base.ui.widgets.SlidingBottomNavigationView import org.koitharu.kotatsu.databinding.ActivityMainBinding +import org.koitharu.kotatsu.details.service.MangaPrefetchService import org.koitharu.kotatsu.details.ui.DetailsActivity import org.koitharu.kotatsu.main.ui.owners.AppBarOwner import org.koitharu.kotatsu.main.ui.owners.BottomNavOwner @@ -334,6 +335,7 @@ class MainActivity : TrackWorker.setup(applicationContext) SuggestionsWorker.setup(applicationContext) } + MangaPrefetchService.prefetchLast(this@MainActivity) requestNotificationsPermission() } }