Skip to content

Commit 3a17ec4

Browse files
committed
db: wip
Signed-off-by: Zixuan James Li <[email protected]>
1 parent aee5f17 commit 3a17ec4

File tree

6 files changed

+998
-4
lines changed

6 files changed

+998
-4
lines changed

lib/model/database.dart

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,55 @@ class Accounts extends Table {
4141
];
4242
}
4343

44+
/// The visual theme of the app.
45+
///
46+
/// See [zulipThemeData] for how themes are determined.
47+
enum ThemeSetting {
48+
/// Corresponds to the default platform setting.
49+
none,
50+
51+
/// Corresponds to [Brightness.light].
52+
light,
53+
54+
/// Corresponds to [Brightness.dark].
55+
dark,
56+
}
57+
58+
/// What browser the user has set to use for opening links in messages.
59+
///
60+
/// See https://chat.zulip.org/#narrow/stream/48-mobile/topic/in-app.20browser
61+
/// for the reasoning behind these options.
62+
enum BrowserPreference {
63+
/// Use [UrlLaunchMode.externalApplication] on iOS,
64+
/// [UrlLaunchMode.platformDefault] on Android.
65+
none,
66+
67+
/// Use the in-app browser.
68+
embedded,
69+
70+
/// Use the user's default browser app.
71+
external,
72+
}
73+
74+
/// The table of the user's chosen settings independent of account, on this
75+
/// client.
76+
///
77+
/// These apply across all the user's accounts on this client (i.e. on this
78+
/// install of the app on this device).
79+
@DataClassName('GlobalSettingsData')
80+
class GlobalSettings extends Table {
81+
Column<String> get themeSetting => textEnum<ThemeSetting>()();
82+
83+
Column<String> get browserPreference => textEnum<BrowserPreference>()();
84+
}
85+
4486
class UriConverter extends TypeConverter<Uri, String> {
4587
const UriConverter();
4688
@override String toSql(Uri value) => value.toString();
4789
@override Uri fromSql(String fromDb) => Uri.parse(fromDb);
4890
}
4991

50-
@DriftDatabase(tables: [Accounts])
92+
@DriftDatabase(tables: [Accounts, GlobalSettings])
5193
class AppDatabase extends _$AppDatabase {
5294
AppDatabase(super.e);
5395

@@ -58,7 +100,7 @@ class AppDatabase extends _$AppDatabase {
58100
// * Write a migration in `onUpgrade` below.
59101
// * Write tests.
60102
@override
61-
int get schemaVersion => 2; // See note.
103+
int get schemaVersion => 3; // See note.
62104

63105
@override
64106
MigrationStrategy get migration {
@@ -84,6 +126,10 @@ class AppDatabase extends _$AppDatabase {
84126
if (from < 2 && 2 <= to) {
85127
await m.addColumn(accounts, accounts.ackedPushToken);
86128
}
129+
130+
if (from < 3 && 3 <= to) {
131+
await m.createTable(globalSettings);
132+
}
87133
// New migrations go here.
88134
}
89135
);
@@ -104,6 +150,20 @@ class AppDatabase extends _$AppDatabase {
104150
rethrow;
105151
}
106152
}
153+
154+
Future<GlobalSettingsData> ensureGlobalSettings() async {
155+
final settings = await select(globalSettings).getSingleOrNull();
156+
// TODO(db): Enforce the singleton constraint more robustly.
157+
if (settings != null) {
158+
return settings;
159+
}
160+
161+
await into(globalSettings).insert(GlobalSettingsCompanion.insert(
162+
themeSetting: ThemeSetting.none,
163+
browserPreference: BrowserPreference.none,
164+
));
165+
return select(globalSettings).getSingle();
166+
}
107167
}
108168

109169
class AccountAlreadyExistsException implements Exception {}

0 commit comments

Comments
 (0)