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';
|
2023-02-28 13:34:29 +03:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2023-11-27 13:41:05 +03:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2022-03-02 10:23:29 +03:00
|
|
|
|
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';
|
2023-06-09 15:46:16 +03:00
|
|
|
import '../../app/views/action_list.dart';
|
2023-11-27 13:41:05 +03:00
|
|
|
import '../../app/views/fs_dialog.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';
|
2023-09-29 15:12:11 +03:00
|
|
|
import '../features.dart' as features;
|
2022-03-02 10:23:29 +03:00
|
|
|
import '../models.dart';
|
2023-02-10 19:37:42 +03:00
|
|
|
import '../state.dart';
|
|
|
|
import 'account_helper.dart';
|
|
|
|
import 'actions.dart';
|
|
|
|
import 'delete_account_dialog.dart';
|
|
|
|
import 'rename_account_dialog.dart';
|
2022-03-02 10:23:29 +03:00
|
|
|
|
2023-02-10 19:37:42 +03:00
|
|
|
class AccountDialog extends ConsumerWidget {
|
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
|
|
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
2022-06-10 17:02:29 +03:00
|
|
|
// TODO: Solve this in a cleaner way
|
2023-02-10 19:37:42 +03:00
|
|
|
final node = ref.watch(currentDeviceDataProvider).valueOrNull?.node;
|
|
|
|
if (node == null) {
|
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();
|
|
|
|
}
|
|
|
|
|
2023-09-29 15:12:11 +03:00
|
|
|
final hasFeature = ref.watch(featureProvider);
|
2023-02-10 19:37:42 +03:00
|
|
|
final helper = AccountHelper(context, ref, credential);
|
|
|
|
final subtitle = helper.subtitle;
|
|
|
|
|
|
|
|
return registerOathActions(
|
|
|
|
credential,
|
|
|
|
ref: ref,
|
2022-06-10 14:49:02 +03:00
|
|
|
actions: {
|
2023-09-29 15:12:11 +03:00
|
|
|
if (hasFeature(features.accountsRename))
|
|
|
|
EditIntent: CallbackAction<EditIntent>(onInvoke: (_) async {
|
|
|
|
final credentials = ref.read(credentialsProvider);
|
|
|
|
final withContext = ref.read(withContextProvider);
|
|
|
|
final renamed =
|
|
|
|
await withContext((context) async => await showBlurDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => RenameAccountDialog.forOathCredential(
|
|
|
|
ref,
|
|
|
|
node,
|
|
|
|
credential,
|
|
|
|
credentials
|
|
|
|
?.map((e) => (e.issuer, e.name))
|
|
|
|
.toList() ??
|
|
|
|
[],
|
|
|
|
)));
|
|
|
|
if (renamed != null) {
|
|
|
|
// Replace the dialog with the renamed credential
|
|
|
|
await withContext((context) async {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
await showBlurDialog(
|
2023-08-18 15:54:55 +03:00
|
|
|
context: context,
|
2023-09-29 15:12:11 +03:00
|
|
|
builder: (context) {
|
|
|
|
return AccountDialog(renamed);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return renamed;
|
|
|
|
}),
|
|
|
|
if (hasFeature(features.accountsDelete))
|
|
|
|
DeleteIntent: CallbackAction<DeleteIntent>(onInvoke: (_) async {
|
|
|
|
final withContext = ref.read(withContextProvider);
|
|
|
|
final bool? deleted =
|
|
|
|
await ref.read(withContextProvider)((context) async =>
|
|
|
|
await showBlurDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => DeleteAccountDialog(
|
2023-08-18 15:54:55 +03:00
|
|
|
node,
|
|
|
|
credential,
|
2023-09-29 15:12:11 +03:00
|
|
|
),
|
|
|
|
) ??
|
|
|
|
false);
|
2023-02-10 19:37:42 +03:00
|
|
|
|
2023-09-29 15:12:11 +03:00
|
|
|
// Pop the account dialog if deleted
|
|
|
|
if (deleted == true) {
|
|
|
|
await withContext((context) async {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return deleted;
|
|
|
|
}),
|
2022-06-09 12:52:54 +03:00
|
|
|
},
|
2023-02-10 19:37:42 +03:00
|
|
|
builder: (context) {
|
|
|
|
if (helper.code == null &&
|
|
|
|
(isDesktop || node.transport == Transport.usb)) {
|
|
|
|
Timer.run(() {
|
2023-02-27 14:01:15 +03:00
|
|
|
// Only call if credential hasn't been deleted/renamed
|
|
|
|
if (ref.read(credentialsProvider)?.contains(credential) == true) {
|
2023-09-29 15:40:17 +03:00
|
|
|
Actions.invoke(context, const RefreshIntent());
|
2023-02-27 14:01:15 +03:00
|
|
|
}
|
2023-02-10 19:37:42 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return FocusScope(
|
|
|
|
autofocus: true,
|
2023-05-02 15:55:18 +03:00
|
|
|
child: FsDialog(
|
|
|
|
child: Column(
|
2023-02-10 19:37:42 +03:00
|
|
|
children: [
|
2023-05-02 15:55:18 +03:00
|
|
|
Padding(
|
2023-08-18 17:51:42 +03:00
|
|
|
padding:
|
|
|
|
const EdgeInsets.symmetric(horizontal: 12, vertical: 32),
|
|
|
|
child: Column(
|
2023-05-02 15:55:18 +03:00
|
|
|
children: [
|
2023-08-18 17:51:42 +03:00
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(vertical: 16),
|
|
|
|
child: Row(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
IconTheme(
|
|
|
|
data: IconTheme.of(context).copyWith(size: 24),
|
|
|
|
child: helper.buildCodeIcon(),
|
|
|
|
),
|
|
|
|
const SizedBox(width: 8.0),
|
|
|
|
DefaultTextStyle.merge(
|
|
|
|
style: const TextStyle(fontSize: 28),
|
|
|
|
child: helper.buildCodeLabel(),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2023-05-02 15:55:18 +03:00
|
|
|
),
|
2023-08-18 17:51:42 +03:00
|
|
|
Text(
|
|
|
|
helper.title,
|
|
|
|
style: Theme.of(context).textTheme.headlineSmall,
|
|
|
|
softWrap: true,
|
|
|
|
textAlign: TextAlign.center,
|
2023-05-02 15:55:18 +03:00
|
|
|
),
|
2023-08-18 17:51:42 +03:00
|
|
|
if (subtitle != null)
|
|
|
|
Text(
|
|
|
|
subtitle,
|
|
|
|
softWrap: true,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
// This is what ListTile uses for subtitle
|
|
|
|
style:
|
|
|
|
Theme.of(context).textTheme.bodyMedium!.copyWith(
|
|
|
|
color: Theme.of(context)
|
|
|
|
.textTheme
|
|
|
|
.bodySmall!
|
|
|
|
.color,
|
|
|
|
),
|
|
|
|
),
|
2023-05-02 15:55:18 +03:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
2023-06-15 18:39:17 +03:00
|
|
|
ActionListSection.fromMenuActions(
|
|
|
|
context,
|
2023-06-09 15:46:16 +03:00
|
|
|
AppLocalizations.of(context)!.s_actions,
|
2023-06-15 18:39:17 +03:00
|
|
|
actions: helper.buildActions(),
|
2023-06-09 15:46:16 +03:00
|
|
|
),
|
2023-02-10 19:37:42 +03:00
|
|
|
],
|
|
|
|
),
|
2022-06-09 12:52:54 +03:00
|
|
|
),
|
2023-02-10 19:37:42 +03:00
|
|
|
);
|
|
|
|
},
|
2022-03-02 10:23:29 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|