react to key change

This commit is contained in:
Adam Velebil 2024-01-11 16:56:27 +01:00
parent c7f2b651fd
commit 7ede30b641
No known key found for this signature in database
GPG Key ID: C9B1E4A3CBBD2E10
4 changed files with 57 additions and 36 deletions

View File

@ -135,6 +135,7 @@ class NfcTapActionNotifier extends StateNotifier<NfcTapAction> {
static const _prefNfcOpenApp = 'prefNfcOpenApp'; static const _prefNfcOpenApp = 'prefNfcOpenApp';
static const _prefNfcCopyOtp = 'prefNfcCopyOtp'; static const _prefNfcCopyOtp = 'prefNfcCopyOtp';
final SharedPreferences _prefs; final SharedPreferences _prefs;
NfcTapActionNotifier._(this._prefs, super._state); NfcTapActionNotifier._(this._prefs, super._state);
factory NfcTapActionNotifier(SharedPreferences prefs) { factory NfcTapActionNotifier(SharedPreferences prefs) {
@ -176,6 +177,7 @@ class NfcKbdLayoutNotifier extends StateNotifier<String> {
static const String _defaultClipKbdLayout = 'US'; static const String _defaultClipKbdLayout = 'US';
static const _prefClipKbdLayout = 'prefClipKbdLayout'; static const _prefClipKbdLayout = 'prefClipKbdLayout';
final SharedPreferences _prefs; final SharedPreferences _prefs;
NfcKbdLayoutNotifier(this._prefs) NfcKbdLayoutNotifier(this._prefs)
: super(_prefs.getString(_prefClipKbdLayout) ?? _defaultClipKbdLayout); : super(_prefs.getString(_prefClipKbdLayout) ?? _defaultClipKbdLayout);
@ -194,6 +196,7 @@ final androidNfcBypassTouchProvider =
class NfcBypassTouchNotifier extends StateNotifier<bool> { class NfcBypassTouchNotifier extends StateNotifier<bool> {
static const _prefNfcBypassTouch = 'prefNfcBypassTouch'; static const _prefNfcBypassTouch = 'prefNfcBypassTouch';
final SharedPreferences _prefs; final SharedPreferences _prefs;
NfcBypassTouchNotifier(this._prefs) NfcBypassTouchNotifier(this._prefs)
: super(_prefs.getBool(_prefNfcBypassTouch) ?? false); : super(_prefs.getBool(_prefNfcBypassTouch) ?? false);
@ -212,6 +215,7 @@ final androidNfcSilenceSoundsProvider =
class NfcSilenceSoundsNotifier extends StateNotifier<bool> { class NfcSilenceSoundsNotifier extends StateNotifier<bool> {
static const _prefNfcSilenceSounds = 'prefNfcSilenceSounds'; static const _prefNfcSilenceSounds = 'prefNfcSilenceSounds';
final SharedPreferences _prefs; final SharedPreferences _prefs;
NfcSilenceSoundsNotifier(this._prefs) NfcSilenceSoundsNotifier(this._prefs)
: super(_prefs.getBool(_prefNfcSilenceSounds) ?? false); : super(_prefs.getBool(_prefNfcSilenceSounds) ?? false);
@ -230,6 +234,7 @@ final androidUsbLaunchAppProvider =
class UsbLaunchAppNotifier extends StateNotifier<bool> { class UsbLaunchAppNotifier extends StateNotifier<bool> {
static const _prefUsbOpenApp = 'prefUsbOpenApp'; static const _prefUsbOpenApp = 'prefUsbOpenApp';
final SharedPreferences _prefs; final SharedPreferences _prefs;
UsbLaunchAppNotifier(this._prefs) UsbLaunchAppNotifier(this._prefs)
: super(_prefs.getBool(_prefUsbOpenApp) ?? false); : super(_prefs.getBool(_prefUsbOpenApp) ?? false);

View File

@ -27,6 +27,7 @@ import 'package:shared_preferences/shared_preferences.dart';
import '../core/state.dart'; import '../core/state.dart';
import '../theme.dart'; import '../theme.dart';
import 'features.dart' as features; import 'features.dart' as features;
import 'key_customization.dart';
import 'logging.dart'; import 'logging.dart';
import 'models.dart'; import 'models.dart';
@ -118,8 +119,12 @@ final l10nProvider = Provider<AppLocalizations>(
); );
final themeModeProvider = StateNotifierProvider<ThemeModeNotifier, ThemeMode>( final themeModeProvider = StateNotifierProvider<ThemeModeNotifier, ThemeMode>(
(ref) => ThemeModeNotifier( (ref) {
ref.watch(prefProvider), ref.read(supportedThemesProvider)), // initialize the keyCustomizationManager
ref.read(keyCustomizationManagerProvider);
return ThemeModeNotifier(
ref.watch(prefProvider), ref.read(supportedThemesProvider));
},
); );
class ThemeModeNotifier extends StateNotifier<ThemeMode> { class ThemeModeNotifier extends StateNotifier<ThemeMode> {
@ -142,37 +147,60 @@ class ThemeModeNotifier extends StateNotifier<ThemeMode> {
final primaryColorProvider = Provider<Color?>((ref) => null); final primaryColorProvider = Provider<Color?>((ref) => null);
final darkThemeProvider = StateNotifierProvider<ThemeNotifier, ThemeData>( final darkThemeProvider = NotifierProvider<ThemeNotifier, ThemeData>(
(ref) => ThemeNotifier(ref.watch(primaryColorProvider), ThemeMode.dark), () => ThemeNotifier(ThemeMode.dark),
); );
final lightThemeProvider = StateNotifierProvider<ThemeNotifier, ThemeData>( final lightThemeProvider = NotifierProvider<ThemeNotifier, ThemeData>(
(ref) => ThemeNotifier(ref.watch(primaryColorProvider), ThemeMode.light), () => ThemeNotifier(ThemeMode.light),
); );
class ThemeNotifier extends StateNotifier<ThemeData> { class ThemeNotifier extends Notifier<ThemeData> {
final ThemeMode _themeMode; final ThemeMode _themeMode;
ThemeNotifier(Color? systemPrimaryColor, this._themeMode) ThemeNotifier(this._themeMode);
: super(_get(systemPrimaryColor, _themeMode));
@override
ThemeData build() {
return _get(
_themeMode,
yubiKeyData: ref.watch(currentDeviceDataProvider).valueOrNull,
);
}
static ThemeData _getDefault(ThemeMode themeMode) => static ThemeData _getDefault(ThemeMode themeMode) =>
themeMode == ThemeMode.light ? AppTheme.lightTheme : AppTheme.darkTheme; themeMode == ThemeMode.light ? AppTheme.lightTheme : AppTheme.darkTheme;
static ThemeData _get(Color? primaryColor, ThemeMode themeMode) => ThemeData _get(ThemeMode themeMode, {YubiKeyData? yubiKeyData}) {
(primaryColor != null) Color? primaryColor;
? _getDefault(themeMode).copyWith( if (yubiKeyData != null) {
colorScheme: ColorScheme.fromSeed( final manager = ref.read(keyCustomizationManagerProvider);
brightness: themeMode == ThemeMode.dark
? Brightness.dark final customization = manager.get(yubiKeyData.info.serial?.toString());
: Brightness.light, String? displayColorCustomization =
seedColor: primaryColor) customization?.properties['display_color'];
.copyWith(primary: primaryColor))
: _getDefault(themeMode); if (displayColorCustomization != null) {
primaryColor = Color(int.parse(displayColorCustomization, radix: 16));
}
}
primaryColor ??= ref.read(primaryColorProvider);
return (primaryColor != null)
? _getDefault(themeMode).copyWith(
colorScheme: ColorScheme.fromSeed(
brightness: themeMode == ThemeMode.dark
? Brightness.dark
: Brightness.light,
seedColor: primaryColor)
.copyWith(primary: primaryColor))
: _getDefault(themeMode);
}
void setPrimaryColor(Color? primaryColor) { void setPrimaryColor(Color? primaryColor) {
_log.debug('Set primary color to $primaryColor'); _log.debug('Set primary color to $primaryColor');
state = _get(primaryColor, _themeMode); state = _get(_themeMode);
} }
} }

View File

@ -19,6 +19,8 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import '../../widgets/app_input_decoration.dart';
import '../../widgets/app_text_form_field.dart';
import '../../widgets/focus_utils.dart'; import '../../widgets/focus_utils.dart';
import '../../widgets/responsive_dialog.dart'; import '../../widgets/responsive_dialog.dart';
import '../key_customization.dart'; import '../key_customization.dart';
@ -110,11 +112,11 @@ class _CustomizePageState extends ConsumerState<CustomizePage> {
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
TextFormField( AppTextFormField(
//controller: displayNameController, //controller: displayNameController,
initialValue: _displayName, initialValue: _displayName,
maxLength: 20, maxLength: 20,
decoration: const InputDecoration( decoration: const AppInputDecoration(
border: OutlineInputBorder(), border: OutlineInputBorder(),
labelText: 'Nick name', labelText: 'Nick name',
helperText: '', // Prevents dialog resizing when disabled helperText: '', // Prevents dialog resizing when disabled

View File

@ -25,7 +25,6 @@ import 'package:logging/logging.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:window_manager/window_manager.dart'; import 'package:window_manager/window_manager.dart';
import '../app/key_customization.dart';
import '../app/logging.dart'; import '../app/logging.dart';
import '../app/models.dart'; import '../app/models.dart';
import '../app/state.dart'; import '../app/state.dart';
@ -216,18 +215,5 @@ class DesktopCurrentDeviceNotifier extends CurrentDeviceNotifier {
setCurrentDevice(DeviceNode? device) { setCurrentDevice(DeviceNode? device) {
state = device; state = device;
ref.read(prefProvider).setString(_lastDevice, device?.path.key ?? ''); ref.read(prefProvider).setString(_lastDevice, device?.path.key ?? '');
if (device != null &&
device is UsbYubiKeyNode &&
device.info?.serial != null) {
final manager = ref.read(keyCustomizationManagerProvider);
final customization = manager.get(device.info?.serial!.toString());
String? displayColorCustomization =
customization?.properties['display_color'];
Color? displayColor;
if (displayColorCustomization != null) {
displayColor = Color(int.parse(displayColorCustomization, radix: 16));
}
ref.watch(darkThemeProvider.notifier).setPrimaryColor(displayColor);
}
} }
} }