Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 91 additions & 63 deletions app/detekt-baseline.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class DownloadStatusReconciler @Inject constructor(
fun isFullyDownloaded(result: PrefetchResult): Boolean =
result.isStrictOfflineReady()

@Suppress("ReturnCount")
suspend fun reconcile(
item: LibraryItem,
result: PrefetchResult,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -622,22 +622,6 @@ class WebContentLoader @Suppress("LongParameterList") @Inject constructor(
)
}

private fun promoteHtmlToDownloads(url: String): File? {
val target = primaryCachedFile(url, StorageTier.DOWNLOADS)
if (target.exists()) return target
val src = legacyCachedFile(url).takeIf(File::exists)
?: primaryCachedFile(url, StorageTier.CACHE).takeIf(File::exists)
?: return null
target.parentFile?.mkdirs()
val moved = if (src.renameTo(target)) target else runCatching {
src.copyTo(target, overwrite = true)
src.delete()
target
}.getOrNull()
moved?.let { parsedContentCache.moveAlongside(src, it) }
return moved
}

private fun extractImageUrls(elements: List<ContentElement>): List<String> {
return elements.flatMap { element ->
when (element) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,20 +348,34 @@ class ReaderProgressController(
}

val isStable = firstVisibleItemSize >= MIN_STABLE_ITEM_SIZE_PX
val effectiveFraction = if (isStable) offsetFraction.coerceIn(0f, 1f) else FRACTION_UNKNOWN
val isTerminal = !canScrollForward
val effectiveFraction = when {
isTerminal -> offsetFraction.coerceIn(0f, 1f)
isStable -> offsetFraction.coerceIn(0f, 1f)
else -> FRACTION_UNKNOWN
}

val nextState = _progressState.value.copy(
scrollPosition = progress,
scrollProgress = progressInt,
scrollIndex = index,
scrollElementKey = if (isStable) elementKey else _progressState.value.scrollElementKey,
scrollElementKey = if (isStable || isTerminal) elementKey else _progressState.value.scrollElementKey,
scrollOffsetFraction = effectiveFraction,
firstVisibleItemSize = firstVisibleItemSize
)
_progressState.value = nextState

// Only schedule a DB write when the sample is stable enough to be meaningful.
if (!isStable || !isSnapshotPersistable(content, nextState)) {
// Terminal end-of-chapter samples are explicit intent: persist them via the
// PAGED_POSITION_ITEM_SIZE_PX sentinel so isSnapshotPersistable bypasses the
// upstream-layout-stability gate (which near the end almost always fails because
// earlier images haven't been measured yet).
val persistSnapshot = if (isTerminal) {
nextState.copy(firstVisibleItemSize = PAGED_POSITION_ITEM_SIZE_PX)
} else {
nextState
}

if ((!isStable && !isTerminal) || !isSnapshotPersistable(content, persistSnapshot)) {
lastRawScrollOffset = scrollOffset
return
}
Expand All @@ -376,7 +390,8 @@ class ReaderProgressController(
index = index,
elementKey = elementKey,
offsetFraction = effectiveFraction,
content = content
content = content,
forcePersist = isTerminal
)
}
lastRawScrollOffset = scrollOffset
Expand All @@ -390,7 +405,8 @@ class ReaderProgressController(
elementKey: String? = null,
offsetFraction: Float? = null,
currentChapterUrl: String? = null,
content: ChapterContent? = null
content: ChapterContent? = null,
forcePersist: Boolean = false
) {
val itemId = currentLibraryItemId ?: return
runCatching {
Expand All @@ -405,7 +421,8 @@ class ReaderProgressController(
scrollProgress = progress,
scrollIndex = lastIndex,
scrollElementKey = lastElementKey,
scrollOffsetFraction = lastFraction
scrollOffsetFraction = lastFraction,
firstVisibleItemSize = if (forcePersist) PAGED_POSITION_ITEM_SIZE_PX else latest.firstVisibleItemSize
)

if (isPlaceholderAtCurrentPosition(content, lastIndex)) return@runCatching
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import io.aatricks.easyreader.ui.theme.AccentTheme
import io.aatricks.easyreader.util.normalizeChapterList
import io.aatricks.easyreader.util.TextUtils
import io.aatricks.easyreader.util.UrlSecurity
import io.aatricks.easyreader.ui.viewmodel.ReaderProgressController.Companion.PAGED_POSITION_ITEM_SIZE_PX
import io.aatricks.easyreader.util.FieldUpdate
import io.aatricks.easyreader.util.computeAutoDeleteCandidates
import kotlinx.coroutines.CancellationException
Expand Down Expand Up @@ -1082,7 +1083,8 @@ class ReaderViewModel @Inject constructor(
index: Int? = null,
elementKey: String? = null,
offsetFraction: Float? = null,
currentChapterUrl: String? = null
currentChapterUrl: String? = null,
forcePersist: Boolean = false
): Unit {
progressController.updateReadingProgress(
progress = progress,
Expand All @@ -1091,7 +1093,8 @@ class ReaderViewModel @Inject constructor(
elementKey = elementKey,
offsetFraction = offsetFraction,
currentChapterUrl = currentChapterUrl,
content = _uiState.value.content
content = _uiState.value.content,
forcePersist = forcePersist
)
}

Expand Down Expand Up @@ -1228,19 +1231,26 @@ class ReaderViewModel @Inject constructor(
scrollElementKey = targetElementKey,
scrollOffsetFraction = targetFraction,
isPreciseRestore = false,
firstVisibleItemSize = progressController.progressState.value.firstVisibleItemSize,
firstVisibleItemSize = PAGED_POSITION_ITEM_SIZE_PX,
seekTrigger = System.currentTimeMillis(),
targetScrollPosition = if (targetPercent == 100f) 100f else null
)
)

// Seek-bar drag is explicit user intent. Mark it before scheduling the write so
// the restore loop triggered by seekTrigger does not later suppress saves, and
// pass forcePersist=true to bypass the upstream-layout-stability gate (which
// would otherwise reject seeks into chapters with unmeasured images).
progressController.markUserDragged()

viewModelScope.launch {
updateReadingProgress(
progress = targetPercent.toInt(),
scrollPosition = targetPercent,
index = roughIndex,
elementKey = targetElementKey,
offsetFraction = targetFraction
offsetFraction = targetFraction,
forcePersist = true
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("MagicNumber")

package io.aatricks.easyreader.util

import java.io.File
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class ChapterDownloadWorker @AssistedInject constructor(
private val downloadStatusReconciler: DownloadStatusReconciler
) : CoroutineWorker(appContext, params) {

@Suppress("ReturnCount")
override suspend fun doWork(): Result {
val url = inputData.getString(KEY_CHAPTER_URL)
if (url.isNullOrBlank()) {
Expand Down
Loading