134 lines
4.3 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';
2021-12-07 23:01:23 +05:30
import 'package:easy_localization/easy_localization.dart';
2022-06-15 20:04:11 +08:00
import 'package:flowy_sdk/protobuf/flowy-error-code/code.pb.dart';
2022-07-04 15:07:11 +08:00
import 'package:flowy_sdk/protobuf/flowy-user/protobuf.dart' show UserProfile;
2021-12-14 18:04:51 +08:00
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
2021-07-13 13:14:49 +08:00
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
2021-12-07 23:01:23 +05:30
import 'package:app_flowy/generated/locale_keys.g.dart';
2021-07-13 13:14:49 +08:00
2021-09-05 22:52:20 +08:00
part 'sign_up_bloc.freezed.dart';
2021-07-13 13:14:49 +08:00
2021-09-05 22:52:20 +08:00
class SignUpBloc extends Bloc<SignUpEvent, SignUpState> {
final AuthService authService;
SignUpBloc(this.authService) : super(SignUpState.initial()) {
2022-01-04 22:44:03 +08:00
on<SignUpEvent>((event, emit) async {
await event.map(signUpWithUserEmailAndPassword: (e) async {
await _performActionOnSignUp(emit);
2022-03-25 15:02:43 +08:00
}, emailChanged: (_EmailChanged value) async {
2022-01-04 22:44:03 +08:00
emit(state.copyWith(email: value.email, emailError: none(), successOrFail: none()));
2022-03-25 15:02:43 +08:00
}, passwordChanged: (_PasswordChanged value) async {
2022-01-04 22:44:03 +08:00
emit(state.copyWith(password: value.password, passwordError: none(), successOrFail: none()));
2022-03-25 15:02:43 +08:00
}, repeatPasswordChanged: (_RepeatPasswordChanged value) async {
2022-01-04 22:44:03 +08:00
emit(state.copyWith(repeatedPassword: value.password, repeatPasswordError: none(), successOrFail: none()));
});
2021-09-06 16:18:34 +08:00
});
2021-07-13 13:14:49 +08:00
}
2022-01-04 22:44:03 +08:00
Future<void> _performActionOnSignUp(Emitter<SignUpState> emit) async {
emit(state.copyWith(
2021-09-06 16:18:34 +08:00
isSubmitting: true,
2021-10-09 16:43:56 +08:00
successOrFail: none(),
2022-01-04 22:44:03 +08:00
));
2021-09-06 16:18:34 +08:00
final password = state.password;
final repeatedPassword = state.repeatedPassword;
if (password == null) {
2022-01-04 22:44:03 +08:00
emit(state.copyWith(
2021-09-06 16:18:34 +08:00
isSubmitting: false,
2021-12-07 23:01:23 +05:30
passwordError: some(LocaleKeys.signUp_emptyPasswordError.tr()),
2022-01-04 22:44:03 +08:00
));
2021-09-06 16:18:34 +08:00
return;
}
if (repeatedPassword == null) {
2022-01-04 22:44:03 +08:00
emit(state.copyWith(
2021-09-06 16:18:34 +08:00
isSubmitting: false,
2021-12-07 23:01:23 +05:30
repeatPasswordError: some(LocaleKeys.signUp_repeatPasswordEmptyError.tr()),
2022-01-04 22:44:03 +08:00
));
2021-09-06 16:18:34 +08:00
return;
}
2021-07-13 13:14:49 +08:00
2021-09-06 16:18:34 +08:00
if (password != repeatedPassword) {
2022-01-04 22:44:03 +08:00
emit(state.copyWith(
2021-09-06 16:18:34 +08:00
isSubmitting: false,
2021-12-07 23:01:23 +05:30
repeatPasswordError: some(LocaleKeys.signUp_unmatchedPasswordError.tr()),
2022-01-04 22:44:03 +08:00
));
2021-09-06 16:18:34 +08:00
return;
}
2022-01-04 22:44:03 +08:00
emit(state.copyWith(
2021-09-06 16:18:34 +08:00
passwordError: none(),
repeatPasswordError: none(),
2022-01-04 22:44:03 +08:00
));
2021-09-06 16:18:34 +08:00
final result = await authService.signUp(
2022-01-31 08:15:49 +08:00
name: state.email,
password: state.password,
email: state.email,
);
2022-01-04 22:44:03 +08:00
emit(result.fold(
2021-11-08 23:15:29 +08:00
(profile) => state.copyWith(
2021-09-06 16:18:34 +08:00
isSubmitting: false,
2021-11-08 23:15:29 +08:00
successOrFail: some(left(profile)),
2021-09-06 16:18:34 +08:00
emailError: none(),
passwordError: none(),
repeatPasswordError: none(),
),
(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
SignUpState stateFromCode(FlowyError error) {
switch (ErrorCode.valueOf(error.code)!) {
case ErrorCode.EmailFormatInvalid:
return state.copyWith(
2021-10-09 16:43:56 +08:00
isSubmitting: false,
emailError: some(error.msg),
passwordError: none(),
successOrFail: none(),
);
case ErrorCode.PasswordFormatInvalid:
return state.copyWith(
2021-10-09 16:43:56 +08:00
isSubmitting: false,
passwordError: some(error.msg),
emailError: none(),
successOrFail: none(),
);
default:
return state.copyWith(isSubmitting: false, successOrFail: some(right(error)));
}
}
2021-07-13 13:14:49 +08:00
}
@freezed
class SignUpEvent with _$SignUpEvent {
const factory SignUpEvent.signUpWithUserEmailAndPassword() = SignUpWithUserEmailAndPassword;
2022-03-25 15:02:43 +08:00
const factory SignUpEvent.emailChanged(String email) = _EmailChanged;
const factory SignUpEvent.passwordChanged(String password) = _PasswordChanged;
const factory SignUpEvent.repeatPasswordChanged(String password) = _RepeatPasswordChanged;
}
@freezed
class SignUpState with _$SignUpState {
2021-09-05 22:52:20 +08:00
const factory SignUpState({
String? email,
String? password,
2021-09-06 16:18:34 +08:00
String? repeatedPassword,
required bool isSubmitting,
required Option<String> passwordError,
2021-09-06 16:18:34 +08:00
required Option<String> repeatPasswordError,
required Option<String> emailError,
2021-12-14 18:04:51 +08:00
required Option<Either<UserProfile, FlowyError>> successOrFail,
2021-09-05 22:52:20 +08:00
}) = _SignUpState;
2021-09-05 22:52:20 +08:00
factory SignUpState.initial() => SignUpState(
isSubmitting: false,
passwordError: none(),
2021-09-06 16:18:34 +08:00
repeatPasswordError: none(),
emailError: none(),
successOrFail: none(),
);
}