yubioath-flutter/lib/android/views/tap_request_dialog.dart

128 lines
3.2 KiB
Dart
Raw Normal View History

2022-03-03 18:43:36 +03:00
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../app/navigation_service.dart';
2022-04-07 12:03:26 +03:00
import '../api/impl.dart';
2022-03-03 18:43:36 +03:00
class FDialogApiImpl extends FDialogApi {
static _TapRequestDialogState? dialogState;
@override
Future<void> closeDialogApi() async {
await dialogState?.close();
dialogState = null;
}
@override
2022-03-16 11:13:31 +03:00
Future<void> showDialogApi(String dialogMessage) async {
2022-03-03 18:43:36 +03:00
/// note about use of unawaited
/// we don't need the result of the dialog and we don't want show to block
unawaited(showDialog(
context: NavigationService.navigatorKey.currentContext!,
builder: (context) => TapRequestDialog(
2022-03-16 11:13:31 +03:00
dialogMessage,
2022-03-03 18:43:36 +03:00
)));
}
}
class TapRequestDialog extends ConsumerStatefulWidget {
/// the current operation he user is performing
final String message;
const TapRequestDialog(this.message, {Key? key}) : super(key: key);
static void initialize() {
FDialogApi.setup(FDialogApiImpl());
}
@override
ConsumerState<ConsumerStatefulWidget> createState() =>
_TapRequestDialogState();
}
class _TapRequestDialogState extends ConsumerState<TapRequestDialog> {
/// used for information in the dialog (Success/Failure)
String _resultMessage = '';
/// mutable icon in the dialog, changes based on the operation result
2022-04-07 12:03:26 +03:00
IconData _icon = Icons.wifi;
2022-03-03 18:43:36 +03:00
bool _canPop = true;
bool _userCancelled = false;
_TapRequestDialogState();
@override
void initState() {
super.initState();
_canPop = true;
FDialogApiImpl.dialogState = this;
}
@override
void dispose() {
super.dispose();
}
Future<void> close() async {
if (_userCancelled) {
return;
}
if (mounted) {
setState(() {
_icon = Icons.check_circle;
_resultMessage = 'Success';
_canPop = false; // we will close after delay, forbid manual closing
});
await Future.delayed(const Duration(seconds: 1), () {
if (mounted) {
var dialogContext = NavigationService.navigatorKey.currentContext!;
Navigator.pop(dialogContext);
}
});
}
}
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async {
setState(() {
_userCancelled = true;
});
HDialogApi _api = HDialogApi();
await _api.dialogClosed();
return _canPop;
},
child: AlertDialog(
title: const Text('Tap your key'),
content: Padding(
padding: const EdgeInsets.all(16),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('Operation: ${widget.message}'),
const SizedBox(
height: 32,
),
Icon(
_icon,
size: 64,
),
_resultMessage.isNotEmpty
? Text(
_resultMessage,
)
: const SizedBox(
height: 0,
),
],
)),
));
}
}