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

205 lines
6.4 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 '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';
import '../../app/state.dart';
2023-01-09 19:22:34 +03:00
import '../../exception/cancellation_exception.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';
import 'account_mixin.dart';
class AccountView extends ConsumerWidget with AccountMixin {
@override
2021-12-06 12:26:38 +03:00
final OathCredential credential;
2022-08-24 18:34:54 +03:00
2022-07-07 13:40:54 +03:00
AccountView(this.credential, {super.key});
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],
];
return colors[label.hashCode % colors.length]!;
}
List<PopupMenuItem> _buildPopupMenu(BuildContext context, WidgetRef ref) {
2022-03-03 15:55:07 +03:00
return buildActions(context, ref).map((e) {
final action = e.action;
2022-07-07 21:20:04 +03:00
return buildMenuItem(
leading: e.icon,
title: Text(e.text),
action: action != null
? () {
ref.read(withContextProvider)((context) async {
action.call(context);
});
}
: null,
2022-09-28 18:31:17 +03:00
trailing: e.trailing,
);
}).toList();
}
@override
2021-12-06 12:26:38 +03:00
Widget build(BuildContext context, WidgetRef ref) {
final code = getCode(ref);
2022-03-31 13:25:14 +03:00
final expired = code == null ||
(credential.oathType == OathType.totp &&
ref.watch(expiredProvider(code.validTo)));
final calculateReady = code == null ||
2022-03-02 10:23:29 +03:00
credential.oathType == OathType.hotp ||
(credential.touchRequired && expired);
2021-12-06 12:26:38 +03:00
Future<void> triggerCopy() async {
try {
final withContext = ref.read(withContextProvider);
await withContext(
(context) async {
2022-08-24 18:34:54 +03:00
OathCode? code = calculateReady
? await calculateCode(
context,
ref,
)
: getCode(ref);
await withContext((context) async =>
copyToClipboard(ref.watch(clipboardProvider), context, code));
},
);
2022-06-20 10:47:34 +03:00
} on CancellationException catch (_) {
// ignored
}
}
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
return GestureDetector(
onSecondaryTapDown: (details) {
showMenu(
2022-03-02 10:23:29 +03:00
context: context,
position: RelativeRect.fromLTRB(
details.globalPosition.dx,
details.globalPosition.dy,
details.globalPosition.dx,
0,
),
items: _buildPopupMenu(context, ref),
2022-03-02 10:23:29 +03:00
);
},
2022-06-10 14:49:02 +03:00
child: Actions(
actions: {
CopyIntent: CallbackAction(onInvoke: (_) async {
await triggerCopy();
2022-06-10 14:49:02 +03:00
return null;
}),
},
child: LayoutBuilder(builder: (context, constraints) {
final showAvatar = constraints.maxWidth >= 315;
return ListTile(
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(30)),
onTap: () {
2022-07-04 13:56:31 +03:00
showBlurDialog(
context: context,
2022-07-07 13:40:54 +03:00
builder: (context) => AccountDialog(credential),
);
},
onLongPress: triggerCopy,
leading: showAvatar
? 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),
),
)
: null,
title: Text(
title,
overflow: TextOverflow.fade,
maxLines: 1,
softWrap: false,
2022-05-18 18:29:50 +03:00
),
subtitle: subtitle != null
? Text(
subtitle!,
overflow: TextOverflow.fade,
maxLines: 1,
softWrap: false,
)
: null,
trailing: GestureDetector(
onTap: () {
// Block opening the dialog.
},
onDoubleTap: triggerCopy,
child: DecoratedBox(
decoration: BoxDecoration(
shape: BoxShape.rectangle,
2023-02-07 16:01:17 +03:00
color: theme.colorScheme.surfaceVariant.withOpacity(0.4),
2023-02-03 11:56:23 +03:00
borderRadius: const BorderRadius.all(Radius.circular(64.0)),
),
child: Padding(
padding: const EdgeInsets.symmetric(
2023-02-03 11:56:23 +03:00
horizontal: 8.0, vertical: 4.0),
child: DefaultTextStyle.merge(
2023-02-03 18:16:42 +03:00
style: theme.textTheme.titleMedium,
child: IconTheme(
data: IconTheme.of(context).copyWith(
2023-02-07 16:01:17 +03:00
color:
theme.colorScheme.onSurfaceVariant.withOpacity(0.4),
2023-02-03 18:16:42 +03:00
),
child: buildCodeView(ref),
),
),
),
2022-05-20 12:10:49 +03:00
),
),
2022-06-10 14:49:02 +03:00
);
}),
),
);
}
}