Retain device better when elevating to admin.

This commit is contained in:
Dain Nilsson 2022-12-21 15:48:22 +01:00
parent d5ce976a9a
commit e19f1a3bc5
No known key found for this signature in database
GPG Key ID: F04367096FBA95E8
2 changed files with 13 additions and 6 deletions

View File

@ -118,7 +118,7 @@ class UsbDeviceNotifier extends StateNotifier<List<UsbYubiKeyNode>> {
pids.forEach((pid, count) { pids.forEach((pid, count) {
for (var i = 0; i < count; i++) { for (var i = 0; i < count; i++) {
usbDevices.add(DeviceNode.usbYubiKey( usbDevices.add(DeviceNode.usbYubiKey(
DevicePath(['invalid', '$pid-$i']), DevicePath(['pid', pid.value.toString(), i.toString()]),
pid.displayName, pid.displayName,
pid, pid,
null) as UsbYubiKeyNode); null) as UsbYubiKeyNode);

View File

@ -17,6 +17,7 @@
import 'dart:async'; import 'dart:async';
import 'dart:io'; import 'dart:io';
import 'package:collection/collection.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';
@ -162,11 +163,17 @@ class DesktopCurrentDeviceNotifier extends CurrentDeviceNotifier {
if (!devices.contains(_deviceNode)) { if (!devices.contains(_deviceNode)) {
final lastDevice = prefs.getString(_lastDevice) ?? ''; final lastDevice = prefs.getString(_lastDevice) ?? '';
try { _deviceNode =
_deviceNode = devices.firstWhere((dev) => dev.path.key == lastDevice, devices.where((dev) => dev.path.key == lastDevice).firstOrNull;
orElse: () => devices.whereType<UsbYubiKeyNode>().first); if (_deviceNode == null) {
} on StateError { final parts = lastDevice.split('/');
_deviceNode = null; if (parts.firstOrNull == 'pid') {
_deviceNode = devices
.whereType<UsbYubiKeyNode>()
.where((e) => e.pid.value.toString() == parts[1])
.firstOrNull;
}
_deviceNode ??= devices.whereType<UsbYubiKeyNode>().firstOrNull;
} }
} }
return _deviceNode; return _deviceNode;