Skip to content

Commit 6c7bae4

Browse files
committed
Adopt in django todolist demo too
1 parent ccf67ef commit 6c7bae4

File tree

4 files changed

+76
-42
lines changed

4 files changed

+76
-42
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:powersync/powersync.dart' hide Column;
3+
import 'package:powersync_django_todolist_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?.untilCompletion
31+
),
32+
var priority? => (
33+
status.statusForPriority(priority).hasSynced ?? false,
34+
status.downloadProgress?.untilPriority(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?.fraction),
46+
if (progress case final progress?)
47+
Text('${progress.completed} out of ${progress.total}')
48+
],
49+
),
50+
);
51+
}
52+
},
53+
);
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import 'dart:async';
2-
31
import 'package:flutter/material.dart';
2+
import 'package:powersync_django_todolist_demo/widgets/guard_by_sync.dart';
43

54
import './list_item.dart';
65
import './list_item_dialog.dart';
@@ -41,48 +40,27 @@ class ListsPage extends StatelessWidget {
4140
}
4241
}
4342

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

47-
@override
48-
State<StatefulWidget> createState() {
49-
return _ListsWidgetState();
50-
}
51-
}
52-
53-
class _ListsWidgetState extends State<ListsWidget> {
54-
List<TodoList> _data = [];
55-
StreamSubscription? _subscription;
56-
57-
_ListsWidgetState();
58-
59-
@override
60-
void initState() {
61-
super.initState();
62-
final stream = TodoList.watchListsWithStats();
63-
_subscription = stream.listen((data) {
64-
if (!context.mounted) {
65-
return;
66-
}
67-
setState(() {
68-
_data = data;
69-
});
70-
});
71-
}
72-
73-
@override
74-
void dispose() {
75-
super.dispose();
76-
_subscription?.cancel();
77-
}
78-
7946
@override
8047
Widget build(BuildContext context) {
81-
return ListView(
82-
padding: const EdgeInsets.symmetric(vertical: 8.0),
83-
children: _data.map((list) {
84-
return ListItemWidget(list: list);
85-
}).toList(),
48+
return GuardBySync(
49+
child: StreamBuilder(
50+
stream: TodoList.watchListsWithStats(),
51+
builder: (context, snapshot) {
52+
if (snapshot.data case final todoLists?) {
53+
return ListView(
54+
padding: const EdgeInsets.symmetric(vertical: 8.0),
55+
children: todoLists.map((list) {
56+
return ListItemWidget(list: list);
57+
}).toList(),
58+
);
59+
} else {
60+
return const CircularProgressIndicator();
61+
}
62+
},
63+
),
8664
);
8765
}
8866
}

demos/django-todolist/macos/Podfile.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ PODS:
2424
- sqlite3_flutter_libs (0.0.1):
2525
- Flutter
2626
- FlutterMacOS
27-
- sqlite3 (~> 3.49.1)
27+
- sqlite3 (~> 3.49.0)
2828
- sqlite3/dbstatvtab
2929
- sqlite3/fts5
3030
- sqlite3/perf-threadsafe
@@ -61,7 +61,7 @@ SPEC CHECKSUMS:
6161
powersync_flutter_libs: 011c1704766d154faf2373bb9c973d26910d322b
6262
shared_preferences_foundation: 9e1978ff2562383bd5676f64ec4e9aa8fa06a6f7
6363
sqlite3: fc1400008a9b3525f5914ed715a5d1af0b8f4983
64-
sqlite3_flutter_libs: f8fc13346870e73fe35ebf6dbb997fbcd156b241
64+
sqlite3_flutter_libs: 3c323550ef3b928bc0aa9513c841e45a7d242832
6565

6666
PODFILE CHECKSUM: 236401fc2c932af29a9fcf0e97baeeb2d750d367
6767

demos/django-todolist/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
ignoresPersistentStateOnLaunch = "NO"
6060
debugDocumentVersioning = "YES"
6161
debugServiceExtension = "internal"
62+
enableGPUValidationMode = "1"
6263
allowLocationSimulation = "YES">
6364
<BuildableProductRunnable
6465
runnableDebuggingMode = "0">

0 commit comments

Comments
 (0)