Cleanup related to fetch interval display

This commit is contained in:
arkon
2023-07-29 10:29:53 -04:00
parent fe90546821
commit 3ad4f1114a
15 changed files with 93 additions and 85 deletions

View File

@@ -13,32 +13,32 @@ import kotlin.math.absoluteValue
const val MAX_GRACE_PERIOD = 28
class SetMangaUpdateInterval(
class SetFetchInterval(
private val libraryPreferences: LibraryPreferences = Injekt.get(),
) {
fun updateInterval(
fun update(
manga: Manga,
chapters: List<Chapter>,
zonedDateTime: ZonedDateTime,
fetchRange: Pair<Long, Long>,
): MangaUpdate? {
val currentFetchRange = if (fetchRange.first == 0L && fetchRange.second == 0L) {
getCurrentFetchRange(ZonedDateTime.now())
val currentInterval = if (fetchRange.first == 0L && fetchRange.second == 0L) {
getCurrent(ZonedDateTime.now())
} else {
fetchRange
}
val interval = manga.calculateInterval.takeIf { it < 0 } ?: calculateInterval(chapters, zonedDateTime)
val nextUpdate = calculateNextUpdate(manga, interval, zonedDateTime, currentFetchRange)
val interval = manga.fetchInterval.takeIf { it < 0 } ?: calculateInterval(chapters, zonedDateTime)
val nextUpdate = calculateNextUpdate(manga, interval, zonedDateTime, currentInterval)
return if (manga.nextUpdate == nextUpdate && manga.calculateInterval == interval) {
return if (manga.nextUpdate == nextUpdate && manga.fetchInterval == interval) {
null
} else {
MangaUpdate(id = manga.id, nextUpdate = nextUpdate, calculateInterval = interval)
MangaUpdate(id = manga.id, nextUpdate = nextUpdate, fetchInterval = interval)
}
}
fun getCurrentFetchRange(timeToCal: ZonedDateTime): Pair<Long, Long> {
fun getCurrent(timeToCal: ZonedDateTime): Pair<Long, Long> {
// lead range and the following range depend on if updateOnlyExpectedPeriod set.
var followRange = 0
var leadRange = 0
@@ -103,7 +103,7 @@ class SetMangaUpdateInterval(
): Long {
return if (
manga.nextUpdate !in fetchRange.first.rangeTo(fetchRange.second + 1) ||
manga.calculateInterval == 0
manga.fetchInterval == 0
) {
val latestDate = ZonedDateTime.ofInstant(Instant.ofEpochMilli(manga.lastUpdate), zonedDateTime.zone).toLocalDate().atStartOfDay()
val timeSinceLatest = ChronoUnit.DAYS.between(latestDate, zonedDateTime).toInt()

View File

@@ -10,7 +10,7 @@ data class Manga(
val favorite: Boolean,
val lastUpdate: Long,
val nextUpdate: Long,
val calculateInterval: Int,
val fetchInterval: Int,
val dateAdded: Long,
val viewerFlags: Long,
val chapterFlags: Long,
@@ -99,7 +99,7 @@ data class Manga(
favorite = false,
lastUpdate = 0L,
nextUpdate = 0L,
calculateInterval = 0,
fetchInterval = 0,
dateAdded = 0L,
viewerFlags = 0L,
chapterFlags = 0L,

View File

@@ -8,7 +8,7 @@ data class MangaUpdate(
val favorite: Boolean? = null,
val lastUpdate: Long? = null,
val nextUpdate: Long? = null,
val calculateInterval: Int? = null,
val fetchInterval: Int? = null,
val dateAdded: Long? = null,
val viewerFlags: Long? = null,
val chapterFlags: Long? = null,
@@ -32,7 +32,7 @@ fun Manga.toMangaUpdate(): MangaUpdate {
favorite = favorite,
lastUpdate = lastUpdate,
nextUpdate = nextUpdate,
calculateInterval = calculateInterval,
fetchInterval = fetchInterval,
dateAdded = dateAdded,
viewerFlags = viewerFlags,
chapterFlags = chapterFlags,

View File

@@ -10,14 +10,14 @@ import java.time.Duration
import java.time.ZonedDateTime
@Execution(ExecutionMode.CONCURRENT)
class SetMangaUpdateIntervalTest {
class SetFetchIntervalTest {
private val testTime = ZonedDateTime.parse("2020-01-01T00:00:00Z")
private var chapter = Chapter.create().copy(
dateFetch = testTime.toEpochSecond() * 1000,
dateUpload = testTime.toEpochSecond() * 1000,
)
private val setMangaUpdateInterval = SetMangaUpdateInterval(mockk())
private val setFetchInterval = SetFetchInterval(mockk())
private fun chapterAddTime(chapter: Chapter, duration: Duration): Chapter {
val newTime = testTime.plus(duration).toEpochSecond() * 1000
@@ -33,7 +33,7 @@ class SetMangaUpdateIntervalTest {
val newChapter = chapterAddTime(chapter, duration)
chapters.add(newChapter)
}
setMangaUpdateInterval.calculateInterval(chapters, testTime) shouldBe 7
setFetchInterval.calculateInterval(chapters, testTime) shouldBe 7
}
@Test
@@ -44,7 +44,7 @@ class SetMangaUpdateIntervalTest {
val newChapter = chapterAddTime(chapter, duration)
chapters.add(newChapter)
}
setMangaUpdateInterval.calculateInterval(chapters, testTime) shouldBe 7
setFetchInterval.calculateInterval(chapters, testTime) shouldBe 7
}
@Test
@@ -60,7 +60,7 @@ class SetMangaUpdateIntervalTest {
val newChapter = chapterAddTime(chapter, duration)
chapters.add(newChapter)
}
setMangaUpdateInterval.calculateInterval(chapters, testTime) shouldBe 7
setFetchInterval.calculateInterval(chapters, testTime) shouldBe 7
}
// Default 1 if interval less than 1
@@ -72,7 +72,7 @@ class SetMangaUpdateIntervalTest {
val newChapter = chapterAddTime(chapter, duration)
chapters.add(newChapter)
}
setMangaUpdateInterval.calculateInterval(chapters, testTime) shouldBe 1
setFetchInterval.calculateInterval(chapters, testTime) shouldBe 1
}
// Normal interval calculation
@@ -84,7 +84,7 @@ class SetMangaUpdateIntervalTest {
val newChapter = chapterAddTime(chapter, duration)
chapters.add(newChapter)
}
setMangaUpdateInterval.calculateInterval(chapters, testTime) shouldBe 1
setFetchInterval.calculateInterval(chapters, testTime) shouldBe 1
}
@Test
@@ -95,7 +95,7 @@ class SetMangaUpdateIntervalTest {
val newChapter = chapterAddTime(chapter, duration)
chapters.add(newChapter)
}
setMangaUpdateInterval.calculateInterval(chapters, testTime) shouldBe 2
setFetchInterval.calculateInterval(chapters, testTime) shouldBe 2
}
// If interval is decimal, floor to closest integer
@@ -107,7 +107,7 @@ class SetMangaUpdateIntervalTest {
val newChapter = chapterAddTime(chapter, duration)
chapters.add(newChapter)
}
setMangaUpdateInterval.calculateInterval(chapters, testTime) shouldBe 1
setFetchInterval.calculateInterval(chapters, testTime) shouldBe 1
}
@Test
@@ -118,7 +118,7 @@ class SetMangaUpdateIntervalTest {
val newChapter = chapterAddTime(chapter, duration)
chapters.add(newChapter)
}
setMangaUpdateInterval.calculateInterval(chapters, testTime) shouldBe 1
setFetchInterval.calculateInterval(chapters, testTime) shouldBe 1
}
// Use fetch time if upload time not available
@@ -130,6 +130,6 @@ class SetMangaUpdateIntervalTest {
val newChapter = chapterAddTime(chapter, duration).copy(dateUpload = 0L)
chapters.add(newChapter)
}
setMangaUpdateInterval.calculateInterval(chapters, testTime) shouldBe 1
setFetchInterval.calculateInterval(chapters, testTime) shouldBe 1
}
}