veilidchat/lib/components/contact_invitation_display.dart

155 lines
5.7 KiB
Dart
Raw Normal View History

2023-08-03 00:49:48 -04:00
import 'dart:async';
2023-08-04 01:00:38 -04:00
import 'package:archive/archive.dart';
2023-08-02 21:09:28 -04:00
import 'package:awesome_extensions/awesome_extensions.dart';
2023-08-04 01:00:38 -04:00
import 'package:basic_utils/basic_utils.dart';
2023-08-01 00:39:50 -04:00
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
2023-08-04 01:00:38 -04:00
import 'package:flutter/services.dart';
2023-08-01 00:39:50 -04:00
import 'package:flutter_riverpod/flutter_riverpod.dart';
2023-08-02 21:09:28 -04:00
import 'package:flutter_translate/flutter_translate.dart';
2023-08-04 01:00:38 -04:00
import 'package:qr_flutter/qr_flutter.dart';
2023-08-01 00:39:50 -04:00
2023-08-02 21:09:28 -04:00
import '../tools/tools.dart';
2023-08-04 01:00:38 -04:00
import '../veilid_support/veilid_support.dart';
2023-08-02 21:09:28 -04:00
class ContactInvitationDisplayDialog extends ConsumerStatefulWidget {
const ContactInvitationDisplayDialog({
2023-08-03 00:49:48 -04:00
required this.name,
required this.message,
required this.generator,
2023-08-02 21:09:28 -04:00
super.key,
});
2023-08-03 00:49:48 -04:00
final String name;
final String message;
2023-08-05 01:00:46 -04:00
final FutureOr<Uint8List> generator;
2023-08-02 21:09:28 -04:00
@override
ContactInvitationDisplayDialogState createState() =>
ContactInvitationDisplayDialogState();
2023-08-03 00:49:48 -04:00
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties
..add(StringProperty('name', name))
..add(StringProperty('message', message))
2023-08-05 01:00:46 -04:00
..add(DiagnosticsProperty<FutureOr<Uint8List>?>('generator', generator));
2023-08-03 00:49:48 -04:00
}
2023-08-02 21:09:28 -04:00
}
class ContactInvitationDisplayDialogState
extends ConsumerState<ContactInvitationDisplayDialog> {
final focusNode = FocusNode();
final formKey = GlobalKey<FormState>();
2023-08-04 01:00:38 -04:00
late final AutoDisposeFutureProvider<Uint8List?> _generateFutureProvider;
2023-08-02 21:09:28 -04:00
@override
void initState() {
super.initState();
2023-08-03 00:49:48 -04:00
_generateFutureProvider =
2023-08-04 01:00:38 -04:00
AutoDisposeFutureProvider<Uint8List>((ref) async => widget.generator);
2023-08-02 21:09:28 -04:00
}
2023-08-01 00:39:50 -04:00
@override
2023-08-02 21:09:28 -04:00
void dispose() {
focusNode.dispose();
super.dispose();
2023-08-01 00:39:50 -04:00
}
2023-08-04 01:00:38 -04:00
String makeTextInvite(String message, Uint8List data) {
final invite = StringUtils.addCharAtPosition(
base64UrlNoPadEncode(data), '\n', 40,
repeat: true);
final msg = message.isNotEmpty ? '$message\n' : '';
return '$msg'
2023-08-05 01:00:46 -04:00
'--- BEGIN VEILIDCHAT CONTACT INVITE ----\n'
2023-08-04 01:00:38 -04:00
'$invite\n'
2023-08-05 01:00:46 -04:00
'---- END VEILIDCHAT CONTACT INVITE -----\n';
2023-08-04 01:00:38 -04:00
}
2023-08-01 00:39:50 -04:00
@override
2023-08-02 21:09:28 -04:00
// ignore: prefer_expression_function_bodies
Widget build(BuildContext context) {
final theme = Theme.of(context);
final scale = theme.extension<ScaleScheme>()!;
2023-08-04 01:00:38 -04:00
final textTheme = theme.textTheme;
2023-08-03 00:49:48 -04:00
final signedContactInvitationBytesV = ref.watch(_generateFutureProvider);
2023-08-02 21:09:28 -04:00
final cardsize = MediaQuery.of(context).size.shortestSide - 24;
return Dialog(
backgroundColor: Colors.white,
child: SizedBox(
width: cardsize,
height: cardsize,
2023-08-03 00:49:48 -04:00
child: signedContactInvitationBytesV.when(
loading: () => waitingPage(context),
2023-08-04 01:00:38 -04:00
data: (data) {
if (data == null) {
Navigator.of(context).pop();
return const Text('');
}
return Form(
key: formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
FittedBox(
fit: BoxFit.scaleDown,
child: Text(
translate(
'send_invite_dialog.contact_invitation'),
style: textTheme.headlineMedium!
.copyWith(color: Colors.black))),
ConstrainedBox(
constraints: const BoxConstraints(
minWidth: 300, minHeight: 300),
child: QrImageView.withQr(
size: 300,
qr: QrCode.fromUint8List(
2023-08-05 01:00:46 -04:00
data: data,
2023-08-04 01:00:38 -04:00
errorCorrectLevel:
QrErrorCorrectLevel.L))),
FittedBox(
fit: BoxFit.scaleDown,
child: Text(widget.message,
softWrap: true,
style: textTheme.headlineSmall!
.copyWith(color: Colors.black))),
ElevatedButton.icon(
icon: const Icon(Icons.copy),
label: Text(translate(
'send_invite_dialog.copy_invitation')),
onPressed: () async {
showInfoToast(
context,
translate(
'send_invite_dialog.invitation_copied'));
await Clipboard.setData(ClipboardData(
2023-08-05 01:00:46 -04:00
text:
makeTextInvite(widget.message, data)));
2023-08-04 01:00:38 -04:00
},
),
]));
},
2023-08-03 00:49:48 -04:00
error: (e, s) {
Navigator.of(context).pop();
2023-08-04 01:00:38 -04:00
showErrorToast(context,
translate('send_invite_dialog.failed_to_generate'));
return const Text('');
2023-08-03 00:49:48 -04:00
})));
}
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties
..add(DiagnosticsProperty<FocusNode>('focusNode', focusNode))
..add(DiagnosticsProperty<GlobalKey<FormState>>('formKey', formKey));
2023-08-01 00:39:50 -04:00
}
}