From b060725ffebdf97f3379fa603da8d614a7a4e0d5 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Fri, 21 Feb 2025 17:31:25 +0100 Subject: [PATCH] Adopt priorities in example app --- demos/supabase-todolist/README.md | 19 +++++++++++++++++++ .../lib/widgets/lists_page.dart | 5 ++++- .../lib/widgets/todo_list_page.dart | 19 ++++++++++++++----- .../xcshareddata/xcschemes/Runner.xcscheme | 1 + demos/supabase-todolist/pubspec.lock | 8 ++++---- 5 files changed, 42 insertions(+), 10 deletions(-) diff --git a/demos/supabase-todolist/README.md b/demos/supabase-todolist/README.md index 542aa14d..4845b3e1 100644 --- a/demos/supabase-todolist/README.md +++ b/demos/supabase-todolist/README.md @@ -29,6 +29,25 @@ Create a new PowerSync instance, connecting to the database of the Supabase proj Then deploy the following sync rules: +```yaml +bucket_definitions: + user_lists: + priority: 1 + parameters: select id as list_id from lists where owner_id = request.user_id() + data: + - select * from lists where id = bucket.list_id + + user_todos: + parameters: select id as list_id from lists where owner_id = request.user_id() + data: + - select * from todos where list_id = bucket.list_id +``` + +The rules synchronize list with a higher priority the items within the list. This can be +useful to keep the list overview page reactive during a large sync cycle affecting many +rows in the `user_todos` bucket. The two buckets can also be unified into a single one if +priorities are not important (the app will work without changes): + ```yaml bucket_definitions: user_lists: diff --git a/demos/supabase-todolist/lib/widgets/lists_page.dart b/demos/supabase-todolist/lib/widgets/lists_page.dart index 142d9e9f..5b237b4e 100644 --- a/demos/supabase-todolist/lib/widgets/lists_page.dart +++ b/demos/supabase-todolist/lib/widgets/lists_page.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'package:flutter/material.dart'; +import 'package:powersync/powersync.dart'; import './list_item.dart'; import './list_item_dialog.dart'; @@ -51,6 +52,8 @@ class ListsWidget extends StatefulWidget { } class _ListsWidgetState extends State { + static final _listsPriority = BucketPriority(1); + List _data = []; bool hasSynced = false; StreamSubscription? _subscription; @@ -75,7 +78,7 @@ class _ListsWidgetState extends State { return; } setState(() { - hasSynced = status.hasSynced ?? false; + hasSynced = status.statusForPriority(_listsPriority).hasSynced ?? false; }); }); } diff --git a/demos/supabase-todolist/lib/widgets/todo_list_page.dart b/demos/supabase-todolist/lib/widgets/todo_list_page.dart index e36e1867..70dde161 100644 --- a/demos/supabase-todolist/lib/widgets/todo_list_page.dart +++ b/demos/supabase-todolist/lib/widgets/todo_list_page.dart @@ -79,11 +79,20 @@ class TodoListWidgetState extends State { @override Widget build(BuildContext context) { - return ListView( - padding: const EdgeInsets.symmetric(vertical: 8.0), - children: _data.map((todo) { - return TodoItemWidget(todo: todo); - }).toList(), + return StreamBuilder( + stream: TodoList.watchSyncStatus().map((e) => e.hasSynced), + builder: (context, snapshot) { + if (snapshot.data ?? false) { + return const Text('Busy with sync'); + } + + return ListView( + padding: const EdgeInsets.symmetric(vertical: 8.0), + children: _data.map((todo) { + return TodoItemWidget(todo: todo); + }).toList(), + ); + }, ); } } diff --git a/demos/supabase-todolist/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme b/demos/supabase-todolist/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme index 992a778b..943aed19 100644 --- a/demos/supabase-todolist/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme +++ b/demos/supabase-todolist/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme @@ -59,6 +59,7 @@ ignoresPersistentStateOnLaunch = "NO" debugDocumentVersioning = "YES" debugServiceExtension = "internal" + enableGPUValidationMode = "1" allowLocationSimulation = "YES"> diff --git a/demos/supabase-todolist/pubspec.lock b/demos/supabase-todolist/pubspec.lock index c6d85781..969bb7bd 100644 --- a/demos/supabase-todolist/pubspec.lock +++ b/demos/supabase-todolist/pubspec.lock @@ -478,28 +478,28 @@ packages: path: "../../packages/powersync" relative: true source: path - version: "1.11.2" + version: "1.11.3" powersync_attachments_helper: dependency: "direct main" description: path: "../../packages/powersync_attachments_helper" relative: true source: path - version: "0.6.18" + version: "0.6.18+1" powersync_core: dependency: "direct overridden" description: path: "../../packages/powersync_core" relative: true source: path - version: "1.1.2" + version: "1.1.3" powersync_flutter_libs: dependency: "direct overridden" description: path: "../../packages/powersync_flutter_libs" relative: true source: path - version: "0.4.4" + version: "0.4.5" pub_semver: dependency: transitive description: