132 lines
4.2 KiB
Dart
Raw Normal View History

2021-07-21 15:43:05 +08:00
import 'package:app_flowy/user/domain/i_auth.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';
2021-12-05 14:04:25 +08:00
import 'package:flowy_sdk/protobuf/flowy-user-infra/protobuf.dart' show UserProfile, ErrorCode;
import 'package:flowy_sdk/protobuf/flowy-user/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> {
2021-11-08 19:19:02 +08:00
final IAuth authManager;
SignUpBloc(this.authManager) : super(SignUpState.initial());
2021-07-13 13:14:49 +08:00
@override
2021-09-05 22:52:20 +08:00
Stream<SignUpState> mapEventToState(
SignUpEvent event,
2021-07-13 13:14:49 +08:00
) async* {
2021-09-06 16:18:34 +08:00
yield* event.map(signUpWithUserEmailAndPassword: (e) async* {
yield* _performActionOnSignUp();
}, emailChanged: (EmailChanged value) async* {
yield state.copyWith(email: value.email, emailError: none(), successOrFail: none());
2021-09-06 16:18:34 +08:00
}, passwordChanged: (PasswordChanged value) async* {
yield state.copyWith(password: value.password, passwordError: none(), successOrFail: none());
2021-09-06 16:18:34 +08:00
}, repeatPasswordChanged: (RepeatPasswordChanged value) async* {
yield 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
}
2021-09-06 16:18:34 +08:00
Stream<SignUpState> _performActionOnSignUp() async* {
yield state.copyWith(
isSubmitting: true,
2021-10-09 16:43:56 +08:00
successOrFail: none(),
2021-09-06 16:18:34 +08:00
);
final password = state.password;
final repeatedPassword = state.repeatedPassword;
if (password == null) {
yield state.copyWith(
isSubmitting: false,
2021-12-07 23:01:23 +05:30
passwordError: some(LocaleKeys.signUp_emptyPasswordError.tr()),
2021-09-06 16:18:34 +08:00
);
return;
}
if (repeatedPassword == null) {
yield state.copyWith(
isSubmitting: false,
2021-12-07 23:01:23 +05:30
repeatPasswordError: some(LocaleKeys.signUp_repeatPasswordEmptyError.tr()),
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) {
yield state.copyWith(
isSubmitting: false,
2021-12-07 23:01:23 +05:30
repeatPasswordError: some(LocaleKeys.signUp_unmatchedPasswordError.tr()),
2021-09-06 16:18:34 +08:00
);
return;
}
yield state.copyWith(
passwordError: none(),
repeatPasswordError: none(),
);
2021-11-08 19:19:02 +08:00
final result = await authManager.signUp(state.email, state.password, state.email);
2021-07-13 13:14:49 +08:00
yield 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),
2021-07-13 13:14:49 +08:00
);
}
2021-09-05 22:52:20 +08:00
SignUpState stateFromCode(UserError 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;
2021-09-05 22:52:20 +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-09-04 16:53:58 +08:00
required Option<Either<UserProfile, UserError>> 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(),
);
}