2021-04-17 08:10:31 +00:00
|
|
|
import 'package:comunic/helpers/account_helper.dart';
|
|
|
|
import 'package:comunic/helpers/server_config_helper.dart';
|
|
|
|
import 'package:comunic/models/new_account.dart';
|
|
|
|
import 'package:comunic/utils/input_utils.dart';
|
|
|
|
import 'package:comunic/utils/intl_utils.dart';
|
|
|
|
import 'package:comunic/utils/ui_utils.dart';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
|
|
|
|
|
|
|
import 'new_password_input_widget.dart';
|
|
|
|
|
|
|
|
/// Create account widget
|
|
|
|
///
|
|
|
|
/// @author Pierre Hubert
|
|
|
|
|
|
|
|
class CreateAccountWidget extends StatefulWidget {
|
|
|
|
final void Function() onCreated;
|
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
const CreateAccountWidget({Key? key, required this.onCreated})
|
2021-04-17 08:10:31 +00:00
|
|
|
: super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
_CreateAccountWidgetState createState() => _CreateAccountWidgetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _CreateAccountWidgetState extends State<CreateAccountWidget> {
|
|
|
|
final _firstNameController = TextEditingController();
|
|
|
|
final _lastNameController = TextEditingController();
|
|
|
|
final _emailController = TextEditingController();
|
|
|
|
final _passwordInputKey = GlobalKey<NewPasswordInputWidgetState>();
|
|
|
|
final _verifyPasswordController = TextEditingController();
|
2022-03-10 18:39:57 +00:00
|
|
|
bool? _acceptedTOS = false;
|
2021-04-17 08:10:31 +00:00
|
|
|
|
|
|
|
bool _isCreating = false;
|
2022-03-10 18:39:57 +00:00
|
|
|
CreateAccountResult? _createAccountResult;
|
2021-04-17 08:10:31 +00:00
|
|
|
|
|
|
|
bool _showErrors = false;
|
|
|
|
|
|
|
|
bool get _isFirstNameValid =>
|
|
|
|
_firstNameController.text.length >=
|
2022-03-10 18:39:57 +00:00
|
|
|
srvConfig!.accountInformationPolicy.minFirstNameLength;
|
2021-04-17 08:10:31 +00:00
|
|
|
|
|
|
|
bool get _isLastNameValid =>
|
|
|
|
_lastNameController.text.length >=
|
2022-03-10 18:39:57 +00:00
|
|
|
srvConfig!.accountInformationPolicy.minLastNameLength;
|
2021-04-17 08:10:31 +00:00
|
|
|
|
|
|
|
bool get _isEmailValid => validateEmail(_emailController.text);
|
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
bool get _isPasswordValid => _passwordInputKey.currentState!.valid;
|
2021-04-17 08:10:31 +00:00
|
|
|
|
|
|
|
bool get _isPasswordConfirmationValid =>
|
|
|
|
_passwordInputKey.currentState != null &&
|
2022-03-10 18:39:57 +00:00
|
|
|
_passwordInputKey.currentState!.value == _verifyPasswordController.text;
|
2021-04-17 08:10:31 +00:00
|
|
|
|
|
|
|
bool get _isFormValid =>
|
|
|
|
_isFirstNameValid &&
|
|
|
|
_isLastNameValid &&
|
|
|
|
_isEmailValid &&
|
|
|
|
_isPasswordValid &&
|
|
|
|
_isPasswordConfirmationValid &&
|
2022-03-10 18:39:57 +00:00
|
|
|
_acceptedTOS!;
|
2021-04-17 08:10:31 +00:00
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
String? get errorMessage => _createAccountResult ==
|
2021-04-17 08:10:31 +00:00
|
|
|
CreateAccountResult.ERROR_EXISTING_EMAIL
|
|
|
|
? tr("An account is already associated to this email address!")
|
|
|
|
: _createAccountResult == CreateAccountResult.ERROR_TOO_MANY_REQUESTS
|
|
|
|
? tr(
|
|
|
|
"Too many accounts have been created from this IP address for now. Please try again later.")
|
|
|
|
: tr(
|
|
|
|
"An error occurred while creating your account. Please try again.");
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
if (_isCreating)
|
|
|
|
return Center(
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
);
|
|
|
|
|
|
|
|
return Center(
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
child: ConstrainedBox(
|
|
|
|
constraints: BoxConstraints(maxWidth: 370),
|
|
|
|
child: ListView(
|
|
|
|
children: <Widget>[
|
|
|
|
_createAccountResult != null
|
|
|
|
? buildErrorCard(errorMessage)
|
|
|
|
: Container(),
|
|
|
|
|
|
|
|
// First name
|
|
|
|
_InputEntry(
|
|
|
|
controller: _firstNameController,
|
2022-03-10 18:39:57 +00:00
|
|
|
label: tr("First name")!,
|
2021-04-17 08:10:31 +00:00
|
|
|
onEdited: _updateUI,
|
|
|
|
icon: Icon(Icons.perm_identity),
|
|
|
|
maxLength:
|
2022-03-10 18:39:57 +00:00
|
|
|
srvConfig!.accountInformationPolicy.maxFirstNameLength,
|
2021-04-17 08:10:31 +00:00
|
|
|
error: _showErrors && !_isFirstNameValid
|
|
|
|
? tr("Invalid first name!")
|
|
|
|
: null,
|
|
|
|
),
|
|
|
|
|
|
|
|
// Last name
|
|
|
|
_InputEntry(
|
|
|
|
controller: _lastNameController,
|
2022-03-10 18:39:57 +00:00
|
|
|
label: tr("Last name")!,
|
2021-04-17 08:10:31 +00:00
|
|
|
onEdited: _updateUI,
|
|
|
|
icon: Icon(Icons.perm_identity),
|
2022-03-11 15:40:56 +00:00
|
|
|
maxLength:
|
|
|
|
srvConfig!.accountInformationPolicy.maxLastNameLength,
|
2021-04-17 08:10:31 +00:00
|
|
|
error: _showErrors && !_isLastNameValid
|
|
|
|
? tr("Invalid last name!")
|
|
|
|
: null,
|
|
|
|
),
|
|
|
|
|
|
|
|
// Email address
|
|
|
|
_InputEntry(
|
|
|
|
controller: _emailController,
|
2022-03-10 18:39:57 +00:00
|
|
|
label: tr("Email address")!,
|
2021-04-17 08:10:31 +00:00
|
|
|
onEdited: _updateUI,
|
|
|
|
icon: Icon(Icons.email),
|
|
|
|
keyboard: TextInputType.emailAddress,
|
|
|
|
error: _showErrors && !_isEmailValid
|
|
|
|
? tr("Invalid email address!")
|
|
|
|
: null,
|
|
|
|
),
|
|
|
|
|
|
|
|
// Password
|
|
|
|
NewPasswordInputWidget(
|
|
|
|
key: _passwordInputKey,
|
2022-03-10 18:39:57 +00:00
|
|
|
label: tr("Password")!,
|
2021-04-17 08:10:31 +00:00
|
|
|
onEdited: _updateUI,
|
|
|
|
icon: Icon(Icons.lock),
|
|
|
|
user: UserInfoForPassword(
|
|
|
|
firstName: _firstNameController.text,
|
|
|
|
lastName: _lastNameController.text,
|
|
|
|
email: _emailController.text,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
|
|
|
|
// Verify password
|
|
|
|
_InputEntry(
|
|
|
|
controller: _verifyPasswordController,
|
2022-03-10 18:39:57 +00:00
|
|
|
label: tr("Confirm your password")!,
|
2021-04-17 08:10:31 +00:00
|
|
|
onEdited: _updateUI,
|
|
|
|
icon: Icon(Icons.lock_outline),
|
|
|
|
isPassword: true,
|
|
|
|
error: _showErrors && !_isPasswordConfirmationValid
|
|
|
|
? tr("The password and its confirmation do not match!")
|
|
|
|
: null,
|
|
|
|
),
|
|
|
|
|
2021-04-17 09:16:21 +00:00
|
|
|
SizedBox(height: 10),
|
|
|
|
|
2021-04-17 08:10:31 +00:00
|
|
|
// TOS
|
|
|
|
CheckboxListTile(
|
|
|
|
title:
|
2022-03-10 18:39:57 +00:00
|
|
|
Text(tr("I have read and accepted the Terms Of Service.")!),
|
2021-04-17 08:10:31 +00:00
|
|
|
value: _acceptedTOS,
|
|
|
|
onChanged: (b) {
|
|
|
|
_acceptedTOS = b;
|
|
|
|
_updateUI();
|
|
|
|
},
|
2022-03-10 18:39:57 +00:00
|
|
|
subtitle: _showErrors && !_acceptedTOS!
|
2021-04-17 08:10:31 +00:00
|
|
|
? Text(
|
2022-03-10 18:39:57 +00:00
|
|
|
tr("You must accept the Terms Of Service to continue.")!,
|
2021-04-17 08:10:31 +00:00
|
|
|
style: TextStyle(color: Colors.redAccent),
|
|
|
|
)
|
|
|
|
: null,
|
|
|
|
controlAffinity: ListTileControlAffinity.leading,
|
|
|
|
secondary: IconButton(
|
|
|
|
icon: Icon(Icons.open_in_new),
|
|
|
|
onPressed: _openTOS,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
|
2021-04-17 09:16:21 +00:00
|
|
|
SizedBox(height: 10),
|
|
|
|
|
2021-04-17 08:10:31 +00:00
|
|
|
// Submit button
|
|
|
|
Center(
|
|
|
|
child: ElevatedButton(
|
|
|
|
onPressed: _submitForm,
|
2022-03-10 18:39:57 +00:00
|
|
|
child: Text(tr("Create account")!),
|
2021-04-17 08:10:31 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _updateUI() {
|
|
|
|
setState(() {
|
|
|
|
// Nothing yet
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void _submitForm() async {
|
|
|
|
if (!_isFormValid) {
|
|
|
|
_showErrors = true;
|
|
|
|
_updateUI();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setState(() {
|
|
|
|
_isCreating = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
final result = await AccountHelper().createAccount(NewAccount(
|
|
|
|
firstName: _firstNameController.text,
|
|
|
|
lastName: _lastNameController.text,
|
|
|
|
email: _emailController.text,
|
2022-03-10 18:39:57 +00:00
|
|
|
password: _passwordInputKey.currentState!.value,
|
2021-04-17 08:10:31 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
setState(() {
|
|
|
|
_isCreating = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (result != CreateAccountResult.SUCCESS) {
|
|
|
|
_createAccountResult = result;
|
|
|
|
_showCreateAccountError();
|
|
|
|
_updateUI();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-17 08:11:31 +00:00
|
|
|
widget.onCreated();
|
2021-04-17 08:10:31 +00:00
|
|
|
}
|
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
void _openTOS() => launch(ServerConfigurationHelper.config!.termsURL);
|
2021-04-17 08:10:31 +00:00
|
|
|
|
|
|
|
void _showCreateAccountError() async {
|
|
|
|
await showCupertinoDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (c) => CupertinoAlertDialog(
|
2022-03-10 18:39:57 +00:00
|
|
|
title: Text(tr("Error while creating your account")!),
|
|
|
|
content: Text(errorMessage!),
|
2021-04-17 08:10:31 +00:00
|
|
|
actions: <Widget>[
|
|
|
|
CupertinoButton(
|
2022-03-10 18:39:57 +00:00
|
|
|
child: Text(tr("Ok")!.toUpperCase()),
|
2021-04-17 08:10:31 +00:00
|
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class _InputEntry extends StatelessWidget {
|
|
|
|
final TextEditingController controller;
|
|
|
|
final String label;
|
|
|
|
final VoidCallback onEdited;
|
|
|
|
final bool isPassword;
|
2022-03-10 18:39:57 +00:00
|
|
|
final String? error;
|
|
|
|
final Widget? icon;
|
|
|
|
final TextInputType? keyboard;
|
|
|
|
final int? maxLength;
|
2021-04-17 08:10:31 +00:00
|
|
|
|
|
|
|
const _InputEntry({
|
2022-03-10 18:39:57 +00:00
|
|
|
Key? key,
|
|
|
|
required this.controller,
|
|
|
|
required this.label,
|
|
|
|
required this.onEdited,
|
2021-04-17 08:10:31 +00:00
|
|
|
this.isPassword = false,
|
|
|
|
this.error,
|
|
|
|
this.icon,
|
|
|
|
this.keyboard,
|
|
|
|
this.maxLength,
|
2022-03-11 15:40:56 +00:00
|
|
|
}) : super(key: key);
|
2021-04-17 08:10:31 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return TextField(
|
|
|
|
controller: controller,
|
|
|
|
onChanged: (s) => onEdited(),
|
|
|
|
keyboardType: keyboard,
|
|
|
|
obscureText: isPassword,
|
|
|
|
maxLength: maxLength,
|
|
|
|
decoration: InputDecoration(
|
2021-04-17 08:59:06 +00:00
|
|
|
counterText: "",
|
2021-04-17 08:10:31 +00:00
|
|
|
alignLabelWithHint: true,
|
|
|
|
errorText: error,
|
|
|
|
labelText: label,
|
|
|
|
icon: icon,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|