Yokan / rebuild
Course 1 8624561

HistoryNavigation.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/activity/HistoryNavigation.kt

implementation50 lines
1package eu.friedcutlet.yokan.ui.screens.activity23import eu.friedcutlet.yokan.data.YokanRepository4import eu.friedcutlet.yokan.domain.History5import eu.friedcutlet.yokan.domain.ItemType6import eu.friedcutlet.yokan.services.notifications.InAppNotifications7import eu.friedcutlet.yokan.ui.navigation.Navigator8import eu.friedcutlet.yokan.ui.navigation.Route910/**11 * Opens the reader at the most recent history entry of [itemType] whose title and chapter still12 * exist; posts [emptyMessage] and returns false when there is none.13 */14internal fun resumeLatestHistory(repository: YokanRepository, nav: Navigator, itemType: ItemType, emptyMessage: String): Boolean {15    val latest = latestResumableHistory(repository.history(), itemType) { entry ->16        val manga = repository.manga(entry.mangaId)?.takeIf { it.itemType == itemType } ?: return@latestResumableHistory null17        repository.chapters(entry.mangaId).firstOrNull { it.id == entry.chapterId }?.let { Route.Reader(manga.id, it.id) }18    }19    if (latest == null) {20        InAppNotifications.post(emptyMessage)21        return false22    }23    nav.navigate(latest)24    return true25}2627internal fun <T : Any> latestResumableHistory(28    history: List<History>,29    itemType: ItemType,30    resolve: (History) -> T?,31): T? = history.asSequence()32    .filter { it.itemType == itemType }33    .sortedByDescending { it.updatedAt }34    .mapNotNull(resolve)35    .firstOrNull()3637internal class HistoryNavigationActivation(private val doubleActivationWindowMs: Long = 400L) {38    private var lastActivationAt: Long? = null3940    fun activate(now: Long, onSingle: () -> Unit, onDouble: () -> Unit) {41        val previous = lastActivationAt42        lastActivationAt = now43        if (previous != null && now - previous in 0..doubleActivationWindowMs) {44            lastActivationAt = null45            onDouble()46        } else {47            onSingle()48        }49    }50}51

Search chapters, reference and source filenames.