Skip to content

Commit cf5b65c

Browse files
committed
feat(restore): report per-book progress from run()
1 parent b599cf9 commit cf5b65c

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

app/src/main/java/io/theficos/ereader/domain/restore/RestoreInProgressUseCase.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import io.theficos.ereader.core.model.DocumentIdentity
44
import io.theficos.ereader.data.library.LibraryItemResponse
55
import io.theficos.ereader.data.sync.ProgressItemDto
66

7+
/** Live progress of a restore run: [done] of [total] candidate books processed. */
8+
data class RestoreProgress(val done: Int, val total: Int)
9+
710
/** Outcome counters for one restore run, rendered as a snackbar summary. */
811
data class RestoreSummary(
912
val requested: Int,
@@ -48,7 +51,7 @@ class RestoreInProgressUseCase(
4851
private val applyPositions: suspend (List<ProgressItemDto>) -> Unit,
4952
private val minPercentExclusive: Double = 0.0,
5053
) {
51-
suspend fun run(): RestoreSummary {
54+
suspend fun run(onProgress: (done: Int, total: Int) -> Unit = { _, _ -> }): RestoreSummary {
5255
val mirrorByHash = fetchLibraryItems().associateBy { it.contentHash }
5356
val inProgress = fetchInProgress().filter {
5457
it.finishedAt == null && it.abandonedAt == null && it.percent > minPercentExclusive
@@ -59,11 +62,13 @@ class RestoreInProgressUseCase(
5962
RestoreCandidate(progress = p, opdsHref = href, title = item.title, authors = item.authors)
6063
}
6164

65+
onProgress(0, candidates.size)
66+
6267
var downloaded = 0
6368
var skippedExisting = 0
6469
var skippedUnfetchable = 0
6570
var failed = 0
66-
for (c in candidates) {
71+
candidates.forEachIndexed { index, c ->
6772
when {
6873
!isHttp(c.opdsHref) -> skippedUnfetchable++
6974
isPresent(c.identity) -> skippedExisting++
@@ -74,6 +79,7 @@ class RestoreInProgressUseCase(
7479
failed++
7580
}
7681
}
82+
onProgress(index + 1, candidates.size)
7783
}
7884

7985
// Restore positions for every in-progress item; items whose document

app/src/test/java/io/theficos/ereader/domain/restore/RestoreInProgressUseCaseTest.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ class RestoreInProgressUseCaseTest {
9292
assertThat(downloaded).containsExactly("https://s/2.epub")
9393
}
9494

95+
@Test fun `run reports progress from 0 to total over candidates`() = runTest {
96+
val events = mutableListOf<Pair<Int, Int>>()
97+
useCase(
98+
mirror = listOf(item("h1", "https://s/1.epub"), item("h2", "https://s/2.epub")),
99+
prog = listOf(progress("h1"), progress("h2")),
100+
).run { done, total -> events += done to total }
101+
assertThat(events).containsExactly(0 to 2, 1 to 2, 2 to 2).inOrder()
102+
}
103+
95104
@Test fun `applies positions for all in-progress items including those not in the mirror`() = runTest {
96105
val applied = mutableListOf<ProgressItemDto>()
97106
useCase(

0 commit comments

Comments
 (0)