yubioath-flutter/lib/oath/views/account_view.dart

230 lines
6.7 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'package:flutter/material.dart';
2021-12-03 17:15:00 +03:00
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../widgets/circle_timer.dart';
import '../../app/models.dart';
import '../models.dart';
import '../state.dart';
2022-01-21 17:45:04 +03:00
import 'delete_account_dialog.dart';
2022-01-24 12:59:09 +03:00
import 'rename_account_dialog.dart';
2021-12-06 12:26:38 +03:00
final _expireProvider =
StateNotifierProvider.autoDispose.family<_ExpireNotifier, bool, int>(
(ref, expiry) =>
_ExpireNotifier(DateTime.now().millisecondsSinceEpoch, expiry * 1000),
);
2021-12-06 12:26:38 +03:00
class _ExpireNotifier extends StateNotifier<bool> {
Timer? _timer;
_ExpireNotifier(int now, int expiry) : super(expiry <= now) {
if (expiry > now) {
_timer = Timer(Duration(milliseconds: expiry - now), () {
if (mounted) {
state = true;
}
});
}
}
@override
2021-12-06 12:26:38 +03:00
dispose() {
_timer?.cancel();
super.dispose();
}
2021-12-06 12:26:38 +03:00
}
2021-12-06 12:26:38 +03:00
class AccountView extends ConsumerWidget {
2022-01-18 17:46:42 +03:00
final YubiKeyData deviceData;
2021-12-06 12:26:38 +03:00
final OathCredential credential;
final OathCode? code;
final FocusNode? focusNode;
const AccountView(this.deviceData, this.credential, this.code,
{Key? key, this.focusNode})
2021-12-06 12:26:38 +03:00
: super(key: key);
2021-12-06 12:26:38 +03:00
String formatCode() {
var value = code?.value;
if (value == null) {
return '••• •••';
} else if (value.length < 6) {
return value;
} else {
var i = value.length ~/ 2;
return value.substring(0, i) + ' ' + value.substring(i);
}
}
List<PopupMenuEntry> _buildPopupMenu(
BuildContext context, WidgetRef ref, bool trigger) =>
[
2021-12-03 17:15:00 +03:00
PopupMenuItem(
child: const ListTile(
leading: Icon(Icons.copy),
title: Text('Copy to clipboard'),
),
onTap: () {
_copyToClipboard(context, ref, trigger);
2021-12-03 17:15:00 +03:00
},
),
PopupMenuItem(
child: const ListTile(
leading: Icon(Icons.star),
title: Text('Toggle favorite'),
),
onTap: () {
2021-12-06 12:26:38 +03:00
ref.read(favoritesProvider.notifier).toggleFavorite(credential.id);
2021-12-03 17:15:00 +03:00
},
),
2022-01-18 17:46:42 +03:00
if (deviceData.info.version.major >= 5 &&
deviceData.info.version.minor >= 3)
2021-12-03 17:15:00 +03:00
PopupMenuItem(
child: const ListTile(
leading: Icon(Icons.edit),
title: Text('Rename account'),
),
onTap: () {
2022-01-24 12:59:09 +03:00
// This ensures the onTap handler finishes before the dialog is shown, otherwise the dialog is immediately closed instead of the popup.
Future.delayed(Duration.zero, () {
showDialog(
context: context,
builder: (context) =>
RenameAccountDialog(deviceData.node, credential),
);
});
2021-12-03 17:15:00 +03:00
},
),
const PopupMenuDivider(),
2022-01-21 17:45:04 +03:00
PopupMenuItem(
child: const ListTile(
2021-12-03 17:15:00 +03:00
leading: Icon(Icons.delete_forever),
title: Text('Delete account'),
),
2022-01-21 17:45:04 +03:00
onTap: () {
// This ensures the onTap handler finishes before the dialog is shown, otherwise the dialog is immediately closed instead of the popup.
Future.delayed(Duration.zero, () {
showDialog(
context: context,
builder: (context) =>
DeleteAccountDialog(deviceData.node, credential),
);
});
},
2021-12-03 17:15:00 +03:00
),
];
_copyToClipboard(BuildContext context, WidgetRef ref, bool trigger) async {
String value;
if (trigger) {
final updated = await _calculate(context, ref);
value = updated.value;
} else {
value = code!.value;
}
await Clipboard.setData(ClipboardData(text: value));
2021-12-07 14:53:05 +03:00
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Code copied'),
duration: Duration(seconds: 2),
),
);
}
Future<OathCode> _calculate(BuildContext context, WidgetRef ref) async {
2021-12-07 14:53:05 +03:00
VoidCallback? close;
if (credential.touchRequired) {
final sbc = ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Touch your YubiKey'),
duration: Duration(seconds: 30),
),
);
unawaited(sbc.closed.then((_) {
close = null;
}));
2021-12-07 14:53:05 +03:00
close = sbc.close;
}
try {
return await ref
2022-01-18 17:46:42 +03:00
.read(credentialListProvider(deviceData.node.path).notifier)
2021-12-07 14:53:05 +03:00
.calculate(credential);
} finally {
close?.call();
}
}
@override
2021-12-06 12:26:38 +03:00
Widget build(BuildContext context, WidgetRef ref) {
final code = this.code;
final label = credential.issuer != null
? '${credential.issuer} (${credential.name})'
: credential.name;
final expired = ref.watch(_expireProvider(code?.validTo ?? 0));
2021-12-07 14:53:05 +03:00
final trigger = code == null ||
expired &&
(credential.touchRequired || credential.oathType == OathType.hotp);
2021-12-06 12:26:38 +03:00
return ListTile(
focusNode: focusNode,
2021-12-07 14:53:05 +03:00
onTap: () {
final focus = focusNode;
if (focus != null && focus.hasFocus == false) {
focus.requestFocus();
2021-12-07 14:53:05 +03:00
} else {
_copyToClipboard(context, ref, trigger);
2021-12-07 14:53:05 +03:00
}
},
2021-12-06 12:26:38 +03:00
leading: CircleAvatar(
backgroundColor: Colors.primaries
.elementAt(label.hashCode % Colors.primaries.length),
child: Text(
(credential.issuer ?? credential.name).characters.first.toUpperCase(),
style: const TextStyle(fontSize: 18),
),
2021-12-06 12:26:38 +03:00
),
title: Text(
formatCode(),
style: expired
? Theme.of(context)
.textTheme
.headline5
?.copyWith(color: Colors.grey)
: Theme.of(context).textTheme.headline5,
),
subtitle: Text(label, style: Theme.of(context).textTheme.caption),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
2021-12-07 14:53:05 +03:00
Column(
2021-12-06 12:26:38 +03:00
children: [
2021-12-07 14:53:05 +03:00
Align(
alignment: AlignmentDirectional.topCenter,
child: trigger
? const Icon(
Icons.touch_app,
size: 18,
)
: SizedBox.square(
dimension: 16,
child: CircleTimer(
code.validFrom * 1000,
code.validTo * 1000,
),
),
),
const Spacer(),
PopupMenuButton(
child: Icon(Icons.adaptive.more),
itemBuilder: (context) =>
_buildPopupMenu(context, ref, trigger),
2021-12-07 14:53:05 +03:00
),
2021-12-06 12:26:38 +03:00
],
),
],
),
);
}
}