From eff800181c6edd2b0c7668adb45fcaca8ee61fd7 Mon Sep 17 00:00:00 2001 From: Adam Velebil Date: Mon, 21 Mar 2022 09:34:45 +0100 Subject: [PATCH] split currentDeviceProvider to desktop and android --- lib/android/init.dart | 1 + lib/android/state.dart | 25 +++++++++++++++++++++ lib/app/state.dart | 49 ++++++------------------------------------ lib/desktop/init.dart | 1 + lib/desktop/state.dart | 48 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+), 43 deletions(-) diff --git a/lib/android/init.dart b/lib/android/init.dart index 069c5699..5c406fab 100644 --- a/lib/android/init.dart +++ b/lib/android/init.dart @@ -43,5 +43,6 @@ Future> initializeAndGetOverrides() async { credentialListProvider.overrideWithProvider(androidCredentialListProvider), currentAppProvider.overrideWithProvider(androidSubPageProvider), managementStateProvider.overrideWithProvider(androidManagementState), + currentDeviceProvider.overrideWithProvider(androidCurrentDeviceProvider) ]; } diff --git a/lib/android/state.dart b/lib/android/state.dart index df8e7283..3b675a6c 100644 --- a/lib/android/state.dart +++ b/lib/android/state.dart @@ -48,3 +48,28 @@ class _AndroidAttachedDevicesNotifier extends AttachedDevicesNotifier { final androidDeviceDataProvider = Provider((ref) => ref.watch(androidYubikeyProvider)); + +final androidCurrentDeviceProvider = + StateNotifierProvider((ref) { + final provider = _AndroidCurrentDeviceNotifier(); + ref.listen(attachedDevicesProvider, provider._updateAttachedDevices); + return provider; +}); + +class _AndroidCurrentDeviceNotifier extends CurrentDeviceNotifier { + _AndroidCurrentDeviceNotifier() : super(null); + + _updateAttachedDevices( + List? previous, List devices) { + if (devices.isNotEmpty) { + state = devices.first; + } else { + state = null; + } + } + + @override + setCurrentDevice(DeviceNode device) { + state = device; + } +} diff --git a/lib/app/state.dart b/lib/app/state.dart index fb9dd033..2c28cc1c 100755 --- a/lib/app/state.dart +++ b/lib/app/state.dart @@ -73,51 +73,14 @@ final currentDeviceDataProvider = Provider( (ref) => throw UnimplementedError(), ); +// Override with platform implementation final currentDeviceProvider = - StateNotifierProvider((ref) { - final provider = CurrentDeviceNotifier(ref.watch(prefProvider)); - ref.listen(attachedDevicesProvider, provider._updateAttachedDevices); - return provider; -}); + StateNotifierProvider( + (ref) => throw UnimplementedError()); -class CurrentDeviceNotifier extends StateNotifier { - static const String _lastDevice = 'APP_STATE_LAST_DEVICE'; - final SharedPreferences _prefs; - CurrentDeviceNotifier(this._prefs) : super(null); - - _updateAttachedDevices(List? previous, List devices) { - if (!devices.contains(state)) { - final lastDevice = _prefs.getString(_lastDevice) ?? ''; - try { - state = devices.firstWhere( - (dev) => dev.when( - usbYubiKey: (path, name, pid, info) => - lastDevice == 'serial:${info?.serial}', - nfcReader: (path, name) => lastDevice == 'name:$name', - ), orElse: () { - var usbDevices = devices.whereType(); - return usbDevices.isNotEmpty ? usbDevices.first : devices.first; - }); - } on StateError { - state = null; - } - } - } - - setCurrentDevice(DeviceNode device) { - state = device; - device.when( - usbYubiKey: (path, name, pid, info) { - final serial = info?.serial; - if (serial != null) { - _prefs.setString(_lastDevice, 'serial:$serial'); - } - }, - nfcReader: (path, name) { - _prefs.setString(_lastDevice, 'name:$name'); - }, - ); - } +abstract class CurrentDeviceNotifier extends StateNotifier { + CurrentDeviceNotifier(DeviceNode? state) : super(state); + setCurrentDevice(DeviceNode device); } final currentAppProvider = diff --git a/lib/desktop/init.dart b/lib/desktop/init.dart index 8bc85913..34250fb7 100755 --- a/lib/desktop/init.dart +++ b/lib/desktop/init.dart @@ -100,5 +100,6 @@ Future> initializeAndGetOverrides( qrScannerProvider.overrideWithProvider(desktopQrScannerProvider), managementStateProvider.overrideWithProvider(desktopManagementState), fidoStateProvider.overrideWithProvider(desktopFidoState), + currentDeviceProvider.overrideWithProvider(desktopCurrentDeviceProvider) ]; } diff --git a/lib/desktop/state.dart b/lib/desktop/state.dart index dbe997d8..e59c28ca 100755 --- a/lib/desktop/state.dart +++ b/lib/desktop/state.dart @@ -3,8 +3,10 @@ import 'dart:io'; import 'package:logging/logging.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:shared_preferences/shared_preferences.dart'; import 'package:window_manager/window_manager.dart'; +import '../app/state.dart'; import '../core/state.dart'; import '../app/models.dart'; import 'models.dart'; @@ -113,3 +115,49 @@ class _WindowStateNotifier extends StateNotifier } } } + +final desktopCurrentDeviceProvider = + StateNotifierProvider((ref) { + final provider = _DesktopCurrentDeviceNotifier(ref.watch(prefProvider)); + ref.listen(attachedDevicesProvider, provider._updateAttachedDevices); + return provider; +}); + +class _DesktopCurrentDeviceNotifier extends CurrentDeviceNotifier { + static const String _lastDevice = 'APP_STATE_LAST_DEVICE'; + final SharedPreferences _prefs; + _DesktopCurrentDeviceNotifier(this._prefs) : super(null); + + _updateAttachedDevices(List? previous, List devices) { + if (!devices.contains(state)) { + final lastDevice = _prefs.getString(_lastDevice) ?? ''; + try { + state = devices.firstWhere( + (dev) => dev.when( + usbYubiKey: (path, name, pid, info) => + lastDevice == 'serial:${info?.serial}', + nfcReader: (path, name) => lastDevice == 'name:$name', + ), + orElse: () => devices.whereType().first); + } on StateError { + state = null; + } + } + } + + @override + setCurrentDevice(DeviceNode device) { + state = device; + device.when( + usbYubiKey: (path, name, pid, info) { + final serial = info?.serial; + if (serial != null) { + _prefs.setString(_lastDevice, 'serial:$serial'); + } + }, + nfcReader: (path, name) { + _prefs.setString(_lastDevice, 'name:$name'); + }, + ); + } +}