refactor: put Auth business logic in a service

This commit is contained in:
MikeWallaceDev 2022-03-01 10:22:28 -05:00
parent e7033aa6e8
commit 33c758e711
6 changed files with 19 additions and 19 deletions

View File

@ -3,7 +3,7 @@ import 'package:flowy_sdk/dispatch/dispatch.dart';
import 'package:flowy_sdk/protobuf/flowy-user-data-model/protobuf.dart' show SignInPayload, SignUpPayload, UserProfile; import 'package:flowy_sdk/protobuf/flowy-user-data-model/protobuf.dart' show SignInPayload, SignUpPayload, UserProfile;
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart'; import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
class AuthRepository { class AuthService {
Future<Either<UserProfile, FlowyError>> signIn({required String? email, required String? password}) { Future<Either<UserProfile, FlowyError>> signIn({required String? email, required String? password}) {
// //
final request = SignInPayload.create() final request = SignInPayload.create()

View File

@ -1,4 +1,4 @@
import 'package:app_flowy/user/infrastructure/repos/auth_repo.dart'; import 'package:app_flowy/user/application/auth_service.dart';
import 'package:dartz/dartz.dart'; import 'package:dartz/dartz.dart';
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart'; import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
import 'package:flowy_sdk/protobuf/flowy-user-data-model/protobuf.dart' show UserProfile, ErrorCode; import 'package:flowy_sdk/protobuf/flowy-user-data-model/protobuf.dart' show UserProfile, ErrorCode;
@ -8,8 +8,8 @@ import 'package:flutter_bloc/flutter_bloc.dart';
part 'sign_in_bloc.freezed.dart'; part 'sign_in_bloc.freezed.dart';
class SignInBloc extends Bloc<SignInEvent, SignInState> { class SignInBloc extends Bloc<SignInEvent, SignInState> {
final AuthRepository authRepo; final AuthService authService;
SignInBloc(this.authRepo) : super(SignInState.initial()) { SignInBloc(this.authService) : super(SignInState.initial()) {
on<SignInEvent>((event, emit) async { on<SignInEvent>((event, emit) async {
await event.map( await event.map(
signedInWithUserEmailAndPassword: (e) async { signedInWithUserEmailAndPassword: (e) async {
@ -31,7 +31,7 @@ class SignInBloc extends Bloc<SignInEvent, SignInState> {
Future<void> _performActionOnSignIn(SignInState state, Emitter<SignInState> emit) async { Future<void> _performActionOnSignIn(SignInState state, Emitter<SignInState> emit) async {
emit(state.copyWith(isSubmitting: true, emailError: none(), passwordError: none(), successOrFail: none())); emit(state.copyWith(isSubmitting: true, emailError: none(), passwordError: none(), successOrFail: none()));
final result = await authRepo.signIn( final result = await authService.signIn(
email: state.email, email: state.email,
password: state.password, password: state.password,
); );

View File

@ -1,4 +1,4 @@
import 'package:app_flowy/user/infrastructure/repos/auth_repo.dart'; import 'package:app_flowy/user/application/auth_service.dart';
import 'package:dartz/dartz.dart'; import 'package:dartz/dartz.dart';
import 'package:easy_localization/easy_localization.dart'; import 'package:easy_localization/easy_localization.dart';
import 'package:flowy_sdk/protobuf/flowy-user-data-model/protobuf.dart' show UserProfile, ErrorCode; import 'package:flowy_sdk/protobuf/flowy-user-data-model/protobuf.dart' show UserProfile, ErrorCode;
@ -10,8 +10,8 @@ import 'package:app_flowy/generated/locale_keys.g.dart';
part 'sign_up_bloc.freezed.dart'; part 'sign_up_bloc.freezed.dart';
class SignUpBloc extends Bloc<SignUpEvent, SignUpState> { class SignUpBloc extends Bloc<SignUpEvent, SignUpState> {
final AuthRepository autoRepo; final AuthService authService;
SignUpBloc(this.autoRepo) : super(SignUpState.initial()) { SignUpBloc(this.authService) : super(SignUpState.initial()) {
on<SignUpEvent>((event, emit) async { on<SignUpEvent>((event, emit) async {
await event.map(signUpWithUserEmailAndPassword: (e) async { await event.map(signUpWithUserEmailAndPassword: (e) async {
await _performActionOnSignUp(emit); await _performActionOnSignUp(emit);
@ -62,7 +62,7 @@ class SignUpBloc extends Bloc<SignUpEvent, SignUpState> {
repeatPasswordError: none(), repeatPasswordError: none(),
)); ));
final result = await autoRepo.signUp( final result = await authService.signUp(
name: state.email, name: state.email,
password: state.password, password: state.password,
email: state.email, email: state.email,

View File

@ -1,7 +1,7 @@
import 'package:app_flowy/user/application/auth_service.dart';
import 'package:app_flowy/user/application/sign_in_bloc.dart'; import 'package:app_flowy/user/application/sign_in_bloc.dart';
import 'package:app_flowy/user/application/sign_up_bloc.dart'; import 'package:app_flowy/user/application/sign_up_bloc.dart';
import 'package:app_flowy/user/application/splash_bloc.dart'; import 'package:app_flowy/user/application/splash_bloc.dart';
import 'package:app_flowy/user/infrastructure/repos/auth_repo.dart';
import 'package:app_flowy/user/infrastructure/router.dart'; import 'package:app_flowy/user/infrastructure/router.dart';
import 'package:app_flowy/workspace/application/edit_pannel/edit_pannel_bloc.dart'; import 'package:app_flowy/workspace/application/edit_pannel/edit_pannel_bloc.dart';
import 'package:app_flowy/workspace/application/home/home_bloc.dart'; import 'package:app_flowy/workspace/application/home/home_bloc.dart';
@ -11,14 +11,14 @@ import 'network_monitor.dart';
class UserDepsResolver { class UserDepsResolver {
static Future<void> resolve(GetIt getIt) async { static Future<void> resolve(GetIt getIt) async {
getIt.registerFactory<AuthRepository>(() => AuthRepository()); getIt.registerFactory<AuthService>(() => AuthService());
//Interface implementation //Interface implementation
getIt.registerFactory<AuthRouter>(() => AuthRouter()); getIt.registerFactory<AuthRouter>(() => AuthRouter());
//Bloc //Bloc
getIt.registerFactory<SignInBloc>(() => SignInBloc(getIt<AuthRepository>())); getIt.registerFactory<SignInBloc>(() => SignInBloc(getIt<AuthService>()));
getIt.registerFactory<SignUpBloc>(() => SignUpBloc(getIt<AuthRepository>())); getIt.registerFactory<SignUpBloc>(() => SignUpBloc(getIt<AuthService>()));
getIt.registerFactory<SplashRoute>(() => SplashRoute()); getIt.registerFactory<SplashRoute>(() => SplashRoute());
getIt.registerFactory<HomeBloc>(() => HomeBloc()); getIt.registerFactory<HomeBloc>(() => HomeBloc());

View File

@ -1,5 +1,5 @@
import 'package:app_flowy/startup/startup.dart'; import 'package:app_flowy/startup/startup.dart';
import 'package:app_flowy/user/infrastructure/repos/auth_repo.dart'; import 'package:app_flowy/user/application/auth_service.dart';
import 'package:app_flowy/user/presentation/sign_in_screen.dart'; import 'package:app_flowy/user/presentation/sign_in_screen.dart';
import 'package:app_flowy/user/presentation/sign_up_screen.dart'; import 'package:app_flowy/user/presentation/sign_up_screen.dart';
import 'package:app_flowy/user/presentation/skip_log_in_screen.dart'; import 'package:app_flowy/user/presentation/skip_log_in_screen.dart';
@ -69,7 +69,7 @@ class SplashRoute {
PageRoutes.fade( PageRoutes.fade(
() => SkipLogInScreen( () => SkipLogInScreen(
router: getIt<AuthRouter>(), router: getIt<AuthRouter>(),
authRepo: getIt<AuthRepository>(), authService: getIt<AuthService>(),
), ),
RouteDurations.slow.inMilliseconds * .001), RouteDurations.slow.inMilliseconds * .001),
); );

View File

@ -1,6 +1,6 @@
import 'package:app_flowy/user/application/auth_service.dart';
import 'package:app_flowy/user/application/user_listener.dart'; import 'package:app_flowy/user/application/user_listener.dart';
import 'package:app_flowy/user/infrastructure/router.dart'; import 'package:app_flowy/user/infrastructure/router.dart';
import 'package:app_flowy/user/infrastructure/repos/auth_repo.dart';
import 'package:app_flowy/user/presentation/widgets/background.dart'; import 'package:app_flowy/user/presentation/widgets/background.dart';
import 'package:easy_localization/easy_localization.dart'; import 'package:easy_localization/easy_localization.dart';
import 'package:flowy_infra/size.dart'; import 'package:flowy_infra/size.dart';
@ -21,12 +21,12 @@ import 'package:app_flowy/generated/locale_keys.g.dart';
class SkipLogInScreen extends StatefulWidget { class SkipLogInScreen extends StatefulWidget {
final AuthRouter router; final AuthRouter router;
final AuthRepository authRepo; final AuthService authService;
const SkipLogInScreen({ const SkipLogInScreen({
Key? key, Key? key,
required this.router, required this.router,
required this.authRepo, required this.authService,
}) : super(key: key); }) : super(key: key);
@override @override
@ -99,7 +99,7 @@ class _SkipLogInScreenState extends State<SkipLogInScreen> {
const password = "AppFlowy123@"; const password = "AppFlowy123@";
final uid = uuid(); final uid = uuid();
final userEmail = "$uid@appflowy.io"; final userEmail = "$uid@appflowy.io";
final result = await widget.authRepo.signUp( final result = await widget.authService.signUp(
name: LocaleKeys.defaultUsername.tr(), name: LocaleKeys.defaultUsername.tr(),
password: password, password: password,
email: userEmail, email: userEmail,