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

221 lines
7.3 KiB
Dart
Raw Normal View History

2022-10-04 13:12:54 +03:00
/*
* Copyright (C) 2022 Yubico.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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-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);
2023-02-07 16:01:17 +03:00
final theme =
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 = {
2023-02-07 16:01:17 +03:00
copy: Pair(theme.primary, theme.onPrimary),
delete: Pair(theme.error, theme.onError),
2022-06-02 18:01:40 +03:00
};
// 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) {
2023-02-07 16:01:17 +03:00
colors[calculates.first] = Pair(theme.primary, theme.onPrimary);
2022-06-02 18:01:40 +03:00
}
}
return actions.map((e) {
final action = e.action;
2023-02-07 16:01:17 +03:00
final color = colors[e] ?? Pair(theme.secondary, theme.onSecondary);
final tooltip = e.trailing != null ? '${e.text}\n${e.trailing}' : e.text;
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 6.0),
2023-02-07 16:01:17 +03:00
child: CircleAvatar(
backgroundColor: action != null ? color.first : theme.secondary,
foregroundColor: color.second,
child: IconButton(
style: IconButton.styleFrom(
backgroundColor: action != null ? color.first : theme.secondary,
foregroundColor: color.second,
disabledBackgroundColor: theme.onSecondary.withOpacity(0.2),
fixedSize: const Size.square(38),
),
icon: e.icon,
iconSize: 22,
tooltip: tooltip,
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
2022-09-26 10:40:20 +03:00
final currentDeviceData = ref.watch(currentDeviceDataProvider);
if (currentDeviceData 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);
2023-01-02 20:02:32 +03:00
if (isValid(ref) && code == null) {
2022-09-26 10:40:20 +03:00
if (isDesktop ||
(isAndroid &&
currentDeviceData.value?.node.transport == Transport.usb)) {
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(
ref.watch(clipboardProvider), context, getCode(ref));
2022-06-10 14:49:02 +03:00
},
);
return null;
}),
},
child: FocusScope(
2022-06-10 14:49:02 +03:00
autofocus: true,
child: AlertDialog(
title: Center(
child: Text(
title,
style: Theme.of(context).textTheme.headlineSmall,
softWrap: true,
2023-01-11 13:12:13 +03:00
textAlign: TextAlign.center,
),
),
contentPadding: const EdgeInsets.symmetric(horizontal: 12.0),
content: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
if (subtitle != null)
Text(
subtitle!,
softWrap: true,
2023-01-11 13:12:13 +03:00
textAlign: TextAlign.center,
// This is what ListTile uses for subtitle
style: Theme.of(context).textTheme.bodyMedium!.copyWith(
color: Theme.of(context).textTheme.bodySmall!.color,
),
),
const SizedBox(height: 12.0),
DecoratedBox(
decoration: BoxDecoration(
shape: BoxShape.rectangle,
2023-02-03 11:56:23 +03:00
color: Theme.of(context).colorScheme.surfaceVariant,
borderRadius: const BorderRadius.all(Radius.circular(30.0)),
),
child: Center(
child: FittedBox(
child: DefaultTextStyle.merge(
style: const TextStyle(fontSize: 28),
child: IconTheme(
2023-02-07 16:01:17 +03:00
data: IconTheme.of(context).copyWith(
size: 24,
color: Theme.of(context)
.colorScheme
.onSurface
.withOpacity(0.4),
),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16.0, vertical: 8.0),
child: buildCodeView(ref),
),
2022-05-20 12:10:49 +03:00
),
),
),
),
),
],
),
2022-09-23 11:12:54 +03:00
actionsPadding: const EdgeInsets.symmetric(vertical: 10.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
),
);
}
}