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" - "**/*.g.dart"
- "**/*.freezed.dart" - "**/*.freezed.dart"
- "build/**/intermediates/**/AndroidManifest.xml" - "build/**/intermediates/**/AndroidManifest.xml"
errors:
invalid_annotation_target: ignore # see https://github.com/rrousselGit/freezed/issues/488
plugins: plugins:
- custom_lint - custom_lint

View File

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

View File

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

View File

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

View File

@ -15,9 +15,18 @@ _$KeyCustomizationImpl _$$KeyCustomizationImplFromJson(
); );
Map<String, dynamic> _$$KeyCustomizationImplToJson( Map<String, dynamic> _$$KeyCustomizationImplToJson(
_$KeyCustomizationImpl instance) => _$KeyCustomizationImpl instance) {
<String, dynamic>{ final val = <String, dynamic>{
'serial': instance.serial, 'serial': instance.serial,
'name': instance.name, };
'color': const _ColorConverter().toJson(instance.color),
}; 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, textInputAction: TextInputAction.done,
onChanged: (value) { onChanged: (value) {
setState(() { setState(() {
_customName = value.trim(); final trimmed = value.trim();
_customName = trimmed.isEmpty ? null : trimmed;
}); });
}, },
onFieldSubmitted: (_) {}, onFieldSubmitted: (_) {},