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.
|
|
|
|
*/
|
|
|
|
|
2022-04-05 15:53:18 +03:00
|
|
|
import 'dart:async';
|
2022-04-03 12:06:22 +03:00
|
|
|
|
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';
|
2022-05-19 09:47:47 +03:00
|
|
|
import '../../core/models.dart';
|
2022-04-07 11:48:59 +03:00
|
|
|
import '../../core/state.dart';
|
2022-03-02 10:23:29 +03:00
|
|
|
import '../models.dart';
|
2022-03-03 12:01:36 +03:00
|
|
|
import 'account_mixin.dart';
|
2022-03-02 10:23:29 +03:00
|
|
|
|
2022-03-03 12:01:36 +03:00
|
|
|
class AccountDialog extends ConsumerWidget with AccountMixin {
|
|
|
|
@override
|
2022-03-02 10:23:29 +03:00
|
|
|
final OathCredential credential;
|
2022-09-21 16:29:34 +03:00
|
|
|
|
2022-05-12 10:56:55 +03:00
|
|
|
const AccountDialog(this.credential, {super.key});
|
2022-03-02 10:23:29 +03:00
|
|
|
|
2022-03-03 12:01:36 +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);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
2022-03-03 12:01:36 +03:00
|
|
|
}
|
|
|
|
return renamed;
|
2022-03-02 10:23:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2022-03-03 12:01:36 +03:00
|
|
|
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();
|
|
|
|
});
|
2022-03-03 12:01:36 +03:00
|
|
|
}
|
|
|
|
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-05-19 09:47:47 +03:00
|
|
|
|
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) {
|
2022-03-03 12:01:36 +03:00
|
|
|
final action = e.action;
|
2023-02-07 16:01:17 +03:00
|
|
|
final color = colors[e] ?? Pair(theme.secondary, theme.onSecondary);
|
2022-09-30 12:42:01 +03:00
|
|
|
final tooltip = e.trailing != null ? '${e.text}\n${e.trailing}' : e.text;
|
2022-05-18 16:17:53 +03:00
|
|
|
return Padding(
|
2022-09-07 10:01:48 +03:00
|
|
|
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,
|
2022-05-18 16:17:53 +03:00
|
|
|
),
|
|
|
|
),
|
2022-03-03 12:01:36 +03:00
|
|
|
);
|
|
|
|
}).toList();
|
|
|
|
}
|
2022-03-02 10:23:29 +03:00
|
|
|
|
2022-03-03 12:01:36 +03:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
2022-06-10 17:02:29 +03:00
|
|
|
// TODO: Solve this in a cleaner way
|
2022-09-26 10:40:20 +03:00
|
|
|
final currentDeviceData = ref.watch(currentDeviceDataProvider);
|
|
|
|
if (currentDeviceData is! AsyncData) {
|
2022-06-10 17:02:29 +03:00
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
2022-03-03 12:01:36 +03:00
|
|
|
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)) {
|
2022-04-07 11:48:59 +03:00
|
|
|
Timer(Duration.zero, () => calculateCode(context, ref));
|
|
|
|
}
|
2022-04-05 15:53:18 +03:00
|
|
|
}
|
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 {
|
2022-09-21 16:29:34 +03:00
|
|
|
copyToClipboard(
|
|
|
|
ref.watch(clipboardProvider), context, getCode(ref));
|
2022-06-10 14:49:02 +03:00
|
|
|
},
|
|
|
|
);
|
|
|
|
return null;
|
|
|
|
}),
|
2022-06-09 12:52:54 +03:00
|
|
|
},
|
2022-07-07 12:30:34 +03:00
|
|
|
child: FocusScope(
|
2022-06-10 14:49:02 +03:00
|
|
|
autofocus: true,
|
2022-07-05 13:11:43 +03:00
|
|
|
child: AlertDialog(
|
|
|
|
title: Center(
|
|
|
|
child: Text(
|
|
|
|
title,
|
|
|
|
style: Theme.of(context).textTheme.headlineSmall,
|
2023-01-11 12:37:59 +03:00
|
|
|
softWrap: true,
|
2023-01-11 13:12:13 +03:00
|
|
|
textAlign: TextAlign.center,
|
2022-06-09 12:52:54 +03:00
|
|
|
),
|
2022-07-05 13:11:43 +03:00
|
|
|
),
|
|
|
|
contentPadding: const EdgeInsets.symmetric(horizontal: 12.0),
|
|
|
|
content: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
if (subtitle != null)
|
|
|
|
Text(
|
|
|
|
subtitle!,
|
2023-01-11 12:37:59 +03:00
|
|
|
softWrap: true,
|
2023-01-11 13:12:13 +03:00
|
|
|
textAlign: TextAlign.center,
|
2022-07-05 13:11:43 +03:00
|
|
|
// This is what ListTile uses for subtitle
|
|
|
|
style: Theme.of(context).textTheme.bodyMedium!.copyWith(
|
2023-01-26 13:52:01 +03:00
|
|
|
color: Theme.of(context).textTheme.bodySmall!.color,
|
2022-07-05 13:11:43 +03:00
|
|
|
),
|
|
|
|
),
|
|
|
|
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,
|
2022-07-05 13:11:43 +03:00
|
|
|
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),
|
|
|
|
),
|
2022-07-05 13:11:43 +03:00
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
horizontal: 16.0, vertical: 8.0),
|
|
|
|
child: buildCodeView(ref),
|
2022-06-09 12:52:54 +03:00
|
|
|
),
|
2022-05-20 12:10:49 +03:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2022-07-05 13:11:43 +03:00
|
|
|
),
|
2022-06-09 12:52:54 +03:00
|
|
|
],
|
|
|
|
),
|
2022-09-23 11:12:54 +03:00
|
|
|
actionsPadding: const EdgeInsets.symmetric(vertical: 10.0),
|
2022-07-05 13:11:43 +03:00
|
|
|
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
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|