Skip to content

Commit 85cd9e4

Browse files
committed
settings: Make general int global settings
1 parent d969588 commit 85cd9e4

13 files changed

+1923
-9
lines changed

lib/model/database.dart

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,34 @@ class BoolGlobalSettings extends Table {
7373
Set<Column<Object>>? get primaryKey => {name};
7474
}
7575

76+
/// The table of the user's int-valued, account-independent settings.
77+
///
78+
/// These apply across all the user's accounts on this client
79+
/// (i.e. on this install of the app on this device).
80+
///
81+
/// Each row is a [IntGlobalSettingRow],
82+
/// referring to a possible setting from [IntGlobalSetting].
83+
/// For settings in [IntGlobalSetting] without a row in this table,
84+
/// the setting's value is `null`.
85+
@DataClassName('IntGlobalSettingRow')
86+
class IntGlobalSettings extends Table {
87+
/// The setting's name, a possible name from [IntGlobalSetting].
88+
///
89+
/// The table may have rows where [name] is not the name of any
90+
/// enum value in [IntGlobalSetting].
91+
/// This happens if the app has previously run at a future or modified
92+
/// version which had additional values in that enum,
93+
/// and the user set one of those additional settings.
94+
/// The app ignores any such unknown rows.
95+
TextColumn get name => text()();
96+
97+
/// The user's chosen value for the setting.
98+
IntColumn get value => integer()();
99+
100+
@override
101+
Set<Column<Object>>? get primaryKey => {name};
102+
}
103+
76104
/// The table of [Account] records in the app's database.
77105
class Accounts extends Table {
78106
/// The ID of this account in the app's local database.
@@ -116,7 +144,7 @@ class UriConverter extends TypeConverter<Uri, String> {
116144
@override Uri fromSql(String fromDb) => Uri.parse(fromDb);
117145
}
118146

119-
@DriftDatabase(tables: [GlobalSettings, BoolGlobalSettings, Accounts])
147+
@DriftDatabase(tables: [GlobalSettings, BoolGlobalSettings, IntGlobalSettings, Accounts])
120148
class AppDatabase extends _$AppDatabase {
121149
AppDatabase(super.e);
122150

@@ -129,7 +157,7 @@ class AppDatabase extends _$AppDatabase {
129157
// information on using the build_runner.
130158
// * Write a migration in `_migrationSteps` below.
131159
// * Write tests.
132-
static const int latestSchemaVersion = 9; // See note.
160+
static const int latestSchemaVersion = 10; // See note.
133161

134162
@override
135163
int get schemaVersion => latestSchemaVersion;
@@ -200,7 +228,10 @@ class AppDatabase extends _$AppDatabase {
200228
// assume there wasn't also the legacy app before that.
201229
await m.database.update(schema.globalSettings).write(
202230
RawValuesInsertable({'legacy_upgrade_state': Constant('noLegacy')}));
203-
}
231+
},
232+
from9To10: (m, schema) async {
233+
await m.createTable(schema.intGlobalSettings);
234+
},
204235
);
205236

206237
Future<void> _createLatestSchema(Migrator m) async {
@@ -256,6 +287,14 @@ class AppDatabase extends _$AppDatabase {
256287
return result;
257288
}
258289

290+
Future<Map<IntGlobalSetting, int>> getIntGlobalSettings() async {
291+
return {
292+
for (final row in await select(intGlobalSettings).get())
293+
if (IntGlobalSetting.byName(row.name) case final setting?)
294+
setting: row.value
295+
};
296+
}
297+
259298
Future<int> createAccount(AccountsCompanion values) async {
260299
try {
261300
return await into(accounts).insert(values);

0 commit comments

Comments
 (0)