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';
|
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-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';
|
2024-06-13 14:52:34 -04:00
|
|
|
import '../../proto/proto.dart' as proto;
|
2024-06-07 14:42:04 -04:00
|
|
|
import '../../theme/theme.dart';
|
|
|
|
import '../../tools/tools.dart';
|
|
|
|
import '../../veilid_processor/veilid_processor.dart';
|
|
|
|
import '../account_manager.dart';
|
2024-06-13 14:52:34 -04:00
|
|
|
import 'profile_edit_form.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
|
2024-06-13 14:52:34 -04:00
|
|
|
State createState() => _NewAccountPageState();
|
2023-07-24 09:31:51 -04:00
|
|
|
}
|
|
|
|
|
2024-06-13 14:52:34 -04:00
|
|
|
class _NewAccountPageState extends State<NewAccountPage> {
|
2023-07-24 09:31:51 -04:00
|
|
|
final _formKey = GlobalKey<FormBuilderState>();
|
2024-06-13 14:52:34 -04:00
|
|
|
bool _isInAsyncCall = false;
|
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;
|
|
|
|
|
2024-06-13 14:52:34 -04:00
|
|
|
return EditProfileForm(
|
|
|
|
header: translate('new_account_page.header'),
|
|
|
|
instructions: translate('new_account_page.instructions'),
|
|
|
|
submitText: translate('new_account_page.create'),
|
|
|
|
submitDisabledText: translate('button.waiting_for_network'),
|
|
|
|
onSubmit: !canSubmit ? null : onSubmit);
|
2024-06-07 14:42:04 -04:00
|
|
|
}
|
2023-07-24 09:31:51 -04:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-06-13 14:52:34 -04: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),
|
2024-06-13 14:52:34 -04:00
|
|
|
tooltip: translate('menu.settings_tooltip'),
|
2023-08-17 18:10:24 -04:00
|
|
|
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-06-13 14:52:34 -04:00
|
|
|
final name = _formKey.currentState!
|
|
|
|
.fields[EditProfileForm.formFieldName]!.value as String;
|
|
|
|
final pronouns = _formKey
|
|
|
|
.currentState!
|
|
|
|
.fields[EditProfileForm.formFieldPronouns]!
|
2024-01-04 22:29:43 -05:00
|
|
|
.value as String? ??
|
|
|
|
'';
|
2024-06-13 14:52:34 -04:00
|
|
|
final newProfile = proto.Profile()
|
|
|
|
..name = name
|
|
|
|
..pronouns = pronouns;
|
2024-01-04 22:29:43 -05:00
|
|
|
|
2024-06-13 14:52:34 -04:00
|
|
|
setState(() {
|
|
|
|
_isInAsyncCall = true;
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
final superSecret = await AccountRepository.instance
|
|
|
|
.createWithNewSuperIdentity(newProfile);
|
|
|
|
GoRouterHelper(context).pushReplacement(
|
|
|
|
'/new_account/recovery_key',
|
|
|
|
extra: superSecret);
|
|
|
|
} finally {
|
|
|
|
if (mounted) {
|
|
|
|
setState(() {
|
|
|
|
_isInAsyncCall = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|