mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
refactor: Put User business logic in a service
This commit is contained in:
parent
289b3e31e7
commit
afab7f23c4
@ -1,6 +1,5 @@
|
||||
import 'dart:async';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:flowy_sdk/dispatch/dispatch.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/workspace.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
|
||||
import 'dart:typed_data';
|
||||
@ -10,65 +9,9 @@ import 'package:flowy_sdk/protobuf/dart-notify/protobuf.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-folder/dart_notification.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-user-data-model/errors.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-user-data-model/user_profile.pb.dart';
|
||||
// import 'package:flowy_sdk/protobuf/flowy-user/errors.pb.dart' as user_error;
|
||||
import 'package:flowy_sdk/protobuf/flowy-user/dart_notification.pb.dart' as user;
|
||||
import 'package:flowy_sdk/rust_stream.dart';
|
||||
|
||||
class UserRepo {
|
||||
final UserProfile user;
|
||||
UserRepo({
|
||||
required this.user,
|
||||
});
|
||||
|
||||
Future<Either<UserProfile, FlowyError>> fetchUserProfile({required String userId}) {
|
||||
return UserEventGetUserProfile().send();
|
||||
}
|
||||
|
||||
Future<Either<Unit, FlowyError>> deleteWorkspace({required String workspaceId}) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
Future<Either<Unit, FlowyError>> signOut() {
|
||||
return UserEventSignOut().send();
|
||||
}
|
||||
|
||||
Future<Either<Unit, FlowyError>> initUser() async {
|
||||
return UserEventInitUser().send();
|
||||
}
|
||||
|
||||
Future<Either<List<Workspace>, FlowyError>> getWorkspaces() {
|
||||
final request = WorkspaceId.create();
|
||||
|
||||
return FolderEventReadWorkspaces(request).send().then((result) {
|
||||
return result.fold(
|
||||
(workspaces) => left(workspaces.items),
|
||||
(error) => right(error),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Future<Either<Workspace, FlowyError>> openWorkspace(String workspaceId) {
|
||||
final request = WorkspaceId.create()..value = workspaceId;
|
||||
return FolderEventOpenWorkspace(request).send().then((result) {
|
||||
return result.fold(
|
||||
(workspace) => left(workspace),
|
||||
(error) => right(error),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Future<Either<Workspace, FlowyError>> createWorkspace(String name, String desc) {
|
||||
final request = CreateWorkspacePayload.create()
|
||||
..name = name
|
||||
..desc = desc;
|
||||
return FolderEventCreateWorkspace(request).send().then((result) {
|
||||
return result.fold(
|
||||
(workspace) => left(workspace),
|
||||
(error) => right(error),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
typedef UserProfileUpdatedNotifierValue = Either<UserProfile, FlowyError>;
|
||||
typedef AuthNotifierValue = Either<Unit, FlowyError>;
|
57
frontend/app_flowy/lib/user/application/user_service.dart
Normal file
57
frontend/app_flowy/lib/user/application/user_service.dart
Normal file
@ -0,0 +1,57 @@
|
||||
import 'dart:async';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:flowy_sdk/dispatch/dispatch.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/workspace.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-user-data-model/user_profile.pb.dart';
|
||||
|
||||
class UserService {
|
||||
Future<Either<UserProfile, FlowyError>> fetchUserProfile({required String userId}) {
|
||||
return UserEventGetUserProfile().send();
|
||||
}
|
||||
|
||||
Future<Either<Unit, FlowyError>> deleteWorkspace({required String workspaceId}) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
Future<Either<Unit, FlowyError>> signOut() {
|
||||
return UserEventSignOut().send();
|
||||
}
|
||||
|
||||
Future<Either<Unit, FlowyError>> initUser() async {
|
||||
return UserEventInitUser().send();
|
||||
}
|
||||
|
||||
Future<Either<List<Workspace>, FlowyError>> getWorkspaces() {
|
||||
final request = WorkspaceId.create();
|
||||
|
||||
return FolderEventReadWorkspaces(request).send().then((result) {
|
||||
return result.fold(
|
||||
(workspaces) => left(workspaces.items),
|
||||
(error) => right(error),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Future<Either<Workspace, FlowyError>> openWorkspace(String workspaceId) {
|
||||
final request = WorkspaceId.create()..value = workspaceId;
|
||||
return FolderEventOpenWorkspace(request).send().then((result) {
|
||||
return result.fold(
|
||||
(workspace) => left(workspace),
|
||||
(error) => right(error),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Future<Either<Workspace, FlowyError>> createWorkspace(String name, String desc) {
|
||||
final request = CreateWorkspacePayload.create()
|
||||
..name = name
|
||||
..desc = desc;
|
||||
return FolderEventCreateWorkspace(request).send().then((result) {
|
||||
return result.fold(
|
||||
(workspace) => left(workspace),
|
||||
(error) => right(error),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
@ -4,7 +4,6 @@ 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/skip_log_in_screen.dart';
|
||||
import 'package:app_flowy/user/presentation/welcome_screen.dart';
|
||||
import 'package:app_flowy/workspace/infrastructure/repos/user_repo.dart';
|
||||
import 'package:app_flowy/workspace/presentation/home/home_screen.dart';
|
||||
import 'package:flowy_infra/time/duration.dart';
|
||||
import 'package:flowy_infra_ui/widget/route/animation.dart';
|
||||
@ -38,9 +37,8 @@ class AuthRouter {
|
||||
}
|
||||
|
||||
class SplashRoute {
|
||||
Future<void> pushWelcomeScreen(BuildContext context, UserProfile user) async {
|
||||
final repo = UserRepo(user: user);
|
||||
final screen = WelcomeScreen(repo: repo);
|
||||
Future<void> pushWelcomeScreen(BuildContext context, UserProfile userProfile) async {
|
||||
final screen = WelcomeScreen(userProfile: userProfile);
|
||||
final workspaceId = await Navigator.of(context).push(
|
||||
PageRoutes.fade(
|
||||
() => screen,
|
||||
@ -48,7 +46,7 @@ class SplashRoute {
|
||||
),
|
||||
);
|
||||
|
||||
pushHomeScreen(context, repo.user, workspaceId);
|
||||
pushHomeScreen(context, userProfile, workspaceId);
|
||||
}
|
||||
|
||||
void pushHomeScreen(BuildContext context, UserProfile userProfile, CurrentWorkspaceSetting workspaceSetting) {
|
||||
|
@ -1,7 +1,7 @@
|
||||
import 'package:app_flowy/user/application/user_listener.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/workspace/infrastructure/repos/user_repo.dart';
|
||||
import 'package:easy_localization/easy_localization.dart';
|
||||
import 'package:flowy_infra/size.dart';
|
||||
import 'package:flowy_infra/theme.dart';
|
||||
|
@ -6,22 +6,22 @@ import 'package:flowy_infra_ui/style_widget/scrolling/styled_list.dart';
|
||||
import 'package:flowy_infra_ui/style_widget/button.dart';
|
||||
import 'package:flowy_infra_ui/widget/error_page.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/workspace.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-user-data-model/user_profile.pb.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:app_flowy/workspace/infrastructure/repos/user_repo.dart';
|
||||
import 'package:app_flowy/generated/locale_keys.g.dart';
|
||||
|
||||
class WelcomeScreen extends StatelessWidget {
|
||||
final UserRepo repo;
|
||||
final UserProfile userProfile;
|
||||
const WelcomeScreen({
|
||||
Key? key,
|
||||
required this.repo,
|
||||
required this.userProfile,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocProvider(
|
||||
create: (_) => getIt<WelcomeBloc>(param1: repo.user)..add(const WelcomeEvent.initial()),
|
||||
create: (_) => getIt<WelcomeBloc>(param1: userProfile)..add(const WelcomeEvent.initial()),
|
||||
child: BlocBuilder<WelcomeBloc, WelcomeState>(
|
||||
builder: (context, state) {
|
||||
return Scaffold(
|
||||
|
@ -1,4 +1,4 @@
|
||||
import 'package:app_flowy/workspace/infrastructure/repos/user_repo.dart';
|
||||
import 'package:app_flowy/user/application/user_listener.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/errors.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
|
@ -1,4 +1,5 @@
|
||||
import 'package:app_flowy/workspace/infrastructure/repos/user_repo.dart';
|
||||
import 'package:app_flowy/user/application/user_listener.dart';
|
||||
import 'package:app_flowy/user/application/user_service.dart';
|
||||
import 'package:flowy_sdk/log.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/workspace.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
|
||||
@ -10,16 +11,17 @@ import 'package:dartz/dartz.dart';
|
||||
part 'menu_user_bloc.freezed.dart';
|
||||
|
||||
class MenuUserBloc extends Bloc<MenuUserEvent, MenuUserState> {
|
||||
final UserRepo repo;
|
||||
final UserListener listener;
|
||||
final UserService userService;
|
||||
final UserListener userListener;
|
||||
final UserProfile userProfile;
|
||||
|
||||
MenuUserBloc(this.repo, this.listener) : super(MenuUserState.initial(repo.user)) {
|
||||
MenuUserBloc(this.userProfile, this.userService, this.userListener) : super(MenuUserState.initial(userProfile)) {
|
||||
on<MenuUserEvent>((event, emit) async {
|
||||
await event.map(
|
||||
initial: (_) async {
|
||||
listener.profileUpdatedNotifier.addPublishListener(_profileUpdated);
|
||||
listener.workspaceUpdatedNotifier.addPublishListener(_workspacesUpdated);
|
||||
listener.start();
|
||||
userListener.profileUpdatedNotifier.addPublishListener(_profileUpdated);
|
||||
userListener.workspaceUpdatedNotifier.addPublishListener(_workspacesUpdated);
|
||||
userListener.start();
|
||||
await _initUser();
|
||||
},
|
||||
fetchWorkspaces: (_FetchWorkspaces value) async {},
|
||||
@ -29,12 +31,12 @@ class MenuUserBloc extends Bloc<MenuUserEvent, MenuUserState> {
|
||||
|
||||
@override
|
||||
Future<void> close() async {
|
||||
await listener.stop();
|
||||
await userListener.stop();
|
||||
super.close();
|
||||
}
|
||||
|
||||
Future<void> _initUser() async {
|
||||
final result = await repo.initUser();
|
||||
final result = await userService.initUser();
|
||||
result.fold((l) => null, (error) => Log.error(error));
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
import 'package:app_flowy/workspace/infrastructure/repos/user_repo.dart';
|
||||
import 'package:app_flowy/user/application/user_listener.dart';
|
||||
import 'package:app_flowy/user/application/user_service.dart';
|
||||
import 'package:flowy_sdk/log.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/workspace.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
|
||||
@ -9,14 +10,14 @@ import 'package:dartz/dartz.dart';
|
||||
part 'welcome_bloc.freezed.dart';
|
||||
|
||||
class WelcomeBloc extends Bloc<WelcomeEvent, WelcomeState> {
|
||||
final UserRepo repo;
|
||||
final UserListener listener;
|
||||
WelcomeBloc({required this.repo, required this.listener}) : super(WelcomeState.initial()) {
|
||||
final UserService userService;
|
||||
final UserListener userListener;
|
||||
WelcomeBloc({required this.userService, required this.userListener}) : super(WelcomeState.initial()) {
|
||||
on<WelcomeEvent>(
|
||||
(event, emit) async {
|
||||
await event.map(initial: (e) async {
|
||||
listener.workspaceUpdatedNotifier.addPublishListener(_workspacesUpdated);
|
||||
listener.start();
|
||||
userListener.workspaceUpdatedNotifier.addPublishListener(_workspacesUpdated);
|
||||
userListener.start();
|
||||
//
|
||||
await _fetchWorkspaces(emit);
|
||||
}, openWorkspace: (e) async {
|
||||
@ -35,12 +36,12 @@ class WelcomeBloc extends Bloc<WelcomeEvent, WelcomeState> {
|
||||
|
||||
@override
|
||||
Future<void> close() async {
|
||||
await listener.stop();
|
||||
await userListener.stop();
|
||||
super.close();
|
||||
}
|
||||
|
||||
Future<void> _fetchWorkspaces(Emitter<WelcomeState> emit) async {
|
||||
final workspacesOrFailed = await repo.getWorkspaces();
|
||||
final workspacesOrFailed = await userService.getWorkspaces();
|
||||
emit(workspacesOrFailed.fold(
|
||||
(workspaces) => state.copyWith(workspaces: workspaces, successOrFailure: left(unit)),
|
||||
(error) {
|
||||
@ -51,7 +52,7 @@ class WelcomeBloc extends Bloc<WelcomeEvent, WelcomeState> {
|
||||
}
|
||||
|
||||
Future<void> _openWorkspace(Workspace workspace, Emitter<WelcomeState> emit) async {
|
||||
final result = await repo.openWorkspace(workspace.id);
|
||||
final result = await userService.openWorkspace(workspace.id);
|
||||
emit(result.fold(
|
||||
(workspaces) => state.copyWith(successOrFailure: left(unit)),
|
||||
(error) {
|
||||
@ -62,7 +63,7 @@ class WelcomeBloc extends Bloc<WelcomeEvent, WelcomeState> {
|
||||
}
|
||||
|
||||
Future<void> _createWorkspace(String name, String desc, Emitter<WelcomeState> emit) async {
|
||||
final result = await repo.createWorkspace(name, desc);
|
||||
final result = await userService.createWorkspace(name, desc);
|
||||
emit(result.fold(
|
||||
(workspace) {
|
||||
return state.copyWith(successOrFailure: left(unit));
|
||||
|
@ -1,3 +1,5 @@
|
||||
import 'package:app_flowy/user/application/user_listener.dart';
|
||||
import 'package:app_flowy/user/application/user_service.dart';
|
||||
import 'package:app_flowy/workspace/application/app/app_bloc.dart';
|
||||
import 'package:app_flowy/workspace/application/app/app_listener.dart';
|
||||
import 'package:app_flowy/workspace/application/app/app_service.dart';
|
||||
@ -17,7 +19,6 @@ import 'package:app_flowy/workspace/application/workspace/welcome_bloc.dart';
|
||||
import 'package:app_flowy/workspace/application/workspace/workspace_listener.dart';
|
||||
import 'package:app_flowy/workspace/application/workspace/workspace_service.dart';
|
||||
import 'package:app_flowy/workspace/domain/page_stack/page_stack.dart';
|
||||
import 'package:app_flowy/workspace/infrastructure/repos/user_repo.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/app.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/view.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-user-data-model/user_profile.pb.dart';
|
||||
@ -38,8 +39,8 @@ class HomeDepsResolver {
|
||||
getIt.registerLazySingleton<HomeStackManager>(() => HomeStackManager());
|
||||
getIt.registerFactoryParam<WelcomeBloc, UserProfile, void>(
|
||||
(user, _) => WelcomeBloc(
|
||||
repo: UserRepo(user: user),
|
||||
listener: getIt<UserListener>(param1: user),
|
||||
userService: UserService(),
|
||||
userListener: getIt<UserListener>(param1: user),
|
||||
),
|
||||
);
|
||||
|
||||
@ -71,7 +72,8 @@ class HomeDepsResolver {
|
||||
|
||||
getIt.registerFactoryParam<MenuUserBloc, UserProfile, void>(
|
||||
(user, _) => MenuUserBloc(
|
||||
UserRepo(user: user),
|
||||
user,
|
||||
UserService(),
|
||||
getIt<UserListener>(param1: user),
|
||||
),
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user