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* {
|
2021-11-08 10:25:10 +08:00
|
|
|
yield state.copyWith(email: value.email, emailError: none(), successOrFail: none());
|
2021-09-06 16:18:34 +08:00
|
|
|
}, passwordChanged: (PasswordChanged value) async* {
|
2021-11-08 10:25:10 +08:00
|
|
|
yield state.copyWith(password: value.password, passwordError: none(), successOrFail: none());
|
2021-09-06 16:18:34 +08:00
|
|
|
}, repeatPasswordChanged: (RepeatPasswordChanged value) async* {
|
2021-11-08 10:25:10 +08:00
|
|
|
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(),
|
|
|
|
),
|
2021-07-25 18:04:16 +08:00
|
|
|
(error) => stateFromCode(error),
|
2021-07-13 13:14:49 +08:00
|
|
|
);
|
|
|
|
}
|
2021-07-25 18:04:16 +08:00
|
|
|
|
2021-09-05 22:52:20 +08:00
|
|
|
SignUpState stateFromCode(UserError error) {
|
2021-11-08 10:25:10 +08:00
|
|
|
switch (ErrorCode.valueOf(error.code)!) {
|
2021-08-31 11:32:51 +08:00
|
|
|
case ErrorCode.EmailFormatInvalid:
|
2021-07-25 18:04:16 +08:00
|
|
|
return state.copyWith(
|
2021-10-09 16:43:56 +08:00
|
|
|
isSubmitting: false,
|
|
|
|
emailError: some(error.msg),
|
|
|
|
passwordError: none(),
|
|
|
|
successOrFail: none(),
|
|
|
|
);
|
2021-08-31 11:32:51 +08:00
|
|
|
case ErrorCode.PasswordFormatInvalid:
|
2021-07-25 18:04:16 +08:00
|
|
|
return state.copyWith(
|
2021-10-09 16:43:56 +08:00
|
|
|
isSubmitting: false,
|
|
|
|
passwordError: some(error.msg),
|
|
|
|
emailError: none(),
|
|
|
|
successOrFail: none(),
|
|
|
|
);
|
2021-07-25 18:04:16 +08:00
|
|
|
default:
|
2021-11-08 10:25:10 +08:00
|
|
|
return state.copyWith(isSubmitting: false, successOrFail: some(right(error)));
|
2021-07-25 18:04:16 +08:00
|
|
|
}
|
|
|
|
}
|
2021-07-13 13:14:49 +08:00
|
|
|
}
|
2021-07-24 15:05:47 +08:00
|
|
|
|
|
|
|
@freezed
|
2021-10-09 10:09:31 +08:00
|
|
|
class SignUpEvent with _$SignUpEvent {
|
2021-11-08 10:25:10 +08:00
|
|
|
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;
|
2021-11-08 10:25:10 +08:00
|
|
|
const factory SignUpEvent.repeatPasswordChanged(String password) = RepeatPasswordChanged;
|
2021-07-24 15:05:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@freezed
|
2021-10-09 10:09:31 +08:00
|
|
|
class SignUpState with _$SignUpState {
|
2021-09-05 22:52:20 +08:00
|
|
|
const factory SignUpState({
|
2021-07-24 15:05:47 +08:00
|
|
|
String? email,
|
|
|
|
String? password,
|
2021-09-06 16:18:34 +08:00
|
|
|
String? repeatedPassword,
|
2021-07-24 15:05:47 +08:00
|
|
|
required bool isSubmitting,
|
2021-07-25 18:04:16 +08:00
|
|
|
required Option<String> passwordError,
|
2021-09-06 16:18:34 +08:00
|
|
|
required Option<String> repeatPasswordError,
|
2021-07-25 18:04:16 +08:00
|
|
|
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-07-24 15:05:47 +08:00
|
|
|
|
2021-09-05 22:52:20 +08:00
|
|
|
factory SignUpState.initial() => SignUpState(
|
2021-07-24 15:05:47 +08:00
|
|
|
isSubmitting: false,
|
2021-07-25 18:04:16 +08:00
|
|
|
passwordError: none(),
|
2021-09-06 16:18:34 +08:00
|
|
|
repeatPasswordError: none(),
|
2021-07-25 18:04:16 +08:00
|
|
|
emailError: none(),
|
|
|
|
successOrFail: none(),
|
2021-07-24 15:05:47 +08:00
|
|
|
);
|
|
|
|
}
|