package eu.friedcutlet.yokan.app import coil3.PlatformContext import coil3.SingletonImageLoader import eu.friedcutlet.yokan.ui.image.clearCaches import eu.friedcutlet.yokan.services.catchingNonCancellation import eu.friedcutlet.yokan.services.notifications.InAppNotifications import eu.friedcutlet.yokan.services.updates.checkForAppUpdate import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.delay import kotlinx.coroutines.launch /** One-shot startup maintenance, run once from the app root after bootstrap. Each task runs concurrently. */ internal suspend fun runAppStartup(imageContext: PlatformContext): Unit = coroutineScope { val owner = YokanRuntime.activeSession val settings = owner.settings.db.value // "Clear chapter/episode cache on app launch": the same chapter cache as the Data and storage // button — Coil's disk+memory image caches (like Mangayomi's DefaultCacheManager.emptyCache()) // and the saved chapter page lists. launch(Dispatchers.Default) { if (!owner.services.database.settings().clearChapterCacheOnAppLaunch) return@launch runCatching { SingletonImageLoader.get(imageContext).clearCaches() owner.repository.clearPageCache() } } // Auto-prune browsed (non-favorite) titles the user never interacted with // (no history, downloads, or tracking), so cached browse results don't // pile up until a manual "Clean database". launch(Dispatchers.Default) { runCatching { owner.repository.pruneBrowsedTitles() } } // Startup extension repo check (Mangayomi's main_screen behavior): when // "Check for extension updates" is on, fetch every item-type repository // shortly after launch; the refresh auto-installs updates when // autoExtensionsUpdates is enabled. if (settings.checkForExtensionUpdates) { launch(Dispatchers.Default) { delay(2_000) owner.sourceInstall.refreshAllRepositories() } } // Startup app-update check (Mangayomi main_screen): compare the latest // GitHub release with the installed version and surface a toast. if (settings.checkForAppUpdates) { launch { delay(3_000) catchingNonCancellation { checkForAppUpdate(owner.services.http)?.let { release -> InAppNotifications.post( "Update available: ${release.tag}", "Open More → About to download it.", ) } } } } }