2022-03-03 12:01:36 +03:00
|
|
|
import 'dart:async';
|
|
|
|
|
2021-11-22 11:49:52 +03:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2022-05-12 09:34:51 +03:00
|
|
|
import 'package:yubico_authenticator/app/state.dart';
|
2021-11-22 11:49:52 +03:00
|
|
|
|
|
|
|
import '../models.dart';
|
2022-03-31 13:25:14 +03:00
|
|
|
import '../state.dart';
|
2022-03-03 12:01:36 +03:00
|
|
|
import 'account_dialog.dart';
|
|
|
|
import 'account_mixin.dart';
|
2022-02-24 11:52:42 +03:00
|
|
|
|
2022-03-03 12:01:36 +03:00
|
|
|
class AccountView extends ConsumerWidget with AccountMixin {
|
|
|
|
@override
|
2021-12-06 12:26:38 +03:00
|
|
|
final OathCredential credential;
|
2022-02-10 14:11:44 +03:00
|
|
|
final FocusNode? focusNode;
|
2022-03-28 13:58:45 +03:00
|
|
|
AccountView(this.credential, {Key? key, this.focusNode}) : super(key: key);
|
2021-11-22 11:49:52 +03:00
|
|
|
|
2022-03-03 12:01:36 +03:00
|
|
|
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]!;
|
|
|
|
}
|
|
|
|
|
2022-03-03 12:01:36 +03:00
|
|
|
List<PopupMenuItem> _buildPopupMenu(BuildContext context, WidgetRef ref) {
|
2022-03-03 15:55:07 +03:00
|
|
|
return buildActions(context, ref).map((e) {
|
2022-03-03 12:01:36 +03:00
|
|
|
final action = e.action;
|
|
|
|
return PopupMenuItem(
|
|
|
|
enabled: action != null,
|
|
|
|
onTap: () {
|
2022-03-03 13:20:47 +03:00
|
|
|
// As soon as onTap returns, the Navigator is popped,
|
|
|
|
// closing the topmost item. Since we sometimes open new dialogs in
|
|
|
|
// the action, make sure that happens *after* the pop.
|
2022-03-03 12:01:36 +03:00
|
|
|
Timer(Duration.zero, () {
|
|
|
|
action?.call(context);
|
|
|
|
});
|
|
|
|
},
|
2022-05-12 09:34:51 +03:00
|
|
|
child: ListTile(
|
|
|
|
leading: e.icon,
|
|
|
|
title: Text(e.text),
|
|
|
|
dense: true,
|
|
|
|
contentPadding: EdgeInsets.zero,
|
|
|
|
),
|
2022-03-03 12:01:36 +03:00
|
|
|
);
|
|
|
|
}).toList();
|
|
|
|
}
|
|
|
|
|
2021-11-22 11:49:52 +03:00
|
|
|
@override
|
2021-12-06 12:26:38 +03:00
|
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
2022-03-03 12:01:36 +03:00
|
|
|
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)));
|
2022-03-03 12:01:36 +03:00
|
|
|
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
|
|
|
|
2022-02-22 12:56:52 +03:00
|
|
|
final darkMode = Theme.of(context).brightness == Brightness.dark;
|
|
|
|
|
2022-03-03 12:01:36 +03:00
|
|
|
return GestureDetector(
|
|
|
|
onSecondaryTapDown: (details) {
|
|
|
|
showMenu(
|
2022-03-02 10:23:29 +03:00
|
|
|
context: context,
|
2022-03-03 12:01:36 +03:00
|
|
|
position: RelativeRect.fromLTRB(
|
2022-03-03 13:20:47 +03:00
|
|
|
details.globalPosition.dx,
|
|
|
|
details.globalPosition.dy,
|
|
|
|
details.globalPosition.dx,
|
|
|
|
0,
|
|
|
|
),
|
2022-03-03 12:01:36 +03:00
|
|
|
items: _buildPopupMenu(context, ref),
|
2022-03-02 10:23:29 +03:00
|
|
|
);
|
|
|
|
},
|
2022-03-03 12:01:36 +03:00
|
|
|
child: ListTile(
|
|
|
|
focusNode: focusNode,
|
|
|
|
onTap: () {
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) {
|
|
|
|
return AccountDialog(credential);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
onLongPress: () async {
|
|
|
|
if (calculateReady) {
|
|
|
|
await calculateCode(
|
|
|
|
context,
|
|
|
|
ref,
|
|
|
|
);
|
|
|
|
}
|
2022-05-12 09:34:51 +03:00
|
|
|
await ref.read(withContextProvider)(
|
|
|
|
(context) async {
|
|
|
|
copyToClipboard(context, ref);
|
|
|
|
},
|
|
|
|
);
|
2022-03-03 12:01:36 +03:00
|
|
|
},
|
|
|
|
leading: 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: 18),
|
|
|
|
),
|
2021-11-22 11:49:52 +03:00
|
|
|
),
|
2022-04-04 20:59:49 +03:00
|
|
|
title: Text(
|
|
|
|
title,
|
|
|
|
overflow: TextOverflow.fade,
|
|
|
|
maxLines: 1,
|
|
|
|
softWrap: false,
|
|
|
|
),
|
2022-03-31 13:25:14 +03:00
|
|
|
subtitle: subtitle != null
|
|
|
|
? Text(
|
|
|
|
subtitle!,
|
|
|
|
overflow: TextOverflow.fade,
|
|
|
|
maxLines: 1,
|
|
|
|
softWrap: false,
|
2022-03-03 12:01:36 +03:00
|
|
|
)
|
2022-03-31 13:25:14 +03:00
|
|
|
: null,
|
2022-04-05 18:12:11 +03:00
|
|
|
trailing: buildCodeView(ref),
|
2022-03-03 12:01:36 +03:00
|
|
|
),
|
2021-11-22 11:49:52 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|