Skip to content

Commit 505acb7

Browse files
committed
Clean up wait for first sync in lists widget
1 parent cfdc111 commit 505acb7

File tree

2 files changed

+29
-56
lines changed

2 files changed

+29
-56
lines changed

demos/supabase-todolist/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ bucket_definitions:
4343
- select * from todos where list_id = bucket.list_id
4444
```
4545
46-
The rules synchronize list with a higher priority the items within the list. This can be
46+
**Note**: These rules showcase [prioritized sync](https://docs.powersync.com/usage/use-case-examples/prioritized-sync),
47+
by syncing a user's lists with a higher priority than the items within a list (todos). This can be
4748
useful to keep the list overview page reactive during a large sync cycle affecting many
4849
rows in the `user_todos` bucket. The two buckets can also be unified into a single one if
4950
priorities are not important (the app will work without changes):
Lines changed: 27 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import 'dart:async';
2-
31
import 'package:flutter/material.dart';
42
import 'package:powersync/powersync.dart';
3+
import 'package:powersync_flutter_demo/powersync.dart';
54

65
import './list_item.dart';
76
import './list_item_dialog.dart';
@@ -42,63 +41,36 @@ class ListsPage extends StatelessWidget {
4241
}
4342
}
4443

45-
class ListsWidget extends StatefulWidget {
44+
final class ListsWidget extends StatelessWidget {
4645
const ListsWidget({super.key});
4746

48-
@override
49-
State<StatefulWidget> createState() {
50-
return _ListsWidgetState();
51-
}
52-
}
53-
54-
class _ListsWidgetState extends State<ListsWidget> {
55-
static final _listsPriority = BucketPriority(1);
56-
57-
List<TodoList> _data = [];
58-
bool hasSynced = false;
59-
StreamSubscription? _subscription;
60-
StreamSubscription? _syncStatusSubscription;
61-
62-
_ListsWidgetState();
63-
64-
@override
65-
void initState() {
66-
super.initState();
67-
final stream = TodoList.watchListsWithStats();
68-
_subscription = stream.listen((data) {
69-
if (!context.mounted) {
70-
return;
71-
}
72-
setState(() {
73-
_data = data;
74-
});
75-
});
76-
_syncStatusSubscription = TodoList.watchSyncStatus().listen((status) {
77-
if (!context.mounted) {
78-
return;
79-
}
80-
setState(() {
81-
hasSynced = status.statusForPriority(_listsPriority).hasSynced ?? false;
82-
});
83-
});
84-
}
85-
86-
@override
87-
void dispose() {
88-
super.dispose();
89-
_subscription?.cancel();
90-
_syncStatusSubscription?.cancel();
91-
}
92-
9347
@override
9448
Widget build(BuildContext context) {
95-
return !hasSynced
96-
? const Text("Busy with sync...")
97-
: ListView(
98-
padding: const EdgeInsets.symmetric(vertical: 8.0),
99-
children: _data.map((list) {
100-
return ListItemWidget(list: list);
101-
}).toList(),
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+
},
10267
);
68+
} else {
69+
return const Text('Busy with sync...');
70+
}
71+
},
72+
);
10373
}
74+
75+
static final _listsPriority = BucketPriority(1);
10476
}

0 commit comments

Comments
 (0)