Skip to content

Commit 83f25bc

Browse files
committed
Simplify API
1 parent 5113f5c commit 83f25bc

File tree

4 files changed

+51
-31
lines changed

4 files changed

+51
-31
lines changed

core/src/commonIntegrationTest/kotlin/com/powersync/sync/SyncProgressTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,14 @@ class SyncProgressTest : BaseInMemorySyncTest() {
8282
val progress = item.downloadProgress ?: error("Expected download progress on $item")
8383

8484
assertTrue { item.downloading }
85-
assertEquals(total.first, progress.untilCompletion.completed)
86-
assertEquals(total.second, progress.untilCompletion.total)
85+
assertEquals(total.first, progress.downloadedOperations)
86+
assertEquals(total.second, progress.totalOperations)
8787

8888
priorities.forEach { (priority, expected) ->
8989
val (expectedDownloaded, expectedTotal) = expected
9090
val progress = progress.untilPriority(priority)
91-
assertEquals(expectedDownloaded, progress.completed)
92-
assertEquals(expectedTotal, progress.total)
91+
assertEquals(expectedDownloaded, progress.downloadedOperations)
92+
assertEquals(expectedTotal, progress.totalOperations)
9393
}
9494
}
9595

core/src/commonMain/kotlin/com/powersync/sync/Progress.kt

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,48 @@ import com.powersync.bucket.LocalOperationCounters
77
/**
88
* Information about a progressing download.
99
*
10-
* This reports the [total] amount of operations to download, how many of them have already been [completed] and finally
11-
* a [fraction] indicating relative progress.
10+
* This reports the [totalOperations] amount of operations to download, how many of them have already been
11+
* [downloadedOperations] and finally a [fraction] indicating relative progress.
1212
*
1313
* To obtain a [ProgressWithOperations] instance, use a method on [SyncDownloadProgress] which in turn is available
1414
* on [SyncStatusData].
1515
*/
16-
public data class ProgressWithOperations(
17-
val completed: Int,
18-
val total: Int,
19-
) {
16+
public interface ProgressWithOperations {
17+
/**
18+
* How many operations need to be downloaded in total for the current download to complete.
19+
*/
20+
public val totalOperations: Int
21+
22+
/**
23+
* How many operations, out of [totalOperations], have already been downloaded.
24+
*/
25+
public val downloadedOperations: Int
26+
2027
/**
21-
* The relative amount of [total] items that have been [completed], as a number between `0.0` and `1.0`.
28+
* The relative amount of [totalOperations] to items in [downloadedOperations], as a number between `0.0` and `1.0`.
2229
*/
2330
public val fraction: Float get() {
24-
if (completed == 0) {
31+
if (totalOperations == 0) {
2532
return 0.0f
2633
}
2734

28-
return completed.toFloat() / total.toFloat()
35+
return downloadedOperations.toFloat() / totalOperations.toFloat()
2936
}
3037
}
3138

39+
internal data class ProgressInfo(
40+
override val downloadedOperations: Int,
41+
override val totalOperations: Int,
42+
): ProgressWithOperations
43+
3244
/**
3345
* Provides realtime progress on how PowerSync is downloading rows.
3446
*
47+
* This type reports progress by implementing [ProgressWithOperations], meaning that the [totalOperations],
48+
* [downloadedOperations] and [fraction] getters are available on this instance.
49+
* Additionally, it's possible to obtain the progress towards a specific priority only (instead of tracking progress for
50+
* the entire download) by using [untilPriority].
51+
*
3552
* The reported progress always reflects the status towards the end of a sync iteration (after which a consistent
3653
* snapshot of all buckets is available locally).
3754
*
@@ -46,7 +63,17 @@ public data class ProgressWithOperations(
4663
@ConsistentCopyVisibility
4764
public data class SyncDownloadProgress private constructor(
4865
private val buckets: Map<String, BucketProgress>,
49-
) {
66+
): ProgressWithOperations {
67+
68+
override val downloadedOperations: Int
69+
override val totalOperations: Int
70+
71+
init {
72+
val (target, completed) = targetAndCompletedCounts(BucketPriority.FULL_SYNC_PRIORITY)
73+
totalOperations = target
74+
downloadedOperations = completed
75+
}
76+
5077
/**
5178
* Creates download progress information from the local progress counters since the last full sync and the target
5279
* checkpoint.
@@ -69,14 +96,6 @@ public data class SyncDownloadProgress private constructor(
6996
},
7097
)
7198

72-
/**
73-
* Download progress towards a complete checkpoint being received.
74-
*
75-
* The returned [ProgressWithOperations] instance tracks the target amount of operations that need to be downloaded
76-
* in total and how many of them have already been received.
77-
*/
78-
public val untilCompletion: ProgressWithOperations get() = untilPriority(BucketPriority.FULL_SYNC_PRIORITY)
79-
8099
/**
81100
* Returns download progress towards all data up until the specified [priority] being received.
82101
*
@@ -85,7 +104,7 @@ public data class SyncDownloadProgress private constructor(
85104
*/
86105
public fun untilPriority(priority: BucketPriority): ProgressWithOperations {
87106
val (total, completed) = targetAndCompletedCounts(priority)
88-
return ProgressWithOperations(completed = completed, total = total)
107+
return ProgressInfo(totalOperations = total, downloadedOperations = completed)
89108
}
90109

91110
internal fun incrementDownloaded(batch: SyncDataBatch): SyncDownloadProgress =

core/src/commonTest/kotlin/com/powersync/sync/ProgressTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import kotlin.test.assertEquals
66
class ProgressTest {
77
@Test
88
fun reportsFraction() {
9-
assertEquals(0.0f, ProgressWithOperations(0, 10).fraction)
10-
assertEquals(0.5f, ProgressWithOperations(5, 10).fraction)
11-
assertEquals(1.0f, ProgressWithOperations(10, 10).fraction)
9+
assertEquals(0.0f, ProgressInfo(0, 10).fraction)
10+
assertEquals(0.5f, ProgressInfo(5, 10).fraction)
11+
assertEquals(1.0f, ProgressInfo(10, 10).fraction)
1212

13-
assertEquals(0.0f, ProgressWithOperations(0, 0).fraction)
13+
assertEquals(0.0f, ProgressInfo(0, 0).fraction)
1414
}
1515
}

demos/supabase-todolist/shared/src/commonMain/kotlin/com/powersync/demos/components/GuardBySync.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import androidx.compose.ui.unit.dp
1717
import com.powersync.PowerSyncDatabase
1818
import com.powersync.bucket.BucketPriority
1919
import com.powersync.compose.composeState
20+
import com.powersync.sync.SyncStatusData
2021
import org.koin.compose.koinInject
2122

2223
/**
@@ -30,7 +31,7 @@ fun GuardBySync(
3031
priority: BucketPriority? = null,
3132
content: @Composable () -> Unit
3233
) {
33-
val state by db.currentStatus.composeState()
34+
val state: SyncStatusData by db.currentStatus.composeState()
3435

3536
if (state.hasSynced == true) {
3637
content()
@@ -55,7 +56,7 @@ fun GuardBySync(
5556

5657
val progress = state.downloadProgress?.let {
5758
if (priority == null) {
58-
it.untilCompletion
59+
it
5960
} else {
6061
it.untilPriority(priority)
6162
}
@@ -66,10 +67,10 @@ fun GuardBySync(
6667
progress = progress.fraction,
6768
)
6869

69-
if (progress.total == progress.completed) {
70+
if (progress.downloadedOperations == progress.totalOperations) {
7071
Text("Applying server-side changes...")
7172
} else {
72-
Text("Downloaded ${progress.completed} out of ${progress.total}.")
73+
Text("Downloaded ${progress.downloadedOperations} out of ${progress.totalOperations}.")
7374
}
7475
} else {
7576
LinearProgressIndicator(

0 commit comments

Comments
 (0)