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

193 lines
6.2 KiB
Dart
Raw Normal View History

import 'dart:async';
2022-03-02 10:23:29 +03:00
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
2022-07-04 13:56:31 +03:00
import '../../app/message.dart';
2022-06-10 14:49:02 +03:00
import '../../app/shortcuts.dart';
2022-05-12 09:34:51 +03:00
import '../../app/state.dart';
import '../../core/models.dart';
import '../../core/state.dart';
2022-03-31 13:25:14 +03:00
import '../../widgets/dialog_frame.dart';
2022-03-02 10:23:29 +03:00
import '../models.dart';
import 'account_mixin.dart';
2022-03-02 10:23:29 +03:00
class AccountDialog extends ConsumerWidget with AccountMixin {
@override
2022-03-02 10:23:29 +03:00
final OathCredential credential;
2022-05-12 10:56:55 +03:00
const AccountDialog(this.credential, {super.key});
2022-03-02 10:23:29 +03:00
@override
Future<OathCredential?> renameCredential(
BuildContext context, WidgetRef ref) async {
final renamed = await super.renameCredential(context, ref);
if (renamed != null) {
// Replace this dialog with a new one, for the renamed credential.
2022-05-12 09:34:51 +03:00
await ref.read(withContextProvider)((context) async {
Navigator.of(context).pop();
2022-07-04 13:56:31 +03:00
await showBlurDialog(
2022-05-12 09:34:51 +03:00
context: context,
builder: (context) {
return AccountDialog(renamed);
},
);
});
}
return renamed;
2022-03-02 10:23:29 +03:00
}
@override
Future<bool> deleteCredential(BuildContext context, WidgetRef ref) async {
final deleted = await super.deleteCredential(context, ref);
if (deleted) {
2022-05-12 09:34:51 +03:00
await ref.read(withContextProvider)((context) async {
Navigator.of(context).pop();
});
}
return deleted;
}
2022-06-02 18:01:40 +03:00
List<Widget> _buildActions(BuildContext context, WidgetRef ref) {
final actions = buildActions(context, ref);
final theme =
2022-05-19 14:53:48 +03:00
ButtonTheme.of(context).colorScheme ?? Theme.of(context).colorScheme;
2022-06-02 18:01:40 +03:00
final copy = actions.firstWhere(((e) => e.text.startsWith('Copy')));
final delete = actions.firstWhere(((e) => e.text.startsWith('Delete')));
final colors = {
copy: Pair(theme.primary, theme.onPrimary),
delete: Pair(theme.error, theme.onError),
};
// If we can't copy, but can calculate, highlight that button instead
if (copy.action == null) {
final calculates = actions.where(((e) => e.text.startsWith('Calculate')));
if (calculates.isNotEmpty) {
colors[calculates.first] = Pair(theme.primary, theme.onPrimary);
}
}
return actions.map((e) {
final action = e.action;
2022-06-02 18:01:40 +03:00
final color = colors[e] ?? Pair(theme.secondary, theme.onSecondary);
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 4.0),
child: CircleAvatar(
2022-06-02 18:01:40 +03:00
backgroundColor: action != null ? color.first : theme.secondary,
foregroundColor: color.second,
child: IconButton(
icon: e.icon,
2022-05-20 12:10:49 +03:00
iconSize: 22,
tooltip: e.text,
2022-06-02 18:01:40 +03:00
disabledColor: theme.onSecondary.withOpacity(0.2),
onPressed: action != null
? () {
action(context);
}
: null,
),
),
);
}).toList();
}
2022-03-02 10:23:29 +03:00
@override
Widget build(BuildContext context, WidgetRef ref) {
// TODO: Solve this in a cleaner way
if (ref.watch(currentDeviceDataProvider) is! AsyncData) {
// The rest of this method assumes there is a device, and will throw an exception if not.
// This will never be shown, as the dialog will be immediately closed
return const SizedBox();
}
final code = getCode(ref);
if (code == null) {
if (isDesktop) {
Timer(Duration.zero, () => calculateCode(context, ref));
}
}
2022-06-10 14:49:02 +03:00
return Actions(
actions: {
CopyIntent: CallbackAction(onInvoke: (_) async {
if (isExpired(code, ref)) {
await calculateCode(context, ref);
}
await ref.read(withContextProvider)(
(context) async {
copyToClipboard(context, ref);
},
);
return null;
}),
},
2022-06-10 14:49:02 +03:00
child: Focus(
autofocus: true,
child: DialogFrame(
child: AlertDialog(
title: Center(
child: Text(
title,
overflow: TextOverflow.fade,
style: Theme.of(context).textTheme.headlineSmall,
maxLines: 1,
softWrap: false,
),
),
2022-06-10 14:49:02 +03:00
contentPadding: const EdgeInsets.symmetric(horizontal: 12.0),
content: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
if (subtitle != null)
Text(
subtitle!,
overflow: TextOverflow.fade,
maxLines: 1,
softWrap: false,
// This is what ListTile uses for subtitle
style: Theme.of(context).textTheme.bodyMedium!.copyWith(
color: Theme.of(context).textTheme.caption!.color,
),
),
const SizedBox(height: 12.0),
DecoratedBox(
decoration: BoxDecoration(
shape: BoxShape.rectangle,
color: CardTheme.of(context).color,
borderRadius: const BorderRadius.all(Radius.circular(30.0)),
),
child: Center(
child: FittedBox(
child: DefaultTextStyle.merge(
style: const TextStyle(fontSize: 28),
child: IconTheme(
data: IconTheme.of(context).copyWith(size: 24),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16.0, vertical: 8.0),
child: buildCodeView(ref),
),
),
2022-05-20 12:10:49 +03:00
),
),
),
),
2022-06-10 14:49:02 +03:00
],
),
actionsPadding: const EdgeInsets.only(top: 10.0, right: -16.0),
actions: [
Center(
child: FittedBox(
alignment: Alignment.center,
child: Row(children: _buildActions(context, ref)),
),
)
],
),
2022-03-02 12:44:35 +03:00
),
2022-03-02 10:23:29 +03:00
),
);
}
}