From 3294dd6ca824466419b8f81db39b727ce061b094 Mon Sep 17 00:00:00 2001 From: Matthias Ahouansou Date: Thu, 10 Jul 2025 19:21:31 +0545 Subject: [PATCH] Use median to determine smart update interval (#2251) Co-authored-by: AntsyLich <59261191+AntsyLich@users.noreply.github.com> (cherry picked from commit d60241690b373e42215761b4108e52a7655daaeb) --- .../tachiyomi/domain/manga/interactor/FetchInterval.kt | 10 ++++------ .../domain/manga/interactor/FetchIntervalTest.kt | 4 ++-- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/domain/src/main/java/tachiyomi/domain/manga/interactor/FetchInterval.kt b/domain/src/main/java/tachiyomi/domain/manga/interactor/FetchInterval.kt index c2b239eb9..ad1a0f757 100644 --- a/domain/src/main/java/tachiyomi/domain/manga/interactor/FetchInterval.kt +++ b/domain/src/main/java/tachiyomi/domain/manga/interactor/FetchInterval.kt @@ -69,15 +69,13 @@ class FetchInterval( val interval = when { // Enough upload date from source uploadDates.size >= 3 -> { - val uploadDelta = uploadDates.last().until(uploadDates.first(), ChronoUnit.DAYS) - val uploadPeriod = uploadDates.indexOf(uploadDates.last()) - uploadDelta.floorDiv(uploadPeriod).toInt() + val ranges = uploadDates.windowed(2).map { x -> x[1].until(x[0], ChronoUnit.DAYS) }.sorted() + ranges[(ranges.size - 1) / 2].toInt() } // Enough fetch date from client fetchDates.size >= 3 -> { - val fetchDelta = fetchDates.last().until(fetchDates.first(), ChronoUnit.DAYS) - val uploadPeriod = fetchDates.indexOf(fetchDates.last()) - fetchDelta.floorDiv(uploadPeriod).toInt() + val ranges = fetchDates.windowed(2).map { x -> x[1].until(x[0], ChronoUnit.DAYS) }.sorted() + ranges[(ranges.size - 1) / 2].toInt() } // Default to 7 days else -> 7 diff --git a/domain/src/test/java/tachiyomi/domain/manga/interactor/FetchIntervalTest.kt b/domain/src/test/java/tachiyomi/domain/manga/interactor/FetchIntervalTest.kt index ccaaf24da..c27a71c94 100644 --- a/domain/src/test/java/tachiyomi/domain/manga/interactor/FetchIntervalTest.kt +++ b/domain/src/test/java/tachiyomi/domain/manga/interactor/FetchIntervalTest.kt @@ -125,11 +125,11 @@ class FetchIntervalTest { } @Test - fun `returns interval of 1 day when chapters are released just below every 2 days`() { + fun `returns interval of 2 days when chapters are released just below every 2 days`() { val chapters = (1..20).map { chapterWithTime(chapter, (43 * it).hours) } - fetchInterval.calculateInterval(chapters, testZoneId) shouldBe 1 + fetchInterval.calculateInterval(chapters, testZoneId) shouldBe 2 } private fun chapterWithTime(chapter: Chapter, duration: Duration): Chapter {