yubioath-flutter/lib/app/views/main_actions_dialog.dart

137 lines
3.5 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
2022-01-12 14:49:04 +03:00
import 'package:yubico_authenticator/management/models.dart';
import '../models.dart';
import '../state.dart';
import 'device_avatar.dart';
class MainActionsDialog extends ConsumerWidget {
const MainActionsDialog({Key? key}) : super(key: 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);
final actions = ref.watch(menuActionsProvider);
2022-01-12 14:49:04 +03:00
if (currentNode != null) {
devices.removeWhere((e) => e.path == currentNode.path);
2022-01-12 14:49:04 +03:00
}
return SimpleDialog(
children: [
2022-01-12 14:49:04 +03:00
if (currentNode != null)
2022-02-01 15:30:03 +03:00
_CurrentDeviceRow(
2022-01-12 14:49:04 +03:00
currentNode,
2022-02-01 15:30:03 +03:00
data: data,
2022-01-12 14:49:04 +03:00
onTap: () {
Navigator.of(context).pop();
},
),
...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
if (currentNode == null && devices.isEmpty)
Center(
child: Text(
'No YubiKey found',
style: Theme.of(context).textTheme.titleMedium,
)),
if (actions.isNotEmpty) const Divider(),
...actions.map((a) => ListTile(
dense: true,
leading: a.icon,
title: Text(a.text),
onTap: () {
Navigator.of(context).pop();
a.action?.call(context);
},
)),
],
);
}
}
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,
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
2022-02-01 15:30:03 +03:00
final subtitle = node.when(
usbYubiKey: (_, __, ___, info) =>
'S/N: ${info.serial} F/W: ${info.version}',
nfcReader: (_, name) {
final info = data?.info;
return info == null
? name
: '$name\nS/N: ${info.serial} F/W: ${info.version}';
});
2022-01-12 14:49:04 +03:00
return ListTile(
2022-02-01 15:30:03 +03:00
leading: data != null
? DeviceAvatar.yubiKeyData(
data!,
selected: true,
)
: DeviceAvatar.deviceNode(
node,
selected: true,
),
title: Text(data?.name ?? 'No YubiKey present'),
2022-01-12 14:49:04 +03:00
isThreeLine: subtitle.contains('\n'),
subtitle: Text(subtitle),
onTap: onTap,
);
}
}
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,
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
2022-01-12 14:49:04 +03:00
return ListTile(
2022-02-01 15:30:03 +03:00
leading: DeviceAvatar.deviceNode(node),
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) =>
'S/N: ${info.serial} F/W: ${info.version}',
nfcReader: (_, __) => 'Select to scan',
),
),
2022-01-12 14:49:04 +03:00
onTap: onTap,
);
}
}