54 lines
1.7 KiB
Dart
54 lines
1.7 KiB
Dart
import 'package:aitrainer_app/model/cache.dart';
|
|
import 'package:aitrainer_app/repository/user_repository.dart';
|
|
import 'package:aitrainer_app/util/common.dart';
|
|
import 'package:flutter_form_bloc/flutter_form_bloc.dart';
|
|
import 'account/account_bloc.dart';
|
|
|
|
class RegistrationFormBloc extends FormBloc<String, String> with Common {
|
|
final AccountBloc accountBloc;
|
|
final emailField = TextFieldBloc(
|
|
validators: [
|
|
FieldBlocValidators.required,
|
|
],
|
|
);
|
|
final passwordField = TextFieldBloc(validators: [
|
|
FieldBlocValidators.required,
|
|
]);
|
|
final UserRepository userRepository;
|
|
|
|
RegistrationFormBloc({this.userRepository, this.accountBloc}) {
|
|
addFieldBlocs(fieldBlocs: [emailField, passwordField]);
|
|
|
|
emailField.onValueChanges(onData: (previous, current) async* {
|
|
userRepository.setEmail(current.value);
|
|
});
|
|
|
|
passwordField.onValueChanges(onData: (previous, current) async* {
|
|
userRepository.setPassword(current.value);
|
|
});
|
|
}
|
|
|
|
@override
|
|
void onSubmitting() async {
|
|
try {
|
|
emitLoading(progress: 30);
|
|
if (!validateEmail(userRepository)) {
|
|
emailField.addFieldError(EMAIL_ERROR, isPermanent: true);
|
|
|
|
emitFailure(failureResponse: EMAIL_ERROR);
|
|
} else if (!validatePassword(userRepository)) {
|
|
passwordField.addFieldError(PASSWORD_ERROR, isPermanent: true);
|
|
emitFailure(failureResponse: PASSWORD_ERROR);
|
|
} else {
|
|
// Emit either Loaded or Error
|
|
await userRepository.addUser();
|
|
emitSuccess(canSubmitAgain: false);
|
|
accountBloc.add(AccountLogInFinished(customer: Cache().userLoggedIn));
|
|
Cache().initBadges();
|
|
}
|
|
} on Exception catch (ex) {
|
|
emitFailure(failureResponse: ex.toString());
|
|
}
|
|
}
|
|
}
|