improve KeyCustomization serialization

This commit is contained in:
Adam Velebil 2024-01-24 12:35:09 +01:00
parent 9c4e31ef46
commit 855915cd87
No known key found for this signature in database
GPG Key ID: C9B1E4A3CBBD2E10
6 changed files with 58 additions and 19 deletions

View File

@ -37,5 +37,7 @@ analyzer:
- "**/*.g.dart"
- "**/*.freezed.dart"
- "build/**/intermediates/**/AndroidManifest.xml"
errors:
invalid_annotation_target: ignore # see https://github.com/rrousselGit/freezed/issues/488
plugins:
- custom_lint

View File

@ -40,8 +40,14 @@ class KeyCustomizationManager {
}
try {
return json.decode(utf8.decode(pref.codeUnits));
final retval = <String, KeyCustomization>{};
for (var element in json.decode(pref)) {
final keyCustomization = KeyCustomization.fromJson(element);
retval[keyCustomization.serial] = keyCustomization;
}
return retval;
} catch (e) {
_log.error('Failure reading customizations: $e');
return {};
}
}
@ -53,12 +59,17 @@ class KeyCustomizationManager {
void set({required String serial, String? name, Color? color}) {
_log.debug('Setting key customization for $serial: $name, $color');
_customizations[serial] =
KeyCustomization(serial: serial, name: name, color: color);
if (name == null && color == null) {
// remove this customization
_customizations.removeWhere((key, value) => key == serial);
} else {
_customizations[serial] =
KeyCustomization(serial: serial, name: name, color: color);
}
}
Future<void> write() async {
await _prefs.setString(_prefKeyCustomizations,
String.fromCharCodes(utf8.encode(json.encode(_customizations))));
await _prefs.setString(
_prefKeyCustomizations, json.encode(_customizations.values.toList()));
}
}

View File

@ -26,8 +26,8 @@ part 'models.g.dart';
class KeyCustomization with _$KeyCustomization {
factory KeyCustomization({
required String serial,
String? name,
@_ColorConverter() Color? color,
@JsonKey(includeIfNull: false) String? name,
@JsonKey(includeIfNull: false) @_ColorConverter() Color? color,
}) = _KeyCustomization;
factory KeyCustomization.fromJson(Map<String, dynamic> json) =>

View File

@ -21,7 +21,9 @@ KeyCustomization _$KeyCustomizationFromJson(Map<String, dynamic> json) {
/// @nodoc
mixin _$KeyCustomization {
String get serial => throw _privateConstructorUsedError;
@JsonKey(includeIfNull: false)
String? get name => throw _privateConstructorUsedError;
@JsonKey(includeIfNull: false)
@_ColorConverter()
Color? get color => throw _privateConstructorUsedError;
@ -37,7 +39,10 @@ abstract class $KeyCustomizationCopyWith<$Res> {
KeyCustomization value, $Res Function(KeyCustomization) then) =
_$KeyCustomizationCopyWithImpl<$Res, KeyCustomization>;
@useResult
$Res call({String serial, String? name, @_ColorConverter() Color? color});
$Res call(
{String serial,
@JsonKey(includeIfNull: false) String? name,
@JsonKey(includeIfNull: false) @_ColorConverter() Color? color});
}
/// @nodoc
@ -82,7 +87,10 @@ abstract class _$$KeyCustomizationImplCopyWith<$Res>
__$$KeyCustomizationImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({String serial, String? name, @_ColorConverter() Color? color});
$Res call(
{String serial,
@JsonKey(includeIfNull: false) String? name,
@JsonKey(includeIfNull: false) @_ColorConverter() Color? color});
}
/// @nodoc
@ -121,7 +129,9 @@ class __$$KeyCustomizationImplCopyWithImpl<$Res>
@JsonSerializable()
class _$KeyCustomizationImpl implements _KeyCustomization {
_$KeyCustomizationImpl(
{required this.serial, this.name, @_ColorConverter() this.color});
{required this.serial,
@JsonKey(includeIfNull: false) this.name,
@JsonKey(includeIfNull: false) @_ColorConverter() this.color});
factory _$KeyCustomizationImpl.fromJson(Map<String, dynamic> json) =>
_$$KeyCustomizationImplFromJson(json);
@ -129,8 +139,10 @@ class _$KeyCustomizationImpl implements _KeyCustomization {
@override
final String serial;
@override
@JsonKey(includeIfNull: false)
final String? name;
@override
@JsonKey(includeIfNull: false)
@_ColorConverter()
final Color? color;
@ -171,8 +183,10 @@ class _$KeyCustomizationImpl implements _KeyCustomization {
abstract class _KeyCustomization implements KeyCustomization {
factory _KeyCustomization(
{required final String serial,
final String? name,
@_ColorConverter() final Color? color}) = _$KeyCustomizationImpl;
@JsonKey(includeIfNull: false) final String? name,
@JsonKey(includeIfNull: false)
@_ColorConverter()
final Color? color}) = _$KeyCustomizationImpl;
factory _KeyCustomization.fromJson(Map<String, dynamic> json) =
_$KeyCustomizationImpl.fromJson;
@ -180,8 +194,10 @@ abstract class _KeyCustomization implements KeyCustomization {
@override
String get serial;
@override
@JsonKey(includeIfNull: false)
String? get name;
@override
@JsonKey(includeIfNull: false)
@_ColorConverter()
Color? get color;
@override

View File

@ -15,9 +15,18 @@ _$KeyCustomizationImpl _$$KeyCustomizationImplFromJson(
);
Map<String, dynamic> _$$KeyCustomizationImplToJson(
_$KeyCustomizationImpl instance) =>
<String, dynamic>{
'serial': instance.serial,
'name': instance.name,
'color': const _ColorConverter().toJson(instance.color),
};
_$KeyCustomizationImpl instance) {
final val = <String, dynamic>{
'serial': instance.serial,
};
void writeNotNull(String key, dynamic value) {
if (value != null) {
val[key] = value;
}
}
writeNotNull('name', instance.name);
writeNotNull('color', const _ColorConverter().toJson(instance.color));
return val;
}

View File

@ -140,7 +140,8 @@ class _KeyCustomizationDialogState
textInputAction: TextInputAction.done,
onChanged: (value) {
setState(() {
_customName = value.trim();
final trimmed = value.trim();
_customName = trimmed.isEmpty ? null : trimmed;
});
},
onFieldSubmitted: (_) {},