diff --git a/analysis_options.yaml b/analysis_options.yaml index 6579df88..4cea670b 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -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 diff --git a/lib/app/key_customization/key_customization.dart b/lib/app/key_customization/key_customization.dart index 9139385b..263a6826 100644 --- a/lib/app/key_customization/key_customization.dart +++ b/lib/app/key_customization/key_customization.dart @@ -40,8 +40,14 @@ class KeyCustomizationManager { } try { - return json.decode(utf8.decode(pref.codeUnits)); + final retval = {}; + 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 write() async { - await _prefs.setString(_prefKeyCustomizations, - String.fromCharCodes(utf8.encode(json.encode(_customizations)))); + await _prefs.setString( + _prefKeyCustomizations, json.encode(_customizations.values.toList())); } } diff --git a/lib/app/key_customization/models.dart b/lib/app/key_customization/models.dart index 794f6d3d..3a933291 100644 --- a/lib/app/key_customization/models.dart +++ b/lib/app/key_customization/models.dart @@ -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 json) => diff --git a/lib/app/key_customization/models.freezed.dart b/lib/app/key_customization/models.freezed.dart index 351def15..17be3040 100644 --- a/lib/app/key_customization/models.freezed.dart +++ b/lib/app/key_customization/models.freezed.dart @@ -21,7 +21,9 @@ KeyCustomization _$KeyCustomizationFromJson(Map 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 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 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 diff --git a/lib/app/key_customization/models.g.dart b/lib/app/key_customization/models.g.dart index c5768daa..604473c6 100644 --- a/lib/app/key_customization/models.g.dart +++ b/lib/app/key_customization/models.g.dart @@ -15,9 +15,18 @@ _$KeyCustomizationImpl _$$KeyCustomizationImplFromJson( ); Map _$$KeyCustomizationImplToJson( - _$KeyCustomizationImpl instance) => - { - 'serial': instance.serial, - 'name': instance.name, - 'color': const _ColorConverter().toJson(instance.color), - }; + _$KeyCustomizationImpl instance) { + final val = { + '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; +} diff --git a/lib/app/key_customization/views/key_customization_dialog.dart b/lib/app/key_customization/views/key_customization_dialog.dart index 82d07395..88e33376 100644 --- a/lib/app/key_customization/views/key_customization_dialog.dart +++ b/lib/app/key_customization/views/key_customization_dialog.dart @@ -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: (_) {},