Fix config_store
This commit is contained in:
parent
d77856f167
commit
236301275d
|
@ -28,6 +28,6 @@
|
||||||
},
|
},
|
||||||
"L10n string": {
|
"L10n string": {
|
||||||
"prefix": "l10n",
|
"prefix": "l10n",
|
||||||
"body": ["L10n.of(context).$0"]
|
"body": ["L10n.of(context)!.$0"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,8 +6,8 @@ export 'l10n_api.dart';
|
||||||
export 'l10n_from_string.dart';
|
export 'l10n_from_string.dart';
|
||||||
|
|
||||||
abstract class LocaleSerde {
|
abstract class LocaleSerde {
|
||||||
static Locale? fromJson(String? json) {
|
static Locale fromJson(String? json) {
|
||||||
if (json == null) return null;
|
if (json == null) return const Locale('en');
|
||||||
|
|
||||||
final lang = json.split('-');
|
final lang = json.split('-');
|
||||||
|
|
||||||
|
|
|
@ -8,20 +8,19 @@ part of 'accounts_store.dart';
|
||||||
|
|
||||||
AccountsStore _$AccountsStoreFromJson(Map<String, dynamic> json) {
|
AccountsStore _$AccountsStoreFromJson(Map<String, dynamic> json) {
|
||||||
return AccountsStore()
|
return AccountsStore()
|
||||||
..tokens = (json['tokens'] as Map<String, dynamic>)?.map(
|
..tokens = (json['tokens'] as Map<String, dynamic>?)?.map(
|
||||||
(k, e) => MapEntry(
|
(k, e) => MapEntry(
|
||||||
k,
|
k,
|
||||||
(e as Map<String, dynamic>)?.map(
|
(e as Map<String, dynamic>).map(
|
||||||
(k, e) =>
|
(k, e) => MapEntry(k, Jwt.fromJson(e as String)),
|
||||||
MapEntry(k, e == null ? null : Jwt.fromJson(e as String)),
|
|
||||||
)),
|
)),
|
||||||
) ??
|
) ??
|
||||||
{'lemmy.ml': {}}
|
{'lemmy.ml': {}}
|
||||||
..defaultAccounts = (json['defaultAccounts'] as Map<String, dynamic>)?.map(
|
..defaultAccounts = (json['defaultAccounts'] as Map<String, dynamic>?)?.map(
|
||||||
(k, e) => MapEntry(k, e as String),
|
(k, e) => MapEntry(k, e as String),
|
||||||
) ??
|
) ??
|
||||||
{}
|
{}
|
||||||
..defaultAccount = json['defaultAccount'] as String;
|
..defaultAccount = json['defaultAccount'] as String?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, dynamic> _$AccountsStoreToJson(AccountsStore instance) =>
|
Map<String, dynamic> _$AccountsStoreToJson(AccountsStore instance) =>
|
||||||
|
|
|
@ -33,11 +33,11 @@ class ConfigStore extends ChangeNotifier {
|
||||||
save();
|
save();
|
||||||
}
|
}
|
||||||
|
|
||||||
Locale? _locale;
|
late Locale _locale;
|
||||||
// default value is set in the `load` method because json_serializable does
|
// default value is set in the `LocaleSerde.fromJson` method because json_serializable does
|
||||||
// not accept non-literals as constant values
|
// not accept non-literals as defaultValue
|
||||||
@JsonKey(fromJson: LocaleSerde.fromJson, toJson: LocaleSerde.toJson)
|
@JsonKey(fromJson: LocaleSerde.fromJson, toJson: LocaleSerde.toJson)
|
||||||
Locale get locale => _locale ?? const Locale('en');
|
Locale get locale => _locale;
|
||||||
set locale(Locale locale) {
|
set locale(Locale locale) {
|
||||||
_locale = locale;
|
_locale = locale;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
|
|
|
@ -10,8 +10,8 @@ ConfigStore _$ConfigStoreFromJson(Map<String, dynamic> json) {
|
||||||
return ConfigStore()
|
return ConfigStore()
|
||||||
..theme = _$enumDecodeNullable(_$ThemeModeEnumMap, json['theme']) ??
|
..theme = _$enumDecodeNullable(_$ThemeModeEnumMap, json['theme']) ??
|
||||||
ThemeMode.system
|
ThemeMode.system
|
||||||
..amoledDarkMode = json['amoledDarkMode'] as bool ?? false
|
..amoledDarkMode = json['amoledDarkMode'] as bool? ?? false
|
||||||
..locale = LocaleSerde.fromJson(json['locale'] as String);
|
..locale = LocaleSerde.fromJson(json['locale'] as String?);
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, dynamic> _$ConfigStoreToJson(ConfigStore instance) =>
|
Map<String, dynamic> _$ConfigStoreToJson(ConfigStore instance) =>
|
||||||
|
@ -21,36 +21,41 @@ Map<String, dynamic> _$ConfigStoreToJson(ConfigStore instance) =>
|
||||||
'locale': LocaleSerde.toJson(instance.locale),
|
'locale': LocaleSerde.toJson(instance.locale),
|
||||||
};
|
};
|
||||||
|
|
||||||
T _$enumDecode<T>(
|
K _$enumDecode<K, V>(
|
||||||
Map<T, dynamic> enumValues,
|
Map<K, V> enumValues,
|
||||||
dynamic source, {
|
Object? source, {
|
||||||
T unknownValue,
|
K? unknownValue,
|
||||||
}) {
|
}) {
|
||||||
if (source == null) {
|
if (source == null) {
|
||||||
throw ArgumentError('A value must be provided. Supported values: '
|
throw ArgumentError(
|
||||||
'${enumValues.values.join(', ')}');
|
'A value must be provided. Supported values: '
|
||||||
|
'${enumValues.values.join(', ')}',
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
final value = enumValues.entries
|
return enumValues.entries.singleWhere(
|
||||||
.singleWhere((e) => e.value == source, orElse: () => null)
|
(e) => e.value == source,
|
||||||
?.key;
|
orElse: () {
|
||||||
|
if (unknownValue == null) {
|
||||||
if (value == null && unknownValue == null) {
|
throw ArgumentError(
|
||||||
throw ArgumentError('`$source` is not one of the supported values: '
|
'`$source` is not one of the supported values: '
|
||||||
'${enumValues.values.join(', ')}');
|
'${enumValues.values.join(', ')}',
|
||||||
}
|
);
|
||||||
return value ?? unknownValue;
|
}
|
||||||
|
return MapEntry(unknownValue, enumValues.values.first);
|
||||||
|
},
|
||||||
|
).key;
|
||||||
}
|
}
|
||||||
|
|
||||||
T _$enumDecodeNullable<T>(
|
K? _$enumDecodeNullable<K, V>(
|
||||||
Map<T, dynamic> enumValues,
|
Map<K, V> enumValues,
|
||||||
dynamic source, {
|
dynamic source, {
|
||||||
T unknownValue,
|
K? unknownValue,
|
||||||
}) {
|
}) {
|
||||||
if (source == null) {
|
if (source == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return _$enumDecode<T>(enumValues, source, unknownValue: unknownValue);
|
return _$enumDecode<K, V>(enumValues, source, unknownValue: unknownValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
const _$ThemeModeEnumMap = {
|
const _$ThemeModeEnumMap = {
|
||||||
|
|
Loading…
Reference in New Issue