|
1 | 1 | import 'package:flutter/material.dart';
|
| 2 | +import 'package:powersync/powersync.dart'; |
2 | 3 |
|
3 | 4 | import '../powersync.dart';
|
4 | 5 | import './status_app_bar.dart';
|
@@ -38,30 +39,82 @@ class TodoListPage extends StatelessWidget {
|
38 | 39 | }
|
39 | 40 | }
|
40 | 41 |
|
41 |
| -class TodoListWidget extends StatelessWidget { |
| 42 | +class TodoListWidget extends StatefulWidget { |
42 | 43 | final TodoList list;
|
43 | 44 |
|
44 | 45 | const TodoListWidget({super.key, required this.list});
|
45 | 46 |
|
| 47 | + @override |
| 48 | + State<TodoListWidget> createState() => _TodoListWidgetState(); |
| 49 | +} |
| 50 | + |
| 51 | +class _TodoListWidgetState extends State<TodoListWidget> { |
| 52 | + SyncStreamSubscription? _listSubscription; |
| 53 | + |
| 54 | + void _subscribe(String listId) { |
| 55 | + db |
| 56 | + .syncStream('todos', {'list': listId}) |
| 57 | + .subscribe(ttl: const Duration(hours: 1)) |
| 58 | + .then((sub) { |
| 59 | + if (mounted && widget.list.id == listId) { |
| 60 | + setState(() { |
| 61 | + _listSubscription = sub; |
| 62 | + }); |
| 63 | + } else { |
| 64 | + sub.unsubscribe(); |
| 65 | + } |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + @override |
| 70 | + void initState() { |
| 71 | + super.initState(); |
| 72 | + _subscribe(widget.list.id); |
| 73 | + } |
| 74 | + |
| 75 | + @override |
| 76 | + void didUpdateWidget(covariant TodoListWidget oldWidget) { |
| 77 | + super.didUpdateWidget(oldWidget); |
| 78 | + _subscribe(widget.list.id); |
| 79 | + } |
| 80 | + |
| 81 | + @override |
| 82 | + void dispose() { |
| 83 | + super.dispose(); |
| 84 | + _listSubscription?.unsubscribe(); |
| 85 | + } |
| 86 | + |
46 | 87 | @override
|
47 | 88 | Widget build(BuildContext context) {
|
48 | 89 | return StreamBuilder(
|
49 |
| - stream: TodoList.watchSyncStatus().map((e) => e.hasSynced), |
50 |
| - initialData: db.currentStatus.hasSynced, |
| 90 | + stream: db.statusStream, |
| 91 | + initialData: db.currentStatus, |
51 | 92 | builder: (context, snapshot) {
|
52 |
| - return StreamBuilder( |
53 |
| - stream: list.watchItems(), |
54 |
| - builder: (context, snapshot) { |
55 |
| - final items = snapshot.data ?? const []; |
56 |
| - |
57 |
| - return ListView( |
58 |
| - padding: const EdgeInsets.symmetric(vertical: 8.0), |
59 |
| - children: items.map((todo) { |
60 |
| - return TodoItemWidget(todo: todo); |
61 |
| - }).toList(), |
62 |
| - ); |
63 |
| - }, |
64 |
| - ); |
| 93 | + final hasSynced = switch (_listSubscription) { |
| 94 | + null => null, |
| 95 | + final sub => snapshot.requireData.statusFor(sub), |
| 96 | + } |
| 97 | + ?.subscription |
| 98 | + .hasSynced ?? |
| 99 | + false; |
| 100 | + |
| 101 | + if (!hasSynced) { |
| 102 | + return const CircularProgressIndicator(); |
| 103 | + } else { |
| 104 | + return StreamBuilder( |
| 105 | + stream: widget.list.watchItems(), |
| 106 | + builder: (context, snapshot) { |
| 107 | + final items = snapshot.data ?? const []; |
| 108 | + |
| 109 | + return ListView( |
| 110 | + padding: const EdgeInsets.symmetric(vertical: 8.0), |
| 111 | + children: items.map((todo) { |
| 112 | + return TodoItemWidget(todo: todo); |
| 113 | + }).toList(), |
| 114 | + ); |
| 115 | + }, |
| 116 | + ); |
| 117 | + } |
65 | 118 | },
|
66 | 119 | );
|
67 | 120 | }
|
|
0 commit comments