veilidchat/lib/account_manager/views/new_account_page.dart

124 lines
3.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';
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';
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';
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
State createState() => _NewAccountPageState();
2023-07-24 09:31:51 -04:00
}
class _NewAccountPageState extends State<NewAccountPage> {
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;
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
2024-07-03 20:59:54 -04:00
Future<void> _onSubmit(GlobalKey<FormBuilderState> formKey) async {
// dismiss the keyboard by unfocusing the textfield
FocusScope.of(context).unfocus();
try {
final name = formKey
.currentState!.fields[EditProfileForm.formFieldName]!.value as String;
final pronouns = formKey.currentState!
.fields[EditProfileForm.formFieldPronouns]!.value as String? ??
'';
final newProfile = proto.Profile()
..name = name
..pronouns = pronouns;
setState(() {
_isInAsyncCall = true;
});
try {
2024-07-08 21:29:52 -04:00
final writableSuperIdentity = await AccountRepository.instance
2024-07-03 20:59:54 -04:00
.createWithNewSuperIdentity(newProfile);
2024-07-08 21:29:52 -04:00
GoRouterHelper(context).pushReplacement('/new_account/recovery_key',
extra: [writableSuperIdentity, newProfile.name]);
2024-07-03 20:59:54 -04:00
} finally {
if (mounted) {
setState(() {
_isInAsyncCall = false;
});
}
}
} on Exception catch (e) {
if (mounted) {
await showErrorModal(
context, translate('new_account_page.error'), 'Exception: $e');
}
}
}
2023-07-24 09:31:51 -04:00
@override
Widget build(BuildContext context) {
final displayModalHUD = _isInAsyncCall;
2023-07-23 23:13:21 -04:00
2024-07-06 20:09:18 -04:00
return StyledScaffold(
2023-08-17 18:10:24 -04:00
appBar: DefaultAppBar(
title: Text(translate('new_account_page.titlebar')),
2024-06-15 00:01:08 -04:00
leading: Navigator.canPop(context)
? IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: () {
Navigator.pop(context);
},
)
: null,
2023-08-17 18:10:24 -04:00
actions: [
const SignalStrengthMeterWidget(),
IconButton(
icon: const Icon(Icons.settings),
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
})
]),
2024-07-08 21:29:52 -04:00
body: SingleChildScrollView(
child: _newAccountForm(
2023-07-24 09:31:51 -04:00
context,
2024-07-03 20:59:54 -04:00
onSubmit: _onSubmit,
2024-07-08 21:29:52 -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
}
}