freeze KeyCustomization

This commit is contained in:
Adam Velebil 2024-01-24 10:57:23 +01:00
parent af66e9201a
commit 15212af086
No known key found for this signature in database
GPG Key ID: C9B1E4A3CBBD2E10
7 changed files with 262 additions and 49 deletions

View File

@ -28,13 +28,13 @@ const _prefKeyCustomizations = 'KEY_CUSTOMIZATIONS';
class KeyCustomizationManager { class KeyCustomizationManager {
final SharedPreferences _prefs; final SharedPreferences _prefs;
final Map<String, dynamic> _customizations; final Map<String, KeyCustomization> _customizations;
KeyCustomizationManager(this._prefs) KeyCustomizationManager(this._prefs)
: _customizations = : _customizations =
readCustomizations(_prefs.getString(_prefKeyCustomizations)); readCustomizations(_prefs.getString(_prefKeyCustomizations));
static Map<String, dynamic> readCustomizations(String? pref) { static Map<String, KeyCustomization> readCustomizations(String? pref) {
if (pref == null) { if (pref == null) {
return {}; return {};
} }
@ -48,25 +48,14 @@ class KeyCustomizationManager {
KeyCustomization? get(String? serial) { KeyCustomization? get(String? serial) {
_log.debug('Getting key customization for $serial'); _log.debug('Getting key customization for $serial');
return _customizations[serial];
if (serial == null || serial.isEmpty) {
return null;
}
if (_customizations.containsKey(serial)) {
return KeyCustomization(serial, _customizations[serial]);
}
return null;
} }
void set({required String serial, String? customName, Color? customColor}) { void set({required String serial, String? customName, Color? customColor}) {
final properties = <String, String?>{ _log.debug(
'display_color': customColor?.value.toRadixString(16), 'Setting key customization for $serial: $customName, $customColor');
'display_name': customName?.isNotEmpty == true ? customName : null _customizations[serial] = KeyCustomization(
}; serial: serial, customName: customName, customColor: customColor);
_log.debug('Setting key customization for $serial: $properties');
_customizations[serial] = properties;
} }
Future<void> write() async { Future<void> write() async {

View File

@ -14,33 +14,32 @@
* limitations under the License. * limitations under the License.
*/ */
import 'dart:convert';
import 'dart:ui'; import 'dart:ui';
class KeyCustomization { import 'package:freezed_annotation/freezed_annotation.dart';
final String serialNumber;
final Map<String, dynamic> _properties;
const KeyCustomization(this.serialNumber, this._properties); part 'models.freezed.dart';
String? getName() => _properties['display_name'] as String?; part 'models.g.dart';
Color? getColor() { @freezed
var customColor = _properties['display_color'] as String?; class KeyCustomization with _$KeyCustomization {
if (customColor == null) { factory KeyCustomization({
return null; required String serial,
} String? customName,
@_ColorConverter() Color? customColor,
}) = _KeyCustomization;
var intValue = int.tryParse(customColor, radix: 16); factory KeyCustomization.fromJson(Map<String, dynamic> json) =>
_$KeyCustomizationFromJson(json);
if (intValue == null) { }
return null;
} class _ColorConverter implements JsonConverter<Color?, int?> {
return Color(intValue); const _ColorConverter();
}
@override
factory KeyCustomization.fromString(String serialNumber, String encodedJson) { Color? fromJson(int? json) => json != null ? Color(json) : null;
final data = json.decode(String.fromCharCodes(base64Decode(encodedJson)));
return KeyCustomization(serialNumber, data); @override
} int? toJson(Color? object) => object?.value;
} }

View File

@ -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>(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<String, dynamic> 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<String, dynamic> toJson() => throw _privateConstructorUsedError;
@JsonKey(ignore: true)
$KeyCustomizationCopyWith<KeyCustomization> 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<String, dynamic> 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<String, dynamic> 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<String, dynamic> 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;
}

View File

@ -0,0 +1,24 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'models.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
_$KeyCustomizationImpl _$$KeyCustomizationImplFromJson(
Map<String, dynamic> json) =>
_$KeyCustomizationImpl(
serial: json['serial'] as String,
customName: json['custom_name'] as String?,
customColor:
const _ColorConverter().fromJson(json['custom_color'] as int?),
);
Map<String, dynamic> _$$KeyCustomizationImplToJson(
_$KeyCustomizationImpl instance) =>
<String, dynamic>{
'serial': instance.serial,
'custom_name': instance.customName,
'custom_color': const _ColorConverter().toJson(instance.customColor),
};

View File

@ -52,8 +52,8 @@ class _KeyCustomizationDialogState
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_customName = widget.initialCustomization?.getName(); _customName = widget.initialCustomization?.customName;
_customColor = widget.initialCustomization?.getColor(); _customColor = widget.initialCustomization?.customColor;
} }
@override @override
@ -99,7 +99,7 @@ class _KeyCustomizationDialogState
onPressed: () async { onPressed: () async {
final manager = ref.read(keyCustomizationManagerProvider); final manager = ref.read(keyCustomizationManagerProvider);
manager.set( manager.set(
serial: widget.initialCustomization!.serialNumber, serial: widget.initialCustomization!.serial,
customName: _customName, customName: _customName,
customColor: _customColor); customColor: _customColor);
await manager.write(); await manager.write();

View File

@ -195,7 +195,7 @@ class ThemeNotifier extends Notifier<ThemeData> {
if (yubiKeyData != null) { if (yubiKeyData != null) {
final manager = ref.read(keyCustomizationManagerProvider); final manager = ref.read(keyCustomizationManagerProvider);
final customization = manager.get(yubiKeyData.info.serial?.toString()); final customization = manager.get(yubiKeyData.info.serial?.toString());
primaryColor = customization?.getColor() ?? color; primaryColor = customization?.customColor ?? color;
if (primaryColor != null) { if (primaryColor != null) {
// remember the last used color // remember the last used color
prefs.setString( prefs.setString(

View File

@ -387,7 +387,7 @@ class _DeviceRowState extends ConsumerState<_DeviceRow> {
Future<void> _showKeyCustomizationDialog(KeyCustomizationManager manager, Future<void> _showKeyCustomizationDialog(KeyCustomizationManager manager,
BuildContext context, DeviceNode? node, String serial) async { BuildContext context, DeviceNode? node, String serial) async {
final keyCustomization = final keyCustomization =
manager.get(serial) ?? KeyCustomization(serial, {}); manager.get(serial) ?? KeyCustomization(serial: serial);
await showBlurDialog( await showBlurDialog(
context: context, context: context,
@ -417,7 +417,7 @@ _DeviceRow _buildDeviceRow(
final keyCustomization = final keyCustomization =
ref.read(keyCustomizationManagerProvider).get(info?.serial?.toString()); ref.read(keyCustomizationManagerProvider).get(info?.serial?.toString());
String displayName = keyCustomization?.getName() ?? node.name; String displayName = keyCustomization?.customName ?? node.name;
return _DeviceRow( return _DeviceRow(
key: ValueKey(node.path.key), key: ValueKey(node.path.key),
@ -453,8 +453,8 @@ _DeviceRow _buildCurrentDeviceRow(
final keyCustomization = final keyCustomization =
ref.read(keyCustomizationManagerProvider).get(serialNumber); ref.read(keyCustomizationManagerProvider).get(serialNumber);
String displayName = keyCustomization?.getName() ?? title; String displayName = keyCustomization?.customName ?? title;
Color? displayColor = keyCustomization?.getColor(); Color? displayColor = keyCustomization?.customColor;
return _DeviceRow( return _DeviceRow(
key: keys.deviceInfoListTile, key: keys.deviceInfoListTile,