82 lines
2.9 KiB
Dart
Raw Normal View History

import 'package:app_flowy/user/application/auth_service.dart';
2021-07-13 13:14:49 +08:00
import 'package:dartz/dartz.dart';
2022-06-15 20:04:11 +08:00
import 'package:flowy_sdk/protobuf/flowy-error-code/code.pb.dart';
2021-12-14 18:04:51 +08:00
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
2022-07-04 15:07:11 +08:00
import 'package:flowy_sdk/protobuf/flowy-user/protobuf.dart' show UserProfile;
2021-07-13 13:14:49 +08:00
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
part 'sign_in_bloc.freezed.dart';
class SignInBloc extends Bloc<SignInEvent, SignInState> {
final AuthService authService;
SignInBloc(this.authService) : super(SignInState.initial()) {
2022-01-04 22:44:03 +08:00
on<SignInEvent>((event, emit) async {
await event.map(
signedInWithUserEmailAndPassword: (e) async {
await _performActionOnSignIn(
state,
emit,
);
},
emailChanged: (EmailChanged value) async {
emit(state.copyWith(email: value.email, emailError: none(), successOrFail: none()));
},
passwordChanged: (PasswordChanged value) async {
emit(state.copyWith(password: value.password, passwordError: none(), successOrFail: none()));
},
);
});
2021-07-13 13:14:49 +08:00
}
2022-01-04 22:44:03 +08:00
Future<void> _performActionOnSignIn(SignInState state, Emitter<SignInState> emit) async {
emit(state.copyWith(isSubmitting: true, emailError: none(), passwordError: none(), successOrFail: none()));
2021-07-13 13:14:49 +08:00
final result = await authService.signIn(
2022-01-31 08:15:49 +08:00
email: state.email,
password: state.password,
);
2022-01-04 22:44:03 +08:00
emit(result.fold(
(userProfile) => state.copyWith(isSubmitting: false, successOrFail: some(left(userProfile))),
(error) => stateFromCode(error),
2022-01-04 22:44:03 +08:00
));
2021-07-13 13:14:49 +08:00
}
2021-12-14 18:04:51 +08:00
SignInState stateFromCode(FlowyError error) {
switch (ErrorCode.valueOf(error.code)!) {
case ErrorCode.EmailFormatInvalid:
return state.copyWith(isSubmitting: false, emailError: some(error.msg), passwordError: none());
case ErrorCode.PasswordFormatInvalid:
return state.copyWith(isSubmitting: false, passwordError: some(error.msg), emailError: none());
default:
return state.copyWith(isSubmitting: false, successOrFail: some(right(error)));
}
}
2021-07-13 13:14:49 +08:00
}
@freezed
2022-03-25 15:02:43 +08:00
class SignInEvent with _$SignInEvent {
const factory SignInEvent.signedInWithUserEmailAndPassword() = SignedInWithUserEmailAndPassword;
const factory SignInEvent.emailChanged(String email) = EmailChanged;
const factory SignInEvent.passwordChanged(String password) = PasswordChanged;
}
@freezed
2022-03-25 15:02:43 +08:00
class SignInState with _$SignInState {
const factory SignInState({
String? email,
String? password,
required bool isSubmitting,
required Option<String> passwordError,
required Option<String> emailError,
2021-12-14 18:04:51 +08:00
required Option<Either<UserProfile, FlowyError>> successOrFail,
}) = _SignInState;
factory SignInState.initial() => SignInState(
isSubmitting: false,
passwordError: none(),
emailError: none(),
successOrFail: none(),
);
}