Fix config_store

This commit is contained in:
shilangyu 2021-04-11 00:55:04 +02:00
parent d77856f167
commit 236301275d
5 changed files with 38 additions and 34 deletions

View File

@ -28,6 +28,6 @@
},
"L10n string": {
"prefix": "l10n",
"body": ["L10n.of(context).$0"]
"body": ["L10n.of(context)!.$0"]
}
}

View File

@ -6,8 +6,8 @@ export 'l10n_api.dart';
export 'l10n_from_string.dart';
abstract class LocaleSerde {
static Locale? fromJson(String? json) {
if (json == null) return null;
static Locale fromJson(String? json) {
if (json == null) return const Locale('en');
final lang = json.split('-');

View File

@ -8,20 +8,19 @@ part of 'accounts_store.dart';
AccountsStore _$AccountsStoreFromJson(Map<String, dynamic> json) {
return AccountsStore()
..tokens = (json['tokens'] as Map<String, dynamic>)?.map(
..tokens = (json['tokens'] as Map<String, dynamic>?)?.map(
(k, e) => MapEntry(
k,
(e as Map<String, dynamic>)?.map(
(k, e) =>
MapEntry(k, e == null ? null : Jwt.fromJson(e as String)),
(e as Map<String, dynamic>).map(
(k, e) => MapEntry(k, Jwt.fromJson(e as String)),
)),
) ??
{'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),
) ??
{}
..defaultAccount = json['defaultAccount'] as String;
..defaultAccount = json['defaultAccount'] as String?;
}
Map<String, dynamic> _$AccountsStoreToJson(AccountsStore instance) =>

View File

@ -33,11 +33,11 @@ class ConfigStore extends ChangeNotifier {
save();
}
Locale? _locale;
// default value is set in the `load` method because json_serializable does
// not accept non-literals as constant values
late Locale _locale;
// default value is set in the `LocaleSerde.fromJson` method because json_serializable does
// not accept non-literals as defaultValue
@JsonKey(fromJson: LocaleSerde.fromJson, toJson: LocaleSerde.toJson)
Locale get locale => _locale ?? const Locale('en');
Locale get locale => _locale;
set locale(Locale locale) {
_locale = locale;
notifyListeners();

View File

@ -10,8 +10,8 @@ ConfigStore _$ConfigStoreFromJson(Map<String, dynamic> json) {
return ConfigStore()
..theme = _$enumDecodeNullable(_$ThemeModeEnumMap, json['theme']) ??
ThemeMode.system
..amoledDarkMode = json['amoledDarkMode'] as bool ?? false
..locale = LocaleSerde.fromJson(json['locale'] as String);
..amoledDarkMode = json['amoledDarkMode'] as bool? ?? false
..locale = LocaleSerde.fromJson(json['locale'] as String?);
}
Map<String, dynamic> _$ConfigStoreToJson(ConfigStore instance) =>
@ -21,36 +21,41 @@ Map<String, dynamic> _$ConfigStoreToJson(ConfigStore instance) =>
'locale': LocaleSerde.toJson(instance.locale),
};
T _$enumDecode<T>(
Map<T, dynamic> enumValues,
dynamic source, {
T unknownValue,
K _$enumDecode<K, V>(
Map<K, V> enumValues,
Object? source, {
K? unknownValue,
}) {
if (source == null) {
throw ArgumentError('A value must be provided. Supported values: '
'${enumValues.values.join(', ')}');
throw ArgumentError(
'A value must be provided. Supported values: '
'${enumValues.values.join(', ')}',
);
}
final value = enumValues.entries
.singleWhere((e) => e.value == source, orElse: () => null)
?.key;
if (value == null && unknownValue == null) {
throw ArgumentError('`$source` is not one of the supported values: '
'${enumValues.values.join(', ')}');
}
return value ?? unknownValue;
return enumValues.entries.singleWhere(
(e) => e.value == source,
orElse: () {
if (unknownValue == null) {
throw ArgumentError(
'`$source` is not one of the supported values: '
'${enumValues.values.join(', ')}',
);
}
return MapEntry(unknownValue, enumValues.values.first);
},
).key;
}
T _$enumDecodeNullable<T>(
Map<T, dynamic> enumValues,
K? _$enumDecodeNullable<K, V>(
Map<K, V> enumValues,
dynamic source, {
T unknownValue,
K? unknownValue,
}) {
if (source == null) {
return null;
}
return _$enumDecode<T>(enumValues, source, unknownValue: unknownValue);
return _$enumDecode<K, V>(enumValues, source, unknownValue: unknownValue);
}
const _$ThemeModeEnumMap = {