2022-03-03 12:01:36 +03:00
|
|
|
import 'dart:async';
|
2022-04-05 18:12:11 +03:00
|
|
|
import 'dart:ui';
|
2022-03-03 12:01:36 +03:00
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
|
2022-03-25 17:43:32 +03:00
|
|
|
import '../../app/message.dart';
|
2022-03-03 12:01:36 +03:00
|
|
|
import '../../app/models.dart';
|
|
|
|
import '../../app/state.dart';
|
2022-06-10 13:23:52 +03:00
|
|
|
import '../../user_cancelled_exception.dart';
|
2022-04-05 18:12:11 +03:00
|
|
|
import '../../widgets/circle_timer.dart';
|
2022-06-05 14:49:16 +03:00
|
|
|
import '../../widgets/custom_icons.dart';
|
2022-03-03 12:01:36 +03:00
|
|
|
import '../models.dart';
|
|
|
|
import '../state.dart';
|
|
|
|
import 'delete_account_dialog.dart';
|
|
|
|
import 'rename_account_dialog.dart';
|
|
|
|
|
|
|
|
mixin AccountMixin {
|
|
|
|
OathCredential get credential;
|
|
|
|
|
|
|
|
@protected
|
|
|
|
String get label => credential.issuer != null
|
|
|
|
? '${credential.issuer} (${credential.name})'
|
|
|
|
: credential.name;
|
|
|
|
|
2022-03-31 13:25:14 +03:00
|
|
|
@protected
|
|
|
|
String get title => credential.issuer ?? credential.name;
|
|
|
|
|
|
|
|
@protected
|
|
|
|
String? get subtitle => credential.issuer != null ? credential.name : null;
|
|
|
|
|
2022-03-03 12:01:36 +03:00
|
|
|
@protected
|
|
|
|
OathCode? getCode(WidgetRef ref) => ref.watch(codeProvider(credential));
|
|
|
|
|
|
|
|
@protected
|
2022-03-31 13:25:14 +03:00
|
|
|
String formatCode(OathCode? code) {
|
|
|
|
final value = code?.value;
|
2022-03-03 12:01:36 +03:00
|
|
|
if (value == null) {
|
2022-04-04 20:14:34 +03:00
|
|
|
return '';
|
2022-03-03 12:01:36 +03:00
|
|
|
} else if (value.length < 6) {
|
|
|
|
return value;
|
|
|
|
} else {
|
|
|
|
var i = value.length ~/ 2;
|
2022-05-12 09:34:51 +03:00
|
|
|
return '${value.substring(0, i)} ${value.substring(i)}';
|
2022-03-03 12:01:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@protected
|
2022-03-31 13:25:14 +03:00
|
|
|
bool isExpired(OathCode? code, WidgetRef ref) {
|
2022-03-03 12:01:36 +03:00
|
|
|
return code == null ||
|
|
|
|
(credential.oathType == OathType.totp &&
|
|
|
|
ref.watch(expiredProvider(code.validTo)));
|
|
|
|
}
|
|
|
|
|
|
|
|
@protected
|
2022-03-03 15:55:07 +03:00
|
|
|
bool isPinned(WidgetRef ref) =>
|
2022-03-03 12:01:36 +03:00
|
|
|
ref.watch(favoritesProvider).contains(credential.id);
|
|
|
|
|
|
|
|
@protected
|
|
|
|
Future<OathCode> calculateCode(BuildContext context, WidgetRef ref) async {
|
2022-04-28 14:08:36 +03:00
|
|
|
final node = ref.read(currentDeviceProvider)!;
|
|
|
|
return await ref
|
|
|
|
.read(credentialListProvider(node.path).notifier)
|
|
|
|
.calculate(credential);
|
2022-03-03 12:01:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@protected
|
|
|
|
void copyToClipboard(BuildContext context, WidgetRef ref) {
|
|
|
|
final code = getCode(ref);
|
|
|
|
if (code != null) {
|
|
|
|
Clipboard.setData(ClipboardData(text: code.value));
|
2022-03-25 17:43:32 +03:00
|
|
|
showMessage(context, 'Code copied to clipboard');
|
2022-03-03 12:01:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@protected
|
|
|
|
Future<OathCredential?> renameCredential(
|
|
|
|
BuildContext context, WidgetRef ref) async {
|
|
|
|
final node = ref.read(currentDeviceProvider)!;
|
|
|
|
return await showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => RenameAccountDialog(node, credential),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@protected
|
|
|
|
Future<bool> deleteCredential(BuildContext context, WidgetRef ref) async {
|
|
|
|
final node = ref.read(currentDeviceProvider)!;
|
|
|
|
return await showDialog(
|
2022-03-16 11:30:03 +03:00
|
|
|
context: context,
|
|
|
|
builder: (context) => DeleteAccountDialog(node, credential),
|
|
|
|
) ??
|
|
|
|
false;
|
2022-03-03 12:01:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@protected
|
2022-03-03 15:55:07 +03:00
|
|
|
List<MenuAction> buildActions(BuildContext context, WidgetRef ref) {
|
2022-03-03 12:01:36 +03:00
|
|
|
final deviceData = ref.watch(currentDeviceDataProvider);
|
|
|
|
if (deviceData == null) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
final code = getCode(ref);
|
2022-03-31 13:25:14 +03:00
|
|
|
final expired = isExpired(code, ref);
|
2022-03-03 12:01:36 +03:00
|
|
|
final manual =
|
|
|
|
credential.touchRequired || credential.oathType == OathType.hotp;
|
|
|
|
final ready = expired || credential.oathType == OathType.hotp;
|
2022-03-03 15:55:07 +03:00
|
|
|
final pinned = isPinned(ref);
|
2022-03-03 12:01:36 +03:00
|
|
|
|
|
|
|
return [
|
|
|
|
MenuAction(
|
|
|
|
text: 'Copy to clipboard',
|
|
|
|
icon: const Icon(Icons.copy),
|
|
|
|
action: code == null || expired
|
|
|
|
? null
|
|
|
|
: (context) {
|
|
|
|
Clipboard.setData(ClipboardData(text: code.value));
|
2022-03-25 17:43:32 +03:00
|
|
|
showMessage(context, 'Code copied to clipboard');
|
2022-03-03 12:01:36 +03:00
|
|
|
},
|
|
|
|
),
|
2022-05-18 18:29:50 +03:00
|
|
|
if (manual)
|
|
|
|
MenuAction(
|
|
|
|
text: 'Calculate',
|
|
|
|
icon: const Icon(Icons.refresh),
|
|
|
|
action: ready
|
2022-06-10 13:23:52 +03:00
|
|
|
? (context) async {
|
|
|
|
try {
|
|
|
|
await calculateCode(context, ref);
|
|
|
|
} on UserCancelledException catch (_) {
|
|
|
|
// ignored
|
|
|
|
}
|
2022-05-18 18:29:50 +03:00
|
|
|
}
|
|
|
|
: null,
|
|
|
|
),
|
2022-03-03 12:01:36 +03:00
|
|
|
MenuAction(
|
2022-03-03 16:23:51 +03:00
|
|
|
text: pinned ? 'Unpin account' : 'Pin account',
|
2022-06-05 14:49:16 +03:00
|
|
|
icon: pinned ? pushPinStrokeIcon : const Icon(Icons.push_pin_outlined),
|
2022-03-03 12:01:36 +03:00
|
|
|
action: (context) {
|
|
|
|
ref.read(favoritesProvider.notifier).toggleFavorite(credential.id);
|
|
|
|
},
|
|
|
|
),
|
2022-05-25 19:25:21 +03:00
|
|
|
if (deviceData.info.version.isAtLeast(5, 3))
|
2022-03-03 12:01:36 +03:00
|
|
|
MenuAction(
|
2022-04-05 14:20:22 +03:00
|
|
|
icon: const Icon(Icons.edit_outlined),
|
2022-03-03 12:01:36 +03:00
|
|
|
text: 'Rename account',
|
|
|
|
action: (context) async {
|
|
|
|
await renameCredential(context, ref);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
MenuAction(
|
|
|
|
text: 'Delete account',
|
2022-05-18 11:53:22 +03:00
|
|
|
icon: const Icon(Icons.delete_outline),
|
2022-03-03 12:01:36 +03:00
|
|
|
action: (context) async {
|
|
|
|
await deleteCredential(context, ref);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
];
|
|
|
|
}
|
2022-04-05 18:12:11 +03:00
|
|
|
|
|
|
|
@protected
|
2022-05-20 12:10:49 +03:00
|
|
|
Widget buildCodeView(WidgetRef ref) {
|
2022-04-05 18:12:11 +03:00
|
|
|
final code = getCode(ref);
|
|
|
|
final expired = isExpired(code, ref);
|
2022-05-18 18:29:50 +03:00
|
|
|
return AnimatedSize(
|
|
|
|
alignment: Alignment.centerRight,
|
|
|
|
duration: const Duration(milliseconds: 100),
|
2022-05-20 12:10:49 +03:00
|
|
|
child: Builder(builder: (context) {
|
|
|
|
return Row(
|
2022-05-18 18:29:50 +03:00
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
children: code == null
|
|
|
|
? [
|
|
|
|
Icon(
|
|
|
|
credential.oathType == OathType.hotp
|
|
|
|
? Icons.refresh
|
|
|
|
: Icons.touch_app,
|
|
|
|
),
|
2022-05-20 12:10:49 +03:00
|
|
|
const Text(''),
|
2022-05-18 18:29:50 +03:00
|
|
|
]
|
|
|
|
: [
|
|
|
|
if (credential.oathType == OathType.totp) ...[
|
|
|
|
...expired
|
|
|
|
? [
|
|
|
|
if (credential.touchRequired) ...[
|
2022-05-20 12:10:49 +03:00
|
|
|
const Icon(Icons.touch_app),
|
2022-04-05 18:12:11 +03:00
|
|
|
const SizedBox(width: 8.0),
|
2022-05-18 18:29:50 +03:00
|
|
|
]
|
|
|
|
]
|
|
|
|
: [
|
|
|
|
SizedBox.square(
|
2022-05-20 12:10:49 +03:00
|
|
|
dimension:
|
|
|
|
(IconTheme.of(context).size ?? 18) * 0.8,
|
2022-05-18 18:29:50 +03:00
|
|
|
child: CircleTimer(
|
|
|
|
code.validFrom * 1000,
|
|
|
|
code.validTo * 1000,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(width: 8.0),
|
|
|
|
],
|
|
|
|
],
|
|
|
|
Opacity(
|
|
|
|
opacity: expired ? 0.4 : 1.0,
|
|
|
|
child: Text(
|
|
|
|
formatCode(code),
|
2022-05-20 12:10:49 +03:00
|
|
|
style: const TextStyle(
|
|
|
|
fontFeatures: [FontFeature.tabularFigures()],
|
|
|
|
//fontWeight: FontWeight.w400,
|
2022-04-05 18:12:11 +03:00
|
|
|
),
|
|
|
|
),
|
2022-05-18 18:29:50 +03:00
|
|
|
),
|
|
|
|
],
|
2022-05-20 12:10:49 +03:00
|
|
|
);
|
|
|
|
}),
|
2022-04-05 18:12:11 +03:00
|
|
|
);
|
|
|
|
}
|
2022-03-03 12:01:36 +03:00
|
|
|
}
|