Skip to content

Commit 4c24182

Browse files
committed
Updated readme to allow an local-only setup. Improved schema switching comment.
1 parent 04ec125 commit 4c24182

File tree

7 files changed

+36
-20
lines changed

7 files changed

+36
-20
lines changed

demos/local-only-todolist/.gitignore

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,4 @@ app.*.map.json
4444
/android/app/release
4545

4646
# asdf
47-
.tool-versions
48-
49-
# secrets
50-
app_config.dart
47+
.tool-versions

demos/local-only-todolist/README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22

33
This demo app is an extension of the Flutter Todo List App that demonstrates how to use the PowerSync SDK for Flutter in an local only way. It allows the user to use the app offline without having to sign in. After signing in data that was kept locally is synced up. For a step-by-step guide, see [here](https://docs.powersync.com/integration-guides/supabase).
44

5-
# Running the app
5+
# Running the app with local-only
66

7+
Note that if Supabase and PowerSync credentials have not been been configured, a user will not be able to login or sign up. There is a hardcoded check in `main.dart` for this.
78
Ensure you have [melos](https://melos.invertase.dev/~melos-latest/getting-started) installed.
89

910
1. `cd demos/local-only-todolist`
1011
2. `melos prepare`
11-
3. `cp lib/app_config_template.dart lib/app_config.dart`
12-
4. Insert your Supabase and PowerSync project credentials into `lib/app_config.dart` (See instructions below)
13-
5. `flutter run`
12+
3. `flutter run`
13+
14+
# Setting up sync functionality
15+
16+
Insert your Supabase and PowerSync project credentials into `lib/app_config.dart` (See instructions below).
17+
A user should now be able to login or sign up, which enables the sync functionality.
1418

1519
# Set up Supabase Project
1620

demos/local-only-todolist/lib/app_config_template.dart renamed to demos/local-only-todolist/lib/app_config.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// Copy this template: `cp lib/app_config_template.dart lib/app_config.dart`
2-
// Edit lib/app_config.dart and enter your Supabase and PowerSync project details.
1+
// Enter your Supabase and PowerSync project details.
32
class AppConfig {
43
static const String supabaseUrl = 'https://foo.supabase.co';
54
static const String supabaseAnonKey = 'foo';

demos/local-only-todolist/lib/main.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:flutter/foundation.dart';
22
import 'package:flutter/material.dart';
33
import 'package:logging/logging.dart';
4+
import 'package:powersync_flutter_local_only_demo/app_config.dart';
45
import 'package:powersync_flutter_local_only_demo/models/schema.dart';
56
import 'package:powersync_flutter_local_only_demo/models/sync_mode.dart';
67

@@ -126,6 +127,10 @@ class MyHomePage extends StatelessWidget {
126127
));
127128
}
128129
},
130+
enabled: // disable login/signup if credentials aren't configured.
131+
!(AppConfig.supabaseUrl == "https://foo.supabase.co" ||
132+
AppConfig.powersyncUrl ==
133+
"https://foo.powersync.journeyapps.com"),
129134
),
130135
],
131136
),

demos/local-only-todolist/lib/models/schema.dart

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11
import 'package:powersync/powersync.dart';
22
import 'package:powersync_flutter_local_only_demo/models/sync_mode.dart';
33

4-
/// The schema contains two copies of each table - a local-only one, and
5-
/// a online/synced one. Depending on the 'online' flag, one of those gets
6-
/// the main 'lists' / 'todos' view name.
4+
/// This schema design supports an online/local-only workflow by managing data
5+
/// across two versions of each table: one for local/offline use and one for
6+
/// online/synced use. This approach simplifies the handling of data in different
7+
/// connectivity states.
78
///
89
/// For local only, the views become:
910
/// online_todos
1011
/// todos
1112
/// online_lists
1213
/// lists
14+
///
15+
/// - 'todos' and 'lists' refer to the local-only data.
16+
/// - 'online_todos' and 'online_lists' refer to the data that will be synced
17+
/// once online, making it clear that these are not currently synchronized.
18+
///
1319
/// For online, we have these views:
1420
/// todos
1521
/// local_todos
1622
/// lists
1723
/// local_lists
1824
///
25+
/// - 'todos' and 'lists' refer to the synced/online data.
26+
/// - local_todos' and 'local_lists' refer to the local-only data, allowing
27+
/// for temporary storage or operations before syncing.
28+
///
29+
/// For an offline-to-online transition [switchToOnlineSchema] copies data so that it ends up in the upload queue.
1930
2031
const todosTable = 'todos';
2132
const listsTable = 'lists';
@@ -69,7 +80,7 @@ Schema makeSchema({online = bool}) {
6980

7081
switchToOnlineSchema(PowerSyncDatabase db, String userId) async {
7182
await db.updateSchema(makeSchema(online: true));
72-
await setSyncMode(true);
83+
await setSyncEnabled(true);
7384

7485
await db.writeTransaction((tx) async {
7586
// Copy local data to the "online" views.

demos/local-only-todolist/lib/models/sync_mode.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ openSyncModeDatabase() async {
2828
await migrations.migrate(sqliteDb);
2929
}
3030

31-
Future<bool> getSyncMode() async {
31+
Future<bool> getSyncEnabled() async {
3232
var rows = await sqliteDb
3333
.getAll('SELECT sync_enabled from local_system where id = 1');
3434

3535
if (rows.isEmpty) {
36-
await setSyncMode(false);
36+
await setSyncEnabled(false);
3737
return false;
3838
}
3939

4040
return rows[0]['sync_enabled'] == 'TRUE';
4141
}
4242

43-
setSyncMode(bool enabled) async {
43+
setSyncEnabled(bool enabled) async {
4444
var enabledString = enabled ? "TRUE" : "FALSE";
4545
await sqliteDb.execute(
4646
'INSERT OR REPLACE INTO local_system(id, sync_enabled) VALUES (1, ?);',

demos/local-only-todolist/lib/powersync.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ Future<String> getDatabasePath() async {
157157
}
158158

159159
Future<void> openDatabase() async {
160-
var isSyncMode = await getSyncMode();
160+
var isSyncMode = await getSyncEnabled();
161161
db = PowerSyncDatabase(
162162
schema: makeSchema(online: isSyncMode),
163163
path: await getDatabasePath(),
@@ -177,7 +177,7 @@ Future<void> connectDatabase() async {
177177
log.severe("Can't connect database without being signed in");
178178
}
179179
SupabaseConnector? currentConnector;
180-
var isSyncMode = await getSyncMode();
180+
var isSyncMode = await getSyncEnabled();
181181

182182
if (!isSyncMode) {
183183
await switchToOnlineSchema(db, getUserId());
@@ -206,6 +206,6 @@ Future<void> logout() async {
206206
await db.disconnectAndClear();
207207

208208
// Resetting app so that no-sync mode works again
209-
await await setSyncMode(false);
209+
await await setSyncEnabled(false);
210210
await openDatabase();
211211
}

0 commit comments

Comments
 (0)