diff --git a/lib/app/key_customization/key_customization.dart b/lib/app/key_customization/key_customization.dart index 478d05a7..ab40b861 100644 --- a/lib/app/key_customization/key_customization.dart +++ b/lib/app/key_customization/key_customization.dart @@ -28,13 +28,13 @@ const _prefKeyCustomizations = 'KEY_CUSTOMIZATIONS'; class KeyCustomizationManager { final SharedPreferences _prefs; - final Map _customizations; + final Map _customizations; KeyCustomizationManager(this._prefs) : _customizations = readCustomizations(_prefs.getString(_prefKeyCustomizations)); - static Map readCustomizations(String? pref) { + static Map readCustomizations(String? pref) { if (pref == null) { return {}; } @@ -48,25 +48,14 @@ class KeyCustomizationManager { KeyCustomization? get(String? serial) { _log.debug('Getting key customization for $serial'); - - if (serial == null || serial.isEmpty) { - return null; - } - - if (_customizations.containsKey(serial)) { - return KeyCustomization(serial, _customizations[serial]); - } - - return null; + return _customizations[serial]; } void set({required String serial, String? customName, Color? customColor}) { - final properties = { - 'display_color': customColor?.value.toRadixString(16), - 'display_name': customName?.isNotEmpty == true ? customName : null - }; - _log.debug('Setting key customization for $serial: $properties'); - _customizations[serial] = properties; + _log.debug( + 'Setting key customization for $serial: $customName, $customColor'); + _customizations[serial] = KeyCustomization( + serial: serial, customName: customName, customColor: customColor); } Future write() async { diff --git a/lib/app/key_customization/models.dart b/lib/app/key_customization/models.dart index c1fd4906..1d2bf4bf 100644 --- a/lib/app/key_customization/models.dart +++ b/lib/app/key_customization/models.dart @@ -14,33 +14,32 @@ * limitations under the License. */ -import 'dart:convert'; import 'dart:ui'; -class KeyCustomization { - final String serialNumber; - final Map _properties; +import 'package:freezed_annotation/freezed_annotation.dart'; - const KeyCustomization(this.serialNumber, this._properties); +part 'models.freezed.dart'; - String? getName() => _properties['display_name'] as String?; +part 'models.g.dart'; - Color? getColor() { - var customColor = _properties['display_color'] as String?; - if (customColor == null) { - return null; - } +@freezed +class KeyCustomization with _$KeyCustomization { + factory KeyCustomization({ + required String serial, + String? customName, + @_ColorConverter() Color? customColor, + }) = _KeyCustomization; - var intValue = int.tryParse(customColor, radix: 16); - - if (intValue == null) { - return null; - } - return Color(intValue); - } - - factory KeyCustomization.fromString(String serialNumber, String encodedJson) { - final data = json.decode(String.fromCharCodes(base64Decode(encodedJson))); - return KeyCustomization(serialNumber, data); - } + factory KeyCustomization.fromJson(Map json) => + _$KeyCustomizationFromJson(json); +} + +class _ColorConverter implements JsonConverter { + const _ColorConverter(); + + @override + Color? fromJson(int? json) => json != null ? Color(json) : null; + + @override + int? toJson(Color? object) => object?.value; } diff --git a/lib/app/key_customization/models.freezed.dart b/lib/app/key_customization/models.freezed.dart new file mode 100644 index 00000000..ce2945da --- /dev/null +++ b/lib/app/key_customization/models.freezed.dart @@ -0,0 +1,201 @@ +// coverage:ignore-file +// GENERATED CODE - DO NOT MODIFY BY HAND +// ignore_for_file: type=lint +// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark + +part of 'models.dart'; + +// ************************************************************************** +// FreezedGenerator +// ************************************************************************** + +T _$identity(T value) => value; + +final _privateConstructorUsedError = UnsupportedError( + 'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#custom-getters-and-methods'); + +KeyCustomization _$KeyCustomizationFromJson(Map json) { + return _KeyCustomization.fromJson(json); +} + +/// @nodoc +mixin _$KeyCustomization { + String get serial => throw _privateConstructorUsedError; + String? get customName => throw _privateConstructorUsedError; + @_ColorConverter() + Color? get customColor => throw _privateConstructorUsedError; + + Map toJson() => throw _privateConstructorUsedError; + @JsonKey(ignore: true) + $KeyCustomizationCopyWith get copyWith => + throw _privateConstructorUsedError; +} + +/// @nodoc +abstract class $KeyCustomizationCopyWith<$Res> { + factory $KeyCustomizationCopyWith( + KeyCustomization value, $Res Function(KeyCustomization) then) = + _$KeyCustomizationCopyWithImpl<$Res, KeyCustomization>; + @useResult + $Res call( + {String serial, + String? customName, + @_ColorConverter() Color? customColor}); +} + +/// @nodoc +class _$KeyCustomizationCopyWithImpl<$Res, $Val extends KeyCustomization> + implements $KeyCustomizationCopyWith<$Res> { + _$KeyCustomizationCopyWithImpl(this._value, this._then); + + // ignore: unused_field + final $Val _value; + // ignore: unused_field + final $Res Function($Val) _then; + + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? serial = null, + Object? customName = freezed, + Object? customColor = freezed, + }) { + return _then(_value.copyWith( + serial: null == serial + ? _value.serial + : serial // ignore: cast_nullable_to_non_nullable + as String, + customName: freezed == customName + ? _value.customName + : customName // ignore: cast_nullable_to_non_nullable + as String?, + customColor: freezed == customColor + ? _value.customColor + : customColor // ignore: cast_nullable_to_non_nullable + as Color?, + ) as $Val); + } +} + +/// @nodoc +abstract class _$$KeyCustomizationImplCopyWith<$Res> + implements $KeyCustomizationCopyWith<$Res> { + factory _$$KeyCustomizationImplCopyWith(_$KeyCustomizationImpl value, + $Res Function(_$KeyCustomizationImpl) then) = + __$$KeyCustomizationImplCopyWithImpl<$Res>; + @override + @useResult + $Res call( + {String serial, + String? customName, + @_ColorConverter() Color? customColor}); +} + +/// @nodoc +class __$$KeyCustomizationImplCopyWithImpl<$Res> + extends _$KeyCustomizationCopyWithImpl<$Res, _$KeyCustomizationImpl> + implements _$$KeyCustomizationImplCopyWith<$Res> { + __$$KeyCustomizationImplCopyWithImpl(_$KeyCustomizationImpl _value, + $Res Function(_$KeyCustomizationImpl) _then) + : super(_value, _then); + + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? serial = null, + Object? customName = freezed, + Object? customColor = freezed, + }) { + return _then(_$KeyCustomizationImpl( + serial: null == serial + ? _value.serial + : serial // ignore: cast_nullable_to_non_nullable + as String, + customName: freezed == customName + ? _value.customName + : customName // ignore: cast_nullable_to_non_nullable + as String?, + customColor: freezed == customColor + ? _value.customColor + : customColor // ignore: cast_nullable_to_non_nullable + as Color?, + )); + } +} + +/// @nodoc +@JsonSerializable() +class _$KeyCustomizationImpl implements _KeyCustomization { + _$KeyCustomizationImpl( + {required this.serial, + this.customName, + @_ColorConverter() this.customColor}); + + factory _$KeyCustomizationImpl.fromJson(Map json) => + _$$KeyCustomizationImplFromJson(json); + + @override + final String serial; + @override + final String? customName; + @override + @_ColorConverter() + final Color? customColor; + + @override + String toString() { + return 'KeyCustomization(serial: $serial, customName: $customName, customColor: $customColor)'; + } + + @override + bool operator ==(Object other) { + return identical(this, other) || + (other.runtimeType == runtimeType && + other is _$KeyCustomizationImpl && + (identical(other.serial, serial) || other.serial == serial) && + (identical(other.customName, customName) || + other.customName == customName) && + (identical(other.customColor, customColor) || + other.customColor == customColor)); + } + + @JsonKey(ignore: true) + @override + int get hashCode => Object.hash(runtimeType, serial, customName, customColor); + + @JsonKey(ignore: true) + @override + @pragma('vm:prefer-inline') + _$$KeyCustomizationImplCopyWith<_$KeyCustomizationImpl> get copyWith => + __$$KeyCustomizationImplCopyWithImpl<_$KeyCustomizationImpl>( + this, _$identity); + + @override + Map toJson() { + return _$$KeyCustomizationImplToJson( + this, + ); + } +} + +abstract class _KeyCustomization implements KeyCustomization { + factory _KeyCustomization( + {required final String serial, + final String? customName, + @_ColorConverter() final Color? customColor}) = _$KeyCustomizationImpl; + + factory _KeyCustomization.fromJson(Map json) = + _$KeyCustomizationImpl.fromJson; + + @override + String get serial; + @override + String? get customName; + @override + @_ColorConverter() + Color? get customColor; + @override + @JsonKey(ignore: true) + _$$KeyCustomizationImplCopyWith<_$KeyCustomizationImpl> get copyWith => + throw _privateConstructorUsedError; +} diff --git a/lib/app/key_customization/models.g.dart b/lib/app/key_customization/models.g.dart new file mode 100644 index 00000000..a3d11a64 --- /dev/null +++ b/lib/app/key_customization/models.g.dart @@ -0,0 +1,24 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'models.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +_$KeyCustomizationImpl _$$KeyCustomizationImplFromJson( + Map json) => + _$KeyCustomizationImpl( + serial: json['serial'] as String, + customName: json['custom_name'] as String?, + customColor: + const _ColorConverter().fromJson(json['custom_color'] as int?), + ); + +Map _$$KeyCustomizationImplToJson( + _$KeyCustomizationImpl instance) => + { + 'serial': instance.serial, + 'custom_name': instance.customName, + 'custom_color': const _ColorConverter().toJson(instance.customColor), + }; diff --git a/lib/app/key_customization/views/key_customization_dialog.dart b/lib/app/key_customization/views/key_customization_dialog.dart index e7d8cdee..618869dd 100644 --- a/lib/app/key_customization/views/key_customization_dialog.dart +++ b/lib/app/key_customization/views/key_customization_dialog.dart @@ -52,8 +52,8 @@ class _KeyCustomizationDialogState @override void initState() { super.initState(); - _customName = widget.initialCustomization?.getName(); - _customColor = widget.initialCustomization?.getColor(); + _customName = widget.initialCustomization?.customName; + _customColor = widget.initialCustomization?.customColor; } @override @@ -99,7 +99,7 @@ class _KeyCustomizationDialogState onPressed: () async { final manager = ref.read(keyCustomizationManagerProvider); manager.set( - serial: widget.initialCustomization!.serialNumber, + serial: widget.initialCustomization!.serial, customName: _customName, customColor: _customColor); await manager.write(); diff --git a/lib/app/state.dart b/lib/app/state.dart index 14a0f258..6abc22c3 100755 --- a/lib/app/state.dart +++ b/lib/app/state.dart @@ -195,7 +195,7 @@ class ThemeNotifier extends Notifier { if (yubiKeyData != null) { final manager = ref.read(keyCustomizationManagerProvider); final customization = manager.get(yubiKeyData.info.serial?.toString()); - primaryColor = customization?.getColor() ?? color; + primaryColor = customization?.customColor ?? color; if (primaryColor != null) { // remember the last used color prefs.setString( diff --git a/lib/app/views/device_picker.dart b/lib/app/views/device_picker.dart index e829dfd6..8647cb2a 100644 --- a/lib/app/views/device_picker.dart +++ b/lib/app/views/device_picker.dart @@ -387,7 +387,7 @@ class _DeviceRowState extends ConsumerState<_DeviceRow> { Future _showKeyCustomizationDialog(KeyCustomizationManager manager, BuildContext context, DeviceNode? node, String serial) async { final keyCustomization = - manager.get(serial) ?? KeyCustomization(serial, {}); + manager.get(serial) ?? KeyCustomization(serial: serial); await showBlurDialog( context: context, @@ -417,7 +417,7 @@ _DeviceRow _buildDeviceRow( final keyCustomization = ref.read(keyCustomizationManagerProvider).get(info?.serial?.toString()); - String displayName = keyCustomization?.getName() ?? node.name; + String displayName = keyCustomization?.customName ?? node.name; return _DeviceRow( key: ValueKey(node.path.key), @@ -453,8 +453,8 @@ _DeviceRow _buildCurrentDeviceRow( final keyCustomization = ref.read(keyCustomizationManagerProvider).get(serialNumber); - String displayName = keyCustomization?.getName() ?? title; - Color? displayColor = keyCustomization?.getColor(); + String displayName = keyCustomization?.customName ?? title; + Color? displayColor = keyCustomization?.customColor; return _DeviceRow( key: keys.deviceInfoListTile,