veilidchat/lib/contact_invitation/views/contact_invitation_display.dart

152 lines
5.4 KiB
Dart
Raw Normal View History

2023-08-03 00:49:48 -04:00
import 'dart:async';
2023-09-22 11:20:28 -04:00
import 'dart:math';
2023-08-03 00:49:48 -04:00
2023-08-09 02:33:31 -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-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';
2024-01-09 20:58:27 -05:00
import 'package:veilid_support/veilid_support.dart';
2023-08-01 00:39:50 -04:00
2024-01-09 20:58:27 -05:00
import '../../tools/tools.dart';
2023-08-02 21:09:28 -04:00
2024-01-09 20:58:27 -05:00
class ContactInvitationDisplayDialog extends StatefulWidget {
2023-08-02 21:09:28 -04:00
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
2024-01-09 20:58:27 -05:00
extends State<ContactInvitationDisplayDialog> {
2023-08-02 21:09:28 -04:00
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);
2023-08-05 23:58:13 -04:00
//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-09-22 11:20:28 -04:00
final cardsize =
min<double>(MediaQuery.of(context).size.shortestSide - 48.0, 400);
2023-08-02 21:09:28 -04:00
return Dialog(
backgroundColor: Colors.white,
2023-09-22 11:20:28 -04:00
child: ConstrainedBox(
constraints: BoxConstraints(
minWidth: cardsize,
maxWidth: cardsize,
minHeight: cardsize,
maxHeight: cardsize),
2023-08-03 00:49:48 -04:00
child: signedContactInvitationBytesV.when(
2023-09-22 11:20:28 -04:00
loading: () => buildProgressIndicator(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,
2023-09-22 11:20:28 -04:00
child: Column(children: [
FittedBox(
child: Text(
translate(
'send_invite_dialog.contact_invitation'),
style: textTheme.headlineSmall!
.copyWith(color: Colors.black)))
.paddingAll(8),
FittedBox(
2023-08-04 01:00:38 -04:00
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:
2023-09-22 11:20:28 -04:00
QrErrorCorrectLevel.L)))
.expanded(),
Text(widget.message,
softWrap: true,
style: textTheme.labelLarge!
.copyWith(color: Colors.black))
.paddingAll(8),
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(
text: makeTextInvite(widget.message, data)));
},
).paddingAll(16),
]));
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
}
}