48 lines
1.2 KiB
Dart
48 lines
1.2 KiB
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 ResetPasswordFormBloc extends FormBloc<String, String> with Common {
|
|
final UserRepository userRepository;
|
|
bool loading = false;
|
|
|
|
final emailField = TextFieldBloc(
|
|
validators: [
|
|
FieldBlocValidators.required,
|
|
],
|
|
);
|
|
|
|
ResetPasswordFormBloc({this.userRepository}) {
|
|
addFieldBlocs(fieldBlocs: [emailField]);
|
|
|
|
emailField.onValueChanges(onData: (previous, current) async* {
|
|
userRepository.setEmail(current.value);
|
|
});
|
|
}
|
|
|
|
@override
|
|
void onSubmitting() async {
|
|
try {
|
|
emitLoading(progress: 30);
|
|
loading = true;
|
|
if (!validateEmail(userRepository)) {
|
|
emailField.addFieldError(EMAIL_ERROR, isPermanent: true);
|
|
|
|
emitFailure(failureResponse: EMAIL_ERROR);
|
|
} else {
|
|
// Emit either Loaded or Error
|
|
await userRepository.resetPassword();
|
|
emitSuccess(canSubmitAgain: false);
|
|
}
|
|
loading = false;
|
|
} on Exception catch (ex) {
|
|
emitFailure(failureResponse: ex.toString());
|
|
}
|
|
}
|
|
|
|
Future<void> close() {
|
|
emailField.close();
|
|
return super.close();
|
|
}
|
|
}
|