62 lines
1.8 KiB
Dart
62 lines
1.8 KiB
Dart
import 'package:aitrainer_app/bloc/account/account_bloc.dart';
|
|
import 'package:aitrainer_app/model/auth.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';
|
|
|
|
class LoginFormBloc extends FormBloc<String, String> {
|
|
final AccountBloc accountBloc;
|
|
final UserRepository userRepository;
|
|
|
|
final emailField = TextFieldBloc(
|
|
validators: [
|
|
FieldBlocValidators.required,
|
|
],
|
|
);
|
|
final passwordField = TextFieldBloc(
|
|
validators: [
|
|
FieldBlocValidators.required,
|
|
]
|
|
);
|
|
|
|
|
|
LoginFormBloc({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 ( ! Common.validateEmail(userRepository)) {
|
|
emailField.addFieldError(Common.EMAIL_ERROR, isPermanent: true);
|
|
|
|
emitFailure(failureResponse: Common.EMAIL_ERROR);
|
|
} else if ( ! Common.validatePassword(userRepository)) {
|
|
passwordField.addFieldError(Common.PASSWORD_ERROR, isPermanent: true);
|
|
emitFailure(failureResponse: Common.PASSWORD_ERROR);
|
|
} else {
|
|
// Emit either Loaded or Error
|
|
await userRepository.getUser();
|
|
emitSuccess(canSubmitAgain: false);
|
|
accountBloc.add(AccountLogInFinished(customer: Auth().userLoggedIn));
|
|
}
|
|
} on Exception catch (ex) {
|
|
emitFailure(failureResponse: ex.toString());
|
|
|
|
}
|
|
}
|
|
}
|