veilidchat/lib/account_manager/views/new_account_page.dart

148 lines
4.6 KiB
Dart
Raw Normal View History

2024-07-11 23:04:08 -04:00
import 'dart:async';
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-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 '../../notifications/cubits/notifications_cubit.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';
2024-07-31 12:04:43 -05:00
import 'edit_profile_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
}
2024-07-11 23:04:08 -04:00
class _NewAccountPageState extends WindowSetupState<NewAccountPage> {
_NewAccountPageState()
: super(
titleBarStyle: TitleBarStyle.normal,
orientationCapability: OrientationCapability.portraitOnly);
2023-07-29 10:55:35 -04:00
Object _defaultAccountValues(String key) {
switch (key) {
case EditProfileForm.formFieldName:
return '';
case EditProfileForm.formFieldPronouns:
return '';
case EditProfileForm.formFieldAbout:
return '';
case EditProfileForm.formFieldAvailability:
return proto.Availability.AVAILABILITY_FREE;
case EditProfileForm.formFieldFreeMessage:
return '';
case EditProfileForm.formFieldAwayMessage:
return '';
case EditProfileForm.formFieldBusyMessage:
return '';
// case EditProfileForm.formFieldAvatar:
// return null;
case EditProfileForm.formFieldAutoAway:
return false;
case EditProfileForm.formFieldAutoAwayTimeout:
return '15';
default:
throw StateError('missing form element');
}
2024-06-07 14:42:04 -04:00
}
2023-07-24 09:31:51 -04:00
Widget _newAccountForm(
BuildContext context,
) =>
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'),
initialValueCallback: _defaultAccountValues,
onSubmit: _onSubmit);
2024-07-31 12:04:43 -05:00
Future<void> _onSubmit(AccountSpec accountSpec) async {
2024-07-03 20:59:54 -04:00
// dismiss the keyboard by unfocusing the textfield
FocusScope.of(context).unfocus();
try {
setState(() {
_isInAsyncCall = true;
});
try {
final networkReady = context
.read<ConnectionStateCubit>()
.state
.asData
?.value
.isPublicInternetReady ??
false;
final canSubmit = networkReady;
if (!canSubmit) {
context.read<NotificationsCubit>().error(
text: translate('new_account_page.network_is_offline'),
title: translate('new_account_page.error'));
return;
}
2024-07-08 21:29:52 -04:00
final writableSuperIdentity = await AccountRepository.instance
2024-07-31 12:04:43 -05:00
.createWithNewSuperIdentity(accountSpec);
2024-07-08 21:29:52 -04:00
GoRouterHelper(context).pushReplacement('/new_account/recovery_key',
2024-07-31 12:04:43 -05:00
extra: [writableSuperIdentity, accountSpec.name]);
2024-07-03 20:59:54 -04:00
} finally {
if (mounted) {
setState(() {
_isInAsyncCall = false;
});
}
}
2024-08-02 23:18:39 -05:00
} on Exception catch (e, st) {
2024-07-03 20:59:54 -04:00
if (mounted) {
2024-08-02 23:18:39 -05:00
await showErrorStacktraceModal(
context: context, error: e, stackTrace: st);
2024-07-03 20:59:54 -04:00
}
}
}
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-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
}
2024-07-11 23:04:08 -04:00
////////////////////////////////////////////////////////////////////////////
bool _isInAsyncCall = false;
2023-01-10 21:04:18 -05:00
}