yubioath-flutter/lib/android/tap_request_dialog.dart

101 lines
2.6 KiB
Dart
Raw Normal View History

2022-08-16 15:05:53 +03:00
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../app/state.dart';
import '../app/views/user_interaction.dart';
import '../widgets/custom_icons.dart';
const _channel = MethodChannel('com.yubico.authenticator.channel.dialog');
final androidDialogProvider = Provider<_DialogProvider>(
(ref) {
return _DialogProvider(ref.watch(withContextProvider));
},
);
class _DialogProvider {
final WithContext _withContext;
UserInteractionController? _controller;
_DialogProvider(this._withContext) {
_channel.setMethodCallHandler((call) async {
final args = jsonDecode(call.arguments);
switch (call.method) {
case 'close':
_closeDialog();
break;
case 'show':
await _showDialog(args['title'], args['description'], args['icon']);
2022-08-16 15:05:53 +03:00
break;
case 'state':
await _updateDialogState(
args['title'], args['description'], args['icon']);
break;
default:
throw PlatformException(
code: 'NotImplemented',
message: 'Method ${call.method} is not implemented',
);
}
});
}
void _closeDialog() {
_controller?.close();
_controller = null;
}
Widget? _getIcon(String? icon) {
switch (icon) {
case 'nfc':
return nfcIcon;
case 'success':
return const Icon(Icons.check_circle);
case 'error':
return const Icon(Icons.error);
default:
return null;
}
}
2022-08-16 15:05:53 +03:00
Future<void> _updateDialogState(
String? title, String? description, String? iconName) async {
final icon = _getIcon(iconName);
await _withContext((context) async {
_controller?.updateContent(
title: title,
description: description,
icon: icon != null
? IconTheme(
data: IconTheme.of(context).copyWith(size: 64),
child: icon,
)
: null,
);
});
2022-08-16 15:05:53 +03:00
}
Future<void> _showDialog(
String title, String description, String? iconName) async {
final icon = _getIcon(iconName);
2022-08-16 15:05:53 +03:00
_controller = await _withContext((context) async => promptUserInteraction(
context,
title: title,
description: description,
icon: icon != null
? IconTheme(
data: IconTheme.of(context).copyWith(size: 64),
child: icon,
)
: null,
2022-08-16 15:05:53 +03:00
onCancel: () {
_channel.invokeMethod('cancel');
},
));
}
}