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

135 lines
4.6 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'dart:ui';
2022-03-02 10:23:29 +03:00
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../widgets/circle_timer.dart';
2022-03-31 13:25:14 +03:00
import '../../widgets/dialog_frame.dart';
2022-03-02 10:23:29 +03:00
import '../models.dart';
import 'account_mixin.dart';
2022-03-02 10:23:29 +03:00
class AccountDialog extends ConsumerWidget with AccountMixin {
@override
2022-03-02 10:23:29 +03:00
final OathCredential credential;
const AccountDialog(this.credential, {Key? key}) : super(key: key);
2022-03-02 10:23:29 +03:00
@override
Future<OathCredential?> renameCredential(
BuildContext context, WidgetRef ref) async {
final renamed = await super.renameCredential(context, ref);
if (renamed != null) {
// Replace this dialog with a new one, for the renamed credential.
Navigator.of(context).pop();
await showDialog(
context: context,
builder: (context) {
return AccountDialog(renamed);
2022-03-02 10:23:29 +03:00
},
);
}
return renamed;
2022-03-02 10:23:29 +03:00
}
@override
Future<bool> deleteCredential(BuildContext context, WidgetRef ref) async {
final deleted = await super.deleteCredential(context, ref);
if (deleted) {
Navigator.of(context).pop();
}
return deleted;
}
List<Widget> _buildActions(BuildContext context, WidgetRef ref) {
2022-03-03 15:55:07 +03:00
return buildActions(context, ref).map((e) {
final action = e.action;
return IconButton(
icon: e.icon,
tooltip: e.text,
onPressed: action != null
? () {
action(context);
}
: null,
);
}).toList();
}
2022-03-02 10:23:29 +03:00
@override
Widget build(BuildContext context, WidgetRef ref) {
final code = getCode(ref);
2022-03-31 13:25:14 +03:00
final expired = isExpired(code, ref);
if (code == null) {
Timer(Duration.zero, () => calculateCode(context, ref));
}
2022-03-31 13:25:14 +03:00
return DialogFrame(
child: AlertDialog(
title: Text(title),
contentPadding: const EdgeInsets.symmetric(horizontal: 24.0),
actionsAlignment: MainAxisAlignment.center,
actionsPadding: EdgeInsets.zero,
content: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(subtitle ?? ''),
const SizedBox(height: 8.0),
Center(
child: DecoratedBox(
decoration: BoxDecoration(
shape: BoxShape.rectangle,
borderRadius: const BorderRadius.all(Radius.circular(30.0)),
border: Border.all(width: 1.0, color: Colors.grey.shade500),
),
2022-04-05 16:23:51 +03:00
child: AnimatedSize(
2022-04-05 17:52:49 +03:00
alignment: Alignment.centerRight,
2022-04-05 16:23:51 +03:00
duration: const Duration(milliseconds: 100),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16.0, vertical: 8.0),
child: Row(
mainAxisSize: MainAxisSize.min,
children: code == null
? [
const Icon(Icons.touch_app, size: 36),
const Text('', style: TextStyle(fontSize: 32.0)),
]
: [
if (credential.oathType == OathType.totp) ...[
credential.touchRequired && expired
? const Icon(Icons.touch_app)
: SizedBox.square(
dimension: 32,
child: CircleTimer(
code.validFrom * 1000,
code.validTo * 1000,
),
),
2022-04-05 16:23:51 +03:00
const SizedBox(width: 8.0)
],
Opacity(
opacity: expired ? 0.4 : 1.0,
child: Text(
formatCode(code),
style: const TextStyle(
fontSize: 32.0,
fontFeatures: [
FontFeature.tabularFigures()
],
),
),
),
2022-04-05 16:23:51 +03:00
],
),
),
2022-03-02 10:23:29 +03:00
),
2022-03-31 13:25:14 +03:00
),
),
2022-03-31 13:25:14 +03:00
],
2022-03-02 12:44:35 +03:00
),
2022-03-31 13:25:14 +03:00
actions: _buildActions(context, ref),
2022-03-02 10:23:29 +03:00
),
);
}
}