veilidchat/lib/pages/new_account.dart

176 lines
6.0 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';
2023-07-26 14:20:29 -04:00
import 'package:flutter_form_builder/flutter_form_builder.dart';
2023-01-10 21:04:18 -05:00
import 'package:flutter_riverpod/flutter_riverpod.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
import '../components/default_app_bar.dart';
2023-08-17 18:10:24 -04:00
import '../components/signal_strength_meter.dart';
2023-09-24 22:35:54 -04:00
import '../entities/entities.dart';
2023-07-24 09:31:51 -04:00
import '../providers/local_accounts.dart';
2023-07-25 01:04:34 -04:00
import '../providers/logins.dart';
2023-07-28 20:36:05 -04:00
import '../providers/window_control.dart';
2023-07-23 23:13:21 -04:00
import '../tools/tools.dart';
2023-07-25 01:04:34 -04:00
import '../veilid_support/veilid_support.dart';
2023-01-10 21:04:18 -05:00
2023-07-24 09:31:51 -04:00
class NewAccountPage extends ConsumerStatefulWidget {
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
}
class NewAccountPageState extends ConsumerState<NewAccountPage> {
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 {
setState(() {});
await ref.read(windowControlProvider.notifier).changeWindowSetup(
TitleBarStyle.normal, OrientationCapability.portraitOnly);
});
}
2023-08-01 00:39:50 -04:00
/// Creates a new master identity, an account associated with the master
/// identity, stores the account in the identity key and then logs into
/// that account with no password set at this time
2023-07-25 01:04:34 -04:00
Future<void> createAccount() async {
2023-08-01 00:39:50 -04:00
final localAccounts = ref.read(localAccountsProvider.notifier);
final logins = ref.read(loginsProvider.notifier);
final name = _formKey.currentState!.fields[formFieldName]!.value as String;
2023-09-28 10:06:22 -04:00
final pronouns =
2023-09-30 21:15:26 -04:00
_formKey.currentState!.fields[formFieldPronouns]!.value as String? ??
'';
2023-07-25 01:04:34 -04:00
2023-08-01 00:39:50 -04:00
final imws = await IdentityMasterWithSecrets.create();
try {
final localAccount = await localAccounts.newLocalAccount(
2023-07-25 01:04:34 -04:00
identityMaster: imws.identityMaster,
identitySecret: imws.identitySecret,
2023-08-01 00:39:50 -04:00
name: name,
2023-09-28 10:06:22 -04:00
pronouns: pronouns);
2023-07-25 01:04:34 -04:00
// Log in the new account by default with no pin
2023-09-24 22:35:54 -04:00
final ok = await logins.login(localAccount.identityMaster.masterRecordKey,
EncryptionKeyType.none, '');
assert(ok, 'login with none should never fail');
2023-07-26 15:58:38 -04:00
} on Exception catch (_) {
2023-07-25 01:04:34 -04:00
await imws.delete();
rethrow;
}
}
2023-07-24 09:31:51 -04:00
Widget _newAccountForm(BuildContext context,
2023-07-26 15:58:38 -04:00
{required Future<void> Function(GlobalKey<FormBuilderState>)
onSubmit}) =>
FormBuilder(
key: _formKey,
child: ListView(
children: [
Text(translate('new_account_page.header'))
.textStyle(context.headlineSmall)
.paddingSymmetric(vertical: 16),
FormBuilderTextField(
autofocus: true,
name: formFieldName,
decoration:
2023-09-28 10:06:22 -04:00
InputDecoration(labelText: translate('account.form_name')),
2023-07-26 15:58:38 -04:00
maxLength: 64,
// The validator receives the text that the user has entered.
validator: FormBuilderValidators.compose([
FormBuilderValidators.required(),
]),
),
FormBuilderTextField(
2023-09-28 10:06:22 -04:00
name: formFieldPronouns,
2023-07-26 15:58:38 -04:00
maxLength: 64,
2023-09-28 10:06:22 -04:00
decoration: InputDecoration(
labelText: translate('account.form_pronouns')),
2023-07-26 15:58:38 -04:00
),
Row(children: [
const Spacer(),
Text(translate('new_account_page.instructions'))
.toCenter()
.flexible(flex: 6),
const Spacer(),
]).paddingSymmetric(vertical: 4),
ElevatedButton(
onPressed: () async {
if (_formKey.currentState?.saveAndValidate() ?? false) {
2023-07-24 09:31:51 -04:00
setState(() {
2023-07-26 15:58:38 -04:00
isInAsyncCall = true;
2023-07-24 09:31:51 -04:00
});
2023-07-26 15:58:38 -04:00
try {
await onSubmit(_formKey);
} finally {
2023-08-04 01:00:38 -04:00
if (mounted) {
setState(() {
isInAsyncCall = false;
});
}
2023-07-26 15:58:38 -04:00
}
2023-07-24 09:31:51 -04:00
}
2023-07-26 15:58:38 -04:00
},
child: Text(translate('new_account_page.create')),
).paddingSymmetric(vertical: 4).alignAtCenterRight(),
],
),
);
2023-07-24 09:31:51 -04:00
@override
Widget build(BuildContext context) {
2023-07-28 20:36:05 -04:00
ref.watch(windowControlProvider);
2023-07-24 09:31:51 -04:00
2023-07-26 22:38:09 -04:00
final localAccounts = ref.watch(localAccountsProvider);
final logins = ref.watch(loginsProvider);
final displayModalHUD =
isInAsyncCall || !localAccounts.hasValue || !logins.hasValue;
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 {
context.go('/new_account/settings');
})
]),
2023-07-24 09:31:51 -04:00
body: _newAccountForm(
context,
onSubmit: (formKey) async {
FocusScope.of(context).unfocus();
2023-07-25 01:04:34 -04:00
try {
await createAccount();
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
}