Skip to content

Commit 94db13c

Browse files
committed
db: Store language global setting in database
An alternative to implementing _fromLanguageTag is using the LocaleParser from package:intl/locale.dart. In this case though, the values to parse always come from `toLanguageTag`, so we can utilize that knowledge to get away with a more simple implementation, that should also be handy upstream.
1 parent bddeeed commit 94db13c

File tree

10 files changed

+1205
-7
lines changed

10 files changed

+1205
-7
lines changed

lib/model/database.dart

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:ui';
2+
13
import 'package:drift/drift.dart';
24
import 'package:drift/internal/versioned_schema.dart';
35
import 'package:drift/remote.dart';
@@ -24,13 +26,66 @@ class GlobalSettings extends Table {
2426
Column<String> get browserPreference => textEnum<BrowserPreference>()
2527
.nullable()();
2628

29+
Column<String> get language => text().map(const LocaleConverter()).nullable()();
30+
2731
// If adding a new column to this table, consider whether [BoolGlobalSettings]
2832
// can do the job instead (by adding a value to the [BoolGlobalSetting] enum).
2933
// That way is more convenient, when it works, because
3034
// it avoids a migration and therefore several added copies of our schema
3135
// in the Drift generated files.
3236
}
3337

38+
class LocaleConverter extends TypeConverter<Locale, String> {
39+
const LocaleConverter();
40+
41+
/// Parse a Unicode BCP 47 Language Identifier into [Locale].
42+
///
43+
/// This supports parsing a subset of syntactically valid Unicode Language
44+
/// Identifiers, including values [Locale.toLanguageTag] might return.
45+
///
46+
/// Like [Locale], neither
47+
/// [variant subtags](https://www.unicode.org/reports/tr35/#unicode_variant_subtag).
48+
/// nor [extensions](https://www.unicode.org/reports/tr35/#extensions)
49+
/// are supported. It throws when it fails to convert [languageTag] into a
50+
/// [Locale].
51+
///
52+
/// See also:
53+
/// * https://www.unicode.org/reports/tr35/#Unicode_language_identifier;
54+
/// the method implements a subset of this EBNF grammar.
55+
// TODO(upstream): support Locale.fromLanguageTag:
56+
// https://github.com/flutter/flutter/issues/143491
57+
Locale _fromLanguageTag(String languageTag) {
58+
final subtags = languageTag.replaceAll('_', '-').split('-');
59+
60+
return switch (subtags) {
61+
[final language, final script, final region] =>
62+
Locale.fromSubtags(
63+
languageCode: language, scriptCode: script, countryCode: region),
64+
65+
[final language, final script] when script.length == 4 =>
66+
Locale.fromSubtags(languageCode: language, scriptCode: script),
67+
68+
[final language, final region] =>
69+
Locale(language, region),
70+
71+
[final language] =>
72+
Locale(language),
73+
74+
_ => throw ArgumentError.value(languageTag, 'languageTag'),
75+
};
76+
}
77+
78+
@override
79+
Locale fromSql(String fromDb) {
80+
return _fromLanguageTag(fromDb);
81+
}
82+
83+
@override
84+
String toSql(Locale value) {
85+
return value.toLanguageTag();
86+
}
87+
}
88+
3489
/// The table of the user's bool-valued, account-independent settings.
3590
///
3691
/// These apply across all the user's accounts on this client
@@ -119,7 +174,7 @@ class AppDatabase extends _$AppDatabase {
119174
// information on using the build_runner.
120175
// * Write a migration in `_migrationSteps` below.
121176
// * Write tests.
122-
static const int latestSchemaVersion = 6; // See note.
177+
static const int latestSchemaVersion = 7; // See note.
123178

124179
@override
125180
int get schemaVersion => latestSchemaVersion;
@@ -174,6 +229,9 @@ class AppDatabase extends _$AppDatabase {
174229
from5To6: (m, schema) async {
175230
await m.createTable(schema.boolGlobalSettings);
176231
},
232+
from6To7: (m, schema) async {
233+
await m.addColumn(schema.globalSettings, schema.globalSettings.language);
234+
}
177235
);
178236

179237
Future<void> _createLatestSchema(Migrator m) async {

lib/model/database.g.dart

Lines changed: 82 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)