veilidchat/lib/pages/new_account.dart

176 lines
6.1 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';
static const String formFieldTitle = 'title';
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;
final title =
_formKey.currentState!.fields[formFieldTitle]!.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,
title: title);
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:
InputDecoration(hintText: translate('account.form_name')),
maxLength: 64,
// The validator receives the text that the user has entered.
validator: FormBuilderValidators.compose([
FormBuilderValidators.required(),
]),
),
FormBuilderTextField(
name: formFieldTitle,
maxLength: 64,
decoration:
InputDecoration(hintText: translate('account.form_title')),
),
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 {
debugPrint(_formKey.currentState?.value.toString());
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
}