Yokan / rebuild
Course 1 8624561

BackgroundScheduler.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/services/scheduling/BackgroundScheduler.kt

implementation85 lines
1package eu.friedcutlet.yokan.services.scheduling23import eu.friedcutlet.yokan.domain.ProfileId4import eu.friedcutlet.yokan.database.YokanDatabase5import eu.friedcutlet.yokan.util.nowMillis6import kotlin.time.Instant7import kotlinx.datetime.DateTimeUnit8import kotlinx.datetime.TimeZone9import kotlinx.datetime.atTime10import kotlinx.datetime.minus11import kotlinx.datetime.plus12import kotlinx.datetime.toInstant13import kotlinx.datetime.toLocalDateTime1415internal fun periodicMinutes(seconds: Int): Long =16    if (seconds <= 0) 0L else ((seconds + 59L) / 60L).coerceAtLeast(15L)1718/** Epoch millis of the most recent occurrence of [dailyAtMinutes] (local time) at or before [now]. */19@OptIn(kotlin.time.ExperimentalTime::class)20internal fun latestDailySlotMillis(21    dailyAtMinutes: Int,22    now: Long = nowMillis(),23    zone: TimeZone = TimeZone.currentSystemDefault(),24): Long {25    val today = Instant.fromEpochMilliseconds(now).toLocalDateTime(zone).date26    val slotToday = today.atTime(dailyAtMinutes / 60, dailyAtMinutes % 60).toInstant(zone).toEpochMilliseconds()27    return if (slotToday <= now) {28        slotToday29    } else {30        today.minus(1, DateTimeUnit.DAY).atTime(dailyAtMinutes / 60, dailyAtMinutes % 60).toInstant(zone).toEpochMilliseconds()31    }32}3334/** Millis until the next occurrence of [dailyAtMinutes] strictly after [now]. */35@OptIn(kotlin.time.ExperimentalTime::class)36internal fun nextDailySlotDelayMillis(37    dailyAtMinutes: Int,38    now: Long = nowMillis(),39    zone: TimeZone = TimeZone.currentSystemDefault(),40): Long {41    val last = Instant.fromEpochMilliseconds(latestDailySlotMillis(dailyAtMinutes, now, zone)).toLocalDateTime(zone)42    return last.date.plus(1, DateTimeUnit.DAY).atTime(last.hour, last.minute).toInstant(zone).toEpochMilliseconds() - now43}4445/** UI values 1..5 mean 6h / 12h / daily / 2 days / weekly. */46internal fun backupFrequencyHours(value: Int?): Long = when (value) {47    1 -> 6L48    2 -> 12L49    3 -> 24L50    4 -> 48L51    5 -> 168L52    else -> 0L53}5455/**56 * Periodic OS-level background workers. Android uses WorkManager, iOS uses57 * BGTaskScheduler, and desktop/web rely on [ForegroundScheduler] while open.58 */59interface BackgroundScheduler {60    /** [libraryUpdateDailyAtMinutes] >= 0 anchors the library update to that local time of day; else repeat every [libraryUpdateMinutes]. */61    fun schedule(62        metadataRefreshMinutes: Long,63        libraryUpdateMinutes: Long,64        libraryUpdateDailyAtMinutes: Int,65        autoSyncMinutes: Long,66        autoBackupHours: Long,67        requireUnmetered: Boolean,68        libraryConstraints: eu.friedcutlet.yokan.database.DbAutomaticUpdateConstraints = eu.friedcutlet.yokan.database.DbAutomaticUpdateConstraints(),69    )7071    fun cancelAll()72}7374expect fun createPlatformBackgroundScheduler(75    database: YokanDatabase,76    profileId: ProfileId = ProfileId.Default,77): BackgroundScheduler7879/** Stale owners are terminal no-ops; unreadable registry metadata is recoverable. */80val ProfileWorkResult<PeriodicTaskOutcome>.shouldRetry: Boolean81    get() = when (this) {82        is ProfileWorkResult.Completed -> value.shouldRetry83        ProfileWorkResult.Skipped -> false84        ProfileWorkResult.Retry -> true85    }86

Search chapters, reference and source filenames.