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

258 lines
8.9 KiB
Dart
Raw Normal View History

2022-10-04 13:12:54 +03:00
/*
2023-02-28 17:25:27 +03:00
* Copyright (C) 2022-2023 Yubico.
2022-10-04 13:12:54 +03:00
*
* 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:io';
2023-03-15 12:32:01 +03:00
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
2023-02-10 13:08:37 +03:00
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.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';
import '../../app/state.dart';
2023-02-10 13:08:37 +03:00
import '../../core/state.dart';
2022-07-07 21:20:04 +03:00
import '../../widgets/menu_list_tile.dart';
import '../models.dart';
2022-03-31 13:25:14 +03:00
import '../state.dart';
import 'account_dialog.dart';
2023-02-10 19:37:42 +03:00
import 'account_helper.dart';
2023-02-21 13:40:38 +03:00
import 'account_icon.dart';
2023-02-10 19:37:42 +03:00
import 'actions.dart';
import 'delete_account_dialog.dart';
import 'rename_account_dialog.dart';
2023-02-10 13:08:37 +03:00
class AccountView extends ConsumerStatefulWidget {
2021-12-06 12:26:38 +03:00
final OathCredential credential;
2023-02-10 13:08:37 +03:00
const AccountView(this.credential, {super.key});
@override
ConsumerState<ConsumerStatefulWidget> createState() => _AccountViewState();
}
2023-03-15 12:32:01 +03:00
String _a11yCredentialLabel(String? issuer, String name, String? code) {
return [issuer, name, code].whereNotNull().join(' ');
}
2023-02-10 19:37:42 +03:00
class _AccountViewState extends ConsumerState<AccountView> {
2023-02-10 13:08:37 +03:00
OathCredential get credential => widget.credential;
2022-08-24 18:34:54 +03:00
2023-02-10 13:08:37 +03:00
final _focusNode = FocusNode();
int _lastTap = 0;
@override
void dispose() {
_focusNode.dispose();
super.dispose();
}
Color _iconColor(int shade) {
2022-02-22 12:56:52 +03:00
final colors = [
Colors.red[shade],
Colors.pink[shade],
Colors.purple[shade],
Colors.deepPurple[shade],
Colors.indigo[shade],
Colors.blue[shade],
Colors.lightBlue[shade],
Colors.cyan[shade],
Colors.teal[shade],
Colors.green[shade],
Colors.lightGreen[shade],
Colors.lime[shade],
Colors.yellow[shade],
Colors.amber[shade],
Colors.orange[shade],
Colors.deepOrange[shade],
Colors.brown[shade],
Colors.grey[shade],
Colors.blueGrey[shade],
];
2023-02-10 19:37:42 +03:00
final label = credential.issuer != null
? '${credential.issuer} (${credential.name})'
: credential.name;
2022-02-22 12:56:52 +03:00
return colors[label.hashCode % colors.length]!;
}
2023-02-10 19:37:42 +03:00
List<PopupMenuItem> _buildPopupMenu(
BuildContext context, AccountHelper helper) {
final shortcut = Platform.isMacOS ? '\u2318 C' : 'Ctrl+C';
final copyText = AppLocalizations.of(context)!.l_copy_to_clipboard;
2023-02-10 19:37:42 +03:00
return helper.buildActions().map((e) {
final intent = e.intent;
2022-07-07 21:20:04 +03:00
return buildMenuItem(
leading: e.icon,
title: Text(e.text),
2023-02-10 19:37:42 +03:00
action: intent != null
2022-07-07 21:20:04 +03:00
? () {
2023-02-10 19:37:42 +03:00
Actions.invoke(context, intent);
2022-07-07 21:20:04 +03:00
}
: null,
trailing: e.text == copyText ? shortcut : null,
);
}).toList();
}
@override
2023-02-10 13:08:37 +03:00
Widget build(BuildContext context) {
2023-02-03 18:16:42 +03:00
final theme = Theme.of(context);
final darkMode = theme.brightness == Brightness.dark;
2022-02-22 12:56:52 +03:00
2023-02-10 19:37:42 +03:00
return registerOathActions(
credential,
ref: ref,
actions: {
OpenIntent: CallbackAction<OpenIntent>(onInvoke: (_) async {
await showBlurDialog(
context: context,
builder: (context) => AccountDialog(credential),
);
return null;
}),
EditIntent: CallbackAction<EditIntent>(onInvoke: (_) async {
final node = ref.read(currentDeviceProvider)!;
final credentials = ref.read(credentialsProvider);
return await ref.read(withContextProvider)(
(context) async => await showBlurDialog(
context: context,
builder: (context) =>
RenameAccountDialog(node, credential, credentials),
));
}),
DeleteIntent: CallbackAction<DeleteIntent>(onInvoke: (_) async {
final node = ref.read(currentDeviceProvider)!;
return await ref.read(withContextProvider)((context) async =>
await showBlurDialog(
context: context,
builder: (context) => DeleteAccountDialog(node, credential),
) ??
false);
}),
2022-03-02 10:23:29 +03:00
},
2023-02-10 19:37:42 +03:00
builder: (context) {
final helper = AccountHelper(context, ref, credential);
2023-05-29 17:49:56 +03:00
return LayoutBuilder(builder: (context, constraints) {
final showAvatar = constraints.maxWidth >= 315;
final subtitle = helper.subtitle;
final circleAvatar = CircleAvatar(
foregroundColor: darkMode ? Colors.black : Colors.white,
backgroundColor: _iconColor(darkMode ? 300 : 400),
child: Text(
(credential.issuer ?? credential.name)
.characters
.first
.toUpperCase(),
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w300),
),
);
2023-02-17 17:15:20 +03:00
2023-05-29 17:49:56 +03:00
return Shortcuts(
shortcuts: {
LogicalKeySet(LogicalKeyboardKey.enter): const OpenIntent(),
LogicalKeySet(LogicalKeyboardKey.space): const OpenIntent(),
},
child: Semantics(
label: _a11yCredentialLabel(
credential.issuer, credential.name, helper.code?.value),
child: InkWell(
focusNode: _focusNode,
borderRadius: BorderRadius.circular(30),
onSecondaryTapDown: (details) {
showMenu(
context: context,
position: RelativeRect.fromLTRB(
details.globalPosition.dx,
details.globalPosition.dy,
details.globalPosition.dx,
0,
),
items: _buildPopupMenu(context, helper),
);
},
onTap: () {
if (isDesktop) {
final now = DateTime.now().millisecondsSinceEpoch;
if (now - _lastTap < 500) {
setState(() {
_lastTap = 0;
});
Actions.maybeInvoke(context, const CopyIntent());
} else {
2023-05-29 17:49:56 +03:00
_focusNode.requestFocus();
setState(() {
_lastTap = now;
});
}
2023-05-29 17:49:56 +03:00
} else {
Actions.maybeInvoke<OpenIntent>(
context, const OpenIntent());
}
},
onLongPress: () {
Actions.maybeInvoke(context, const CopyIntent());
},
child: ListTile(
leading: showAvatar
? AccountIcon(
issuer: credential.issuer,
defaultWidget: circleAvatar)
: null,
title: Text(
helper.title,
overflow: TextOverflow.fade,
maxLines: 1,
softWrap: false,
),
subtitle: subtitle != null
? Text(
subtitle,
overflow: TextOverflow.fade,
maxLines: 1,
softWrap: false,
)
: null,
trailing: Focus(
skipTraversal: true,
descendantsAreTraversable: false,
child: helper.code != null
? FilledButton.tonalIcon(
icon: helper.buildCodeIcon(),
label: helper.buildCodeLabel(),
onPressed: () {
Actions.maybeInvoke<OpenIntent>(
context, const OpenIntent());
},
)
: FilledButton.tonal(
onPressed: () {
Actions.maybeInvoke<OpenIntent>(
context, const OpenIntent());
},
child: helper.buildCodeIcon()),
),
),
2023-05-29 17:49:56 +03:00
),
));
});
2023-02-10 19:37:42 +03:00
},
);
}
}