Skip to content

Commit 720a4e9

Browse files
committed
Adopt progress api in demo
1 parent cadd682 commit 720a4e9

File tree

2 files changed

+71
-24
lines changed

2 files changed

+71
-24
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:powersync/powersync.dart' hide Column;
3+
import 'package:powersync_flutter_demo/powersync.dart';
4+
5+
/// A widget that shows [child] after a complete sync on the database has
6+
/// completed and a progress bar before that.
7+
class GuardBySync extends StatelessWidget {
8+
final Widget child;
9+
10+
/// When set, wait only for a complete sync within the [BucketPriority]
11+
/// instead of a full sync.
12+
final BucketPriority? priority;
13+
14+
const GuardBySync({
15+
super.key,
16+
required this.child,
17+
this.priority,
18+
});
19+
20+
@override
21+
Widget build(BuildContext context) {
22+
return StreamBuilder<SyncStatus>(
23+
stream: db.statusStream,
24+
initialData: db.currentStatus,
25+
builder: (context, snapshot) {
26+
final status = snapshot.requireData;
27+
final (didSync, progress) = switch (priority) {
28+
null => (
29+
status.hasSynced ?? false,
30+
status.downloadProgress?.progress
31+
),
32+
var priority? => (
33+
status.statusForPriority(priority).hasSynced ?? false,
34+
status.downloadProgress?.progressFor(priority)
35+
),
36+
};
37+
38+
if (didSync) {
39+
return child;
40+
} else {
41+
return Center(
42+
child: Column(
43+
children: [
44+
const Text('Busy with sync...'),
45+
LinearProgressIndicator(value: progress),
46+
],
47+
),
48+
);
49+
}
50+
},
51+
);
52+
}
53+
}

demos/supabase-todolist/lib/widgets/lists_page.dart

+18-24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'package:flutter/material.dart';
22
import 'package:powersync/powersync.dart';
3-
import 'package:powersync_flutter_demo/powersync.dart';
3+
import 'package:powersync_flutter_demo/widgets/guard_by_sync.dart';
44

55
import './list_item.dart';
66
import './list_item_dialog.dart';
@@ -46,29 +46,23 @@ final class ListsWidget extends StatelessWidget {
4646

4747
@override
4848
Widget build(BuildContext context) {
49-
return FutureBuilder(
50-
future: db.waitForFirstSync(priority: _listsPriority),
51-
builder: (context, snapshot) {
52-
if (snapshot.connectionState == ConnectionState.done) {
53-
return StreamBuilder(
54-
stream: TodoList.watchListsWithStats(),
55-
builder: (context, snapshot) {
56-
if (snapshot.data case final todoLists?) {
57-
return ListView(
58-
padding: const EdgeInsets.symmetric(vertical: 8.0),
59-
children: todoLists.map((list) {
60-
return ListItemWidget(list: list);
61-
}).toList(),
62-
);
63-
} else {
64-
return const CircularProgressIndicator();
65-
}
66-
},
67-
);
68-
} else {
69-
return const Text('Busy with sync...');
70-
}
71-
},
49+
return GuardBySync(
50+
priority: _listsPriority,
51+
child: StreamBuilder(
52+
stream: TodoList.watchListsWithStats(),
53+
builder: (context, snapshot) {
54+
if (snapshot.data case final todoLists?) {
55+
return ListView(
56+
padding: const EdgeInsets.symmetric(vertical: 8.0),
57+
children: todoLists.map((list) {
58+
return ListItemWidget(list: list);
59+
}).toList(),
60+
);
61+
} else {
62+
return const CircularProgressIndicator();
63+
}
64+
},
65+
),
7266
);
7367
}
7468

0 commit comments

Comments
 (0)