ChapterUpdateSchedule.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/domain/ChapterUpdateSchedule.kt
1package eu.friedcutlet.yokan.domain23import eu.friedcutlet.yokan.util.nowMillis4import kotlin.math.roundToInt5import kotlinx.datetime.LocalDate6import kotlinx.datetime.TimeZone7import kotlinx.datetime.atStartOfDayIn89private const val DayMillis = 86_400_000L1011/** Mean gap between known releases. Duplicate dates do not count as new intervals. */12fun meanChapterUpdateDays(uploadDates: Iterable<String>, now: Long = nowMillis()): Int? {13 val dates = uploadDates.mapNotNull { raw ->14 val value = raw.trim()15 val date = value.toLongOrNull()?.let {16 if (it in 1L until 10_000_000_000L) it * 1_000L else it17 } ?: runCatching {18 LocalDate.parse(value.take(10)).atStartOfDayIn(TimeZone.UTC).toEpochMilliseconds()19 }.getOrNull()20 date?.takeIf { it > 0 && it <= now }21 }.distinct().sorted()22 if (dates.size < 2) return null23 val mean = (dates.last() - dates.first()).toDouble() / (dates.size - 1) / DayMillis24 return mean.roundToInt().coerceAtLeast(1)25}2627/** null = automatic, zero = every update, positive = a manual cooldown. */28fun effectiveChapterUpdateDays(manualDays: Int?, uploadDates: Iterable<String>, now: Long = nowMillis()): Int =29 manualDays?.coerceAtLeast(0) ?: meanChapterUpdateDays(uploadDates, now) ?: 030