veilidchat/lib/account_manager/views/new_account_page.dart

165 lines
5.5 KiB
Dart
Raw Normal View History

2023-07-23 23:13:21 -04:00
import 'package:awesome_extensions/awesome_extensions.dart';
2023-07-26 15:58:38 -04:00
import 'package:flutter/foundation.dart';
2023-01-10 21:04:18 -05:00
import 'package:flutter/material.dart';
2024-06-07 14:42:04 -04:00
import 'package:flutter_bloc/flutter_bloc.dart';
2023-07-26 14:20:29 -04:00
import 'package:flutter_form_builder/flutter_form_builder.dart';
2023-07-23 23:13:21 -04:00
import 'package:flutter_translate/flutter_translate.dart';
2023-07-24 09:31:51 -04:00
import 'package:form_builder_validators/form_builder_validators.dart';
2023-08-17 18:10:24 -04:00
import 'package:go_router/go_router.dart';
2023-07-23 23:13:21 -04:00
2024-06-07 14:42:04 -04:00
import '../../layout/default_app_bar.dart';
import '../../theme/theme.dart';
import '../../tools/tools.dart';
import '../../veilid_processor/veilid_processor.dart';
import '../account_manager.dart';
2023-01-10 21:04:18 -05:00
2023-12-26 20:26:54 -05:00
class NewAccountPage extends StatefulWidget {
2023-01-10 21:04:18 -05:00
const NewAccountPage({super.key});
@override
2023-07-26 14:20:29 -04:00
NewAccountPageState createState() => NewAccountPageState();
2023-07-24 09:31:51 -04:00
}
2023-12-27 22:56:24 -05:00
class NewAccountPageState extends State<NewAccountPage> {
2023-07-24 09:31:51 -04:00
final _formKey = GlobalKey<FormBuilderState>();
late bool isInAsyncCall = false;
2023-07-26 14:20:29 -04:00
static const String formFieldName = 'name';
2023-09-28 10:06:22 -04:00
static const String formFieldPronouns = 'pronouns';
2023-07-25 01:04:34 -04:00
2023-07-29 10:55:35 -04:00
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) async {
2023-12-27 22:56:24 -05:00
await changeWindowSetup(
2023-07-29 10:55:35 -04:00
TitleBarStyle.normal, OrientationCapability.portraitOnly);
});
}
2023-07-24 09:31:51 -04:00
Widget _newAccountForm(BuildContext context,
2024-06-07 14:42:04 -04:00
{required Future<void> Function(GlobalKey<FormBuilderState>) onSubmit}) {
final networkReady = context
.watch<ConnectionStateCubit>()
.state
.asData
?.value
.isPublicInternetReady ??
false;
final canSubmit = networkReady;
return FormBuilder(
key: _formKey,
child: ListView(
children: [
Text(translate('new_account_page.header'))
.textStyle(context.headlineSmall)
.paddingSymmetric(vertical: 16),
FormBuilderTextField(
autofocus: true,
name: formFieldName,
decoration:
InputDecoration(labelText: translate('account.form_name')),
maxLength: 64,
// The validator receives the text that the user has entered.
validator: FormBuilderValidators.compose([
FormBuilderValidators.required(),
]),
textInputAction: TextInputAction.next,
),
FormBuilderTextField(
name: formFieldPronouns,
maxLength: 64,
decoration:
InputDecoration(labelText: translate('account.form_pronouns')),
textInputAction: TextInputAction.next,
),
Row(children: [
const Spacer(),
Text(translate('new_account_page.instructions'))
.toCenter()
.flexible(flex: 6),
const Spacer(),
]).paddingSymmetric(vertical: 4),
ElevatedButton(
onPressed: !canSubmit
? null
: () async {
if (_formKey.currentState?.saveAndValidate() ?? false) {
2023-08-04 01:00:38 -04:00
setState(() {
2024-06-07 14:42:04 -04:00
isInAsyncCall = true;
2023-08-04 01:00:38 -04:00
});
2024-06-07 14:42:04 -04:00
try {
await onSubmit(_formKey);
} finally {
if (mounted) {
setState(() {
isInAsyncCall = false;
});
}
}
2023-08-04 01:00:38 -04:00
}
2024-06-07 14:42:04 -04:00
},
child: Text(translate(!networkReady
? 'button.waiting_for_network'
: 'new_account_page.create')),
).paddingSymmetric(vertical: 4).alignAtCenterRight(),
],
),
);
}
2023-07-24 09:31:51 -04:00
@override
Widget build(BuildContext context) {
2024-01-04 22:29:43 -05:00
final displayModalHUD = isInAsyncCall;
2023-07-23 23:13:21 -04:00
2023-01-10 21:04:18 -05:00
return Scaffold(
2023-07-24 09:31:51 -04:00
// resizeToAvoidBottomInset: false,
2023-08-17 18:10:24 -04:00
appBar: DefaultAppBar(
title: Text(translate('new_account_page.titlebar')),
actions: [
const SignalStrengthMeterWidget(),
IconButton(
icon: const Icon(Icons.settings),
tooltip: translate('app_bar.settings_tooltip'),
onPressed: () async {
2024-02-11 23:18:20 -05:00
await GoRouterHelper(context).push('/settings');
2023-08-17 18:10:24 -04:00
})
]),
2023-07-24 09:31:51 -04:00
body: _newAccountForm(
context,
onSubmit: (formKey) async {
2024-04-10 16:13:08 -04:00
// dismiss the keyboard by unfocusing the textfield
2023-07-24 09:31:51 -04:00
FocusScope.of(context).unfocus();
2024-04-10 16:13:08 -04:00
2023-07-25 01:04:34 -04:00
try {
2024-01-04 22:29:43 -05:00
final name =
_formKey.currentState!.fields[formFieldName]!.value as String;
final pronouns = _formKey.currentState!.fields[formFieldPronouns]!
.value as String? ??
'';
final newProfileSpec =
NewProfileSpec(name: name, pronouns: pronouns);
2024-06-10 10:04:03 -04:00
final superSecret = await AccountRepository.instance
2024-06-07 14:42:04 -04:00
.createWithNewSuperIdentity(newProfileSpec);
2024-06-10 10:04:03 -04:00
GoRouterHelper(context).pushReplacement('/new_account/recovery_key',
extra: superSecret);
2023-07-26 15:58:38 -04:00
} on Exception catch (e) {
2023-09-26 18:46:02 -04:00
if (context.mounted) {
await showErrorModal(context, translate('new_account_page.error'),
'Exception: $e');
}
2023-07-25 01:04:34 -04:00
}
2023-07-24 09:31:51 -04:00
},
).paddingSymmetric(horizontal: 24, vertical: 8),
2023-07-25 01:04:34 -04:00
).withModalHUD(context, displayModalHUD);
2023-01-10 21:04:18 -05:00
}
2023-07-26 15:58:38 -04:00
2023-07-26 14:20:29 -04:00
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(DiagnosticsProperty<bool>('isInAsyncCall', isInAsyncCall));
}
2023-01-10 21:04:18 -05:00
}