veilidchat/lib/pages/new_account.dart

148 lines
4.9 KiB
Dart
Raw Normal View History

2023-07-23 23:13:21 -04:00
import 'package:awesome_extensions/awesome_extensions.dart';
2023-01-10 21:04:18 -05:00
import 'package:flutter/material.dart';
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:flutter_form_builder/flutter_form_builder.dart';
import 'package:form_builder_validators/form_builder_validators.dart';
2023-07-25 01:04:34 -04:00
import 'package:quickalert/quickalert.dart';
2023-07-23 23:13:21 -04:00
import '../components/default_app_bar.dart';
2023-07-25 01:04:34 -04:00
import '../entities/proto.dart' as proto;
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-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});
static const path = '/new_account';
@override
2023-07-24 09:31:51 -04:00
NewAccountPageState createState() {
return NewAccountPageState();
}
}
class NewAccountPageState extends ConsumerState<NewAccountPage> {
final _formKey = GlobalKey<FormBuilderState>();
late bool isInAsyncCall = false;
2023-07-25 01:04:34 -04:00
static const String formFieldName = "name";
static const String formFieldTitle = "title";
Future<void> createAccount() async {
final imws = await newIdentityMaster();
try {
final localAccounts = ref.read(localAccountsProvider.notifier);
final logins = ref.read(loginsProvider.notifier);
final profile = proto.Profile();
profile.name = _formKey.currentState!.fields[formFieldName]!.value;
profile.title = _formKey.currentState!.fields[formFieldTitle]!.value;
final account = proto.Account();
account.profile = profile;
final localAccount = await localAccounts.newAccount(
identityMaster: imws.identityMaster,
identitySecret: imws.identitySecret,
account: account);
// Log in the new account by default with no pin
final ok = await logins
.loginWithNone(localAccount.identityMaster.masterRecordKey);
assert(ok == true);
} catch (e) {
await imws.delete();
rethrow;
}
}
2023-07-24 09:31:51 -04:00
Widget _newAccountForm(BuildContext context,
{required Future<void> Function(GlobalKey<FormBuilderState>) onSubmit}) {
return FormBuilder(
key: _formKey,
child: ListView(
children: [
Text(translate("new_account_page.header"))
.textStyle(context.headlineSmall)
.paddingSymmetric(vertical: 16),
FormBuilderTextField(
autofocus: true,
2023-07-25 01:04:34 -04:00
name: formFieldName,
decoration:
InputDecoration(hintText: translate("account.form_name")),
2023-07-24 09:31:51 -04:00
maxLength: 64,
// The validator receives the text that the user has entered.
validator: FormBuilderValidators.compose([
FormBuilderValidators.required(),
]),
),
FormBuilderTextField(
2023-07-25 01:04:34 -04:00
name: formFieldTitle,
2023-07-24 09:31:51 -04:00
maxLength: 64,
2023-07-25 01:04:34 -04:00
decoration:
InputDecoration(hintText: translate("account.form_title")),
2023-07-24 09:31:51 -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) {
setState(() {
isInAsyncCall = true;
});
try {
await onSubmit(_formKey);
} finally {
setState(() {
isInAsyncCall = false;
});
}
}
},
child: Text(translate('new_account_page.create')),
).paddingSymmetric(vertical: 4).alignAtCenterRight(),
],
),
);
}
@override
Widget build(BuildContext context) {
2023-07-23 23:13:21 -04:00
enableTitleBar(true);
2023-07-24 09:31:51 -04:00
portraitOnly();
2023-07-25 01:04:34 -04:00
// final localAccountsData = ref.watch(localAccountsProvider);
final displayModalHUD = isInAsyncCall; // || !localAccountsData.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,
appBar: DefaultAppBar(context,
title: Text(translate("new_account_page.titlebar"))),
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();
} catch (e) {
QuickAlert.show(
context: context,
type: QuickAlertType.error,
title: translate("new_account_page.error"),
text: 'Exception: ${e.toString()}',
//backgroundColor: Colors.black,
//titleColor: Colors.white,
//textColor: Colors.white,
);
}
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
}
}