Skip to content

Commit 9d45ba3

Browse files
committed
Extract into record
1 parent 720a4e9 commit 9d45ba3

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

Diff for: demos/supabase-todolist/lib/widgets/guard_by_sync.dart

+5-3
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ class GuardBySync extends StatelessWidget {
2727
final (didSync, progress) = switch (priority) {
2828
null => (
2929
status.hasSynced ?? false,
30-
status.downloadProgress?.progress
30+
status.downloadProgress?.untilCompletion
3131
),
3232
var priority? => (
3333
status.statusForPriority(priority).hasSynced ?? false,
34-
status.downloadProgress?.progressFor(priority)
34+
status.downloadProgress?.untilPriority(priority)
3535
),
3636
};
3737

@@ -42,7 +42,9 @@ class GuardBySync extends StatelessWidget {
4242
child: Column(
4343
children: [
4444
const Text('Busy with sync...'),
45-
LinearProgressIndicator(value: progress),
45+
LinearProgressIndicator(value: progress?.fraction),
46+
if (progress case final progress?)
47+
Text('${progress.completed} out of ${progress.total}')
4648
],
4749
),
4850
);

Diff for: packages/powersync_core/lib/src/sync_status.dart

+24
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,12 @@ final class InternalSyncDownloadProgress {
288288
static const _mapEquality = MapEquality<Object?, Object?>();
289289
}
290290

291+
typedef ProgressWithOperations = ({
292+
int total,
293+
int completed,
294+
double fraction,
295+
});
296+
291297
/// Provides realtime progress about how PowerSync is downloading rows.
292298
///
293299
/// The reported progress always reflects the status towards the end of a
@@ -322,6 +328,24 @@ extension type SyncDownloadProgress._(InternalSyncDownloadProgress _internal) {
322328
/// a number between 0 and 1.
323329
double get progress => _internal._totalDownloaded / _internal._totalTarget;
324330

331+
ProgressWithOperations get untilCompletion => (
332+
total: total,
333+
completed: downloaded,
334+
fraction: progress,
335+
);
336+
337+
ProgressWithOperations untilPriority(BucketPriority priority) {
338+
final downloaded = downloadedFor(priority);
339+
final total = totalFor(priority);
340+
final progress = total == 0 ? 0.0 : downloaded / total;
341+
342+
return (
343+
total: totalFor(priority),
344+
completed: downloaded,
345+
fraction: progress,
346+
);
347+
}
348+
325349
/// Returns how many operations have been downloaded for buckets in
326350
/// [priority].
327351
///

0 commit comments

Comments
 (0)