1
1
mirror of https://github.com/Yubico/yubioath-flutter.git synced 2024-12-20 00:31:44 +03:00
yubioath-flutter/lib/app/views/device_picker_dialog.dart

172 lines
4.3 KiB
Dart
Raw Normal View History

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
2022-04-04 11:24:29 +03:00
import '../../management/models.dart';
import '../models.dart';
import '../state.dart';
import 'device_avatar.dart';
String _getSubtitle(DeviceInfo info) {
final serial = info.serial;
var subtitle = '';
if (serial != null) {
subtitle += 'S/N: $serial ';
}
subtitle += 'F/W: ${info.version}';
return subtitle;
}
2022-03-31 12:41:28 +03:00
class DevicePickerDialog extends ConsumerWidget {
2022-05-12 10:56:55 +03:00
const DevicePickerDialog({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
2022-01-12 14:49:04 +03:00
final devices = ref.watch(attachedDevicesProvider).toList();
final currentNode = ref.watch(currentDeviceProvider);
final data = ref.watch(currentDeviceDataProvider);
if (currentNode != null) {
devices.removeWhere((e) => e.path == currentNode.path);
2022-01-12 14:49:04 +03:00
}
return SimpleDialog(
children: [
currentNode == null
? ListTile(
2022-06-02 15:49:38 +03:00
leading: const DeviceAvatar(
child: Icon(Icons.no_cell),
),
title: const Text('No YubiKey'),
subtitle: Text(Platform.isAndroid
? 'Insert or tap a YubiKey'
: (devices.isEmpty
? 'Insert a YubiKey'
: 'Insert a YubiKey, or select an item below')),
)
: _CurrentDeviceRow(
currentNode,
data: data,
onTap: () {
Navigator.of(context).pop();
},
),
2022-03-31 12:41:28 +03:00
if (devices.isNotEmpty) const Divider(),
2022-01-12 14:49:04 +03:00
...devices.map(
2022-02-01 15:30:03 +03:00
(e) => _DeviceRow(
2022-01-12 14:49:04 +03:00
e,
2022-02-01 15:30:03 +03:00
info: e.map(
usbYubiKey: (node) => node.info,
nfcReader: (_) => null,
2022-01-12 14:49:04 +03:00
),
onTap: () {
Navigator.of(context).pop();
ref.read(currentDeviceProvider.notifier).setCurrentDevice(e);
},
),
),
],
);
}
}
2022-02-01 15:30:03 +03:00
class _CurrentDeviceRow extends StatelessWidget {
2022-01-12 14:49:04 +03:00
final DeviceNode node;
2022-02-01 15:30:03 +03:00
final YubiKeyData? data;
2022-01-12 14:49:04 +03:00
final Function() onTap;
2022-02-01 15:30:03 +03:00
const _CurrentDeviceRow(
this.node, {
this.data,
2022-01-12 14:49:04 +03:00
required this.onTap,
2022-05-12 10:56:55 +03:00
});
2022-01-12 14:49:04 +03:00
@override
Widget build(BuildContext context) {
return node.when(usbYubiKey: (path, name, pid, info) {
if (info != null) {
return ListTile(
leading: DeviceAvatar.yubiKeyData(
data!,
selected: true,
),
title: Text(name),
subtitle: Text(_getSubtitle(info)),
onTap: onTap,
);
} else {
{
return ListTile(
leading: DeviceAvatar.deviceNode(
2022-02-01 15:30:03 +03:00
node,
selected: true,
),
title: Text(name),
subtitle: const Text('Device inaccessible'),
onTap: onTap,
);
}
}
}, nfcReader: (path, name) {
final info = data?.info;
if (info != null) {
return ListTile(
leading: DeviceAvatar.yubiKeyData(
data!,
selected: true,
),
title: Text(data!.name),
isThreeLine: true,
subtitle: Text('$name\n${_getSubtitle(info)}'),
onTap: onTap,
);
} else {
return ListTile(
leading: DeviceAvatar.deviceNode(
node,
selected: true,
),
title: const Text('No YubiKey present'),
subtitle: Text(name),
onTap: onTap,
);
}
});
2022-01-12 14:49:04 +03:00
}
}
2022-02-01 15:30:03 +03:00
class _DeviceRow extends StatelessWidget {
2022-01-12 14:49:04 +03:00
final DeviceNode node;
final DeviceInfo? info;
final Function() onTap;
2022-02-01 15:30:03 +03:00
const _DeviceRow(
this.node, {
2022-01-12 14:49:04 +03:00
required this.info,
required this.onTap,
2022-05-12 10:56:55 +03:00
});
@override
Widget build(BuildContext context) {
2022-01-12 14:49:04 +03:00
return ListTile(
2022-06-02 15:49:38 +03:00
leading: Padding(
padding: const EdgeInsets.symmetric(horizontal: 4),
child: DeviceAvatar.deviceNode(
node,
radius: 20,
),
),
2022-02-01 15:30:03 +03:00
title: Text(node.name),
2022-01-12 14:49:04 +03:00
subtitle: Text(
2022-02-01 15:30:03 +03:00
node.when(
usbYubiKey: (_, __, ___, info) =>
info == null ? 'Device inaccessible' : _getSubtitle(info),
2022-02-01 15:30:03 +03:00
nfcReader: (_, __) => 'Select to scan',
),
),
2022-01-12 14:49:04 +03:00
onTap: onTap,
);
}
}