Skip to content

Commit 5cfd4ae

Browse files
committed
settings: Make general bool global settings, so as to add without migrations
This new table to read looks to add something like 5-8ms to the app's startup time. Not nothing; but not a big cost compared to the time we spend loading server data right afterward. Specifically, results on my Pixel 8 (compare to those in the previous commit): db load time 117.9ms total: 15.2ms init, 89.2ms settings, 5.1ms bool-settings, 8.3ms accounts db load time 90.9ms total: 1.3ms init, 78.5ms settings, 8.3ms bool-settings, 2.8ms accounts db load time 87.2ms total: 1.4ms init, 71.8ms settings, 5.9ms bool-settings, 8.1ms accounts db load time 85.7ms total: 1.2ms init, 72.8ms settings, 4.9ms bool-settings, 6.8ms accounts db load time 91.3ms total: 1.4ms init, 80.0ms settings, 7.5ms bool-settings, 2.5ms accounts db load time 83.8ms total: 1.1ms init, 70.0ms settings, 5.0ms bool-settings, 7.7ms accounts
1 parent 8f4155d commit 5cfd4ae

13 files changed

+1692
-11
lines changed

lib/model/database.dart

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,44 @@ class GlobalSettings extends Table {
2323

2424
Column<String> get browserPreference => textEnum<BrowserPreference>()
2525
.nullable()();
26+
27+
// If adding a new column to this table, consider whether [BoolGlobalSettings]
28+
// can do the job instead (by adding a value to the [BoolGlobalSetting] enum).
29+
// That way is more convenient, when it works, because
30+
// it avoids a migration and therefore several added copies of our schema
31+
// in the Drift generated files.
32+
}
33+
34+
/// The table of the user's bool-valued, account-independent settings.
35+
///
36+
/// These apply across all the user's accounts on this client
37+
/// (i.e. on this install of the app on this device).
38+
///
39+
/// Each row is a [BoolGlobalSettingRow],
40+
/// referring to a possible setting from [BoolGlobalSetting].
41+
/// For settings in [BoolGlobalSetting] without a row in this table,
42+
/// the setting's value is that of [BoolGlobalSetting.default_].
43+
@DataClassName('BoolGlobalSettingRow')
44+
class BoolGlobalSettings extends Table {
45+
/// The setting's name, a possible name from [BoolGlobalSetting].
46+
///
47+
/// The table may have rows where [name] is not the name of any
48+
/// enum value in [BoolGlobalSetting].
49+
/// This happens if the app has previously run at a future or modified
50+
/// version which had additional values in that enum,
51+
/// and the user set one of those additional settings.
52+
/// The app ignores any such unknown rows.
53+
Column<String> get name => text()();
54+
55+
/// The user's chosen value for the setting.
56+
///
57+
/// This is non-nullable; if the user wants to revert to
58+
/// following the app's default for the setting,
59+
/// that can be expressed by deleting the row.
60+
Column<bool> get value => boolean()();
61+
62+
@override
63+
Set<Column<Object>>? get primaryKey => {name};
2664
}
2765

2866
/// The table of [Account] records in the app's database.
@@ -68,7 +106,7 @@ class UriConverter extends TypeConverter<Uri, String> {
68106
@override Uri fromSql(String fromDb) => Uri.parse(fromDb);
69107
}
70108

71-
@DriftDatabase(tables: [GlobalSettings, Accounts])
109+
@DriftDatabase(tables: [GlobalSettings, BoolGlobalSettings, Accounts])
72110
class AppDatabase extends _$AppDatabase {
73111
AppDatabase(super.e);
74112

@@ -81,7 +119,7 @@ class AppDatabase extends _$AppDatabase {
81119
// information on using the build_runner.
82120
// * Write a migration in `_migrationSteps` below.
83121
// * Write tests.
84-
static const int latestSchemaVersion = 5; // See note.
122+
static const int latestSchemaVersion = 6; // See note.
85123

86124
@override
87125
int get schemaVersion => latestSchemaVersion;
@@ -133,6 +171,9 @@ class AppDatabase extends _$AppDatabase {
133171
RawValuesInsertable({}));
134172
}
135173
},
174+
from5To6: (m, schema) async {
175+
await m.createTable(schema.boolGlobalSettings);
176+
},
136177
);
137178

138179
Future<void> _createLatestSchema(Migrator m) async {
@@ -173,6 +214,17 @@ class AppDatabase extends _$AppDatabase {
173214
return await (select(globalSettings)..limit(1)).getSingle();
174215
}
175216

217+
Future<Map<BoolGlobalSetting, bool>> getBoolGlobalSettings() async {
218+
final result = <BoolGlobalSetting, bool>{};
219+
final rows = await select(boolGlobalSettings).get();
220+
for (final row in rows) {
221+
final setting = BoolGlobalSetting.byName(row.name);
222+
if (setting == null) continue;
223+
result[setting] = row.value;
224+
}
225+
return result;
226+
}
227+
176228
Future<int> createAccount(AccountsCompanion values) async {
177229
try {
178230
return await into(accounts).insert(values);

0 commit comments

Comments
 (0)