mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
refactor: put Workspace business logic in a service
This commit is contained in:
parent
a979037644
commit
871d0bb7be
@ -1,6 +1,7 @@
|
||||
import 'dart:async';
|
||||
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/workspace_repo.dart';
|
||||
import 'package:app_flowy/workspace/presentation/stack_page/blank/blank_page.dart';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:flowy_sdk/log.dart';
|
||||
@ -12,9 +13,11 @@ import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
part 'menu_bloc.freezed.dart';
|
||||
|
||||
class MenuBloc extends Bloc<MenuEvent, MenuState> {
|
||||
final WorkspaceRepo repo;
|
||||
final WorkspaceService service;
|
||||
final WorkspaceListener listener;
|
||||
MenuBloc({required this.repo, required this.listener}) : super(MenuState.initial()) {
|
||||
final String workspaceId;
|
||||
|
||||
MenuBloc({required this.workspaceId, required this.service, required this.listener}) : super(MenuState.initial()) {
|
||||
on<MenuEvent>((event, emit) async {
|
||||
await event.map(
|
||||
initial: (e) async {
|
||||
@ -48,7 +51,7 @@ class MenuBloc extends Bloc<MenuEvent, MenuState> {
|
||||
}
|
||||
|
||||
Future<void> _performActionOnCreateApp(CreateApp event, Emitter<MenuState> emit) async {
|
||||
final result = await repo.createApp(event.name, event.desc ?? "");
|
||||
final result = await service.createApp(workspaceId: workspaceId, name: event.name, desc: event.desc ?? "");
|
||||
result.fold(
|
||||
(app) => {},
|
||||
(error) {
|
||||
@ -60,7 +63,7 @@ class MenuBloc extends Bloc<MenuEvent, MenuState> {
|
||||
|
||||
// ignore: unused_element
|
||||
Future<void> _fetchApps(Emitter<MenuState> emit) async {
|
||||
final appsOrFail = await repo.getApps();
|
||||
final appsOrFail = await service.getApps(workspaceId: workspaceId);
|
||||
emit(appsOrFail.fold(
|
||||
(apps) => state.copyWith(apps: some(apps)),
|
||||
(error) {
|
||||
|
@ -1,10 +1,9 @@
|
||||
import 'dart:async';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:app_flowy/workspace/infrastructure/repos/helper.dart';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:easy_localization/easy_localization.dart';
|
||||
import 'package:flowy_sdk/log.dart';
|
||||
import 'package:flowy_sdk/dispatch/dispatch.dart';
|
||||
import 'package:flowy_sdk/protobuf/dart-notify/subject.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-user-data-model/protobuf.dart' show UserProfile;
|
||||
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/app.pb.dart';
|
||||
@ -12,56 +11,28 @@ 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-folder/dart_notification.pb.dart';
|
||||
import 'package:flowy_sdk/rust_stream.dart';
|
||||
import 'package:app_flowy/generated/locale_keys.g.dart';
|
||||
|
||||
import 'helper.dart';
|
||||
|
||||
class WorkspaceRepo {
|
||||
UserProfile user;
|
||||
String workspaceId;
|
||||
WorkspaceRepo({
|
||||
required this.user,
|
||||
required this.workspaceId,
|
||||
typedef WorkspaceAppsChangedCallback = void Function(Either<List<App>, FlowyError> appsOrFail);
|
||||
typedef WorkspaceUpdatedCallback = void Function(String name, String desc);
|
||||
|
||||
class WorkspaceListener {
|
||||
WorkspaceListenerService service;
|
||||
WorkspaceListener({
|
||||
required this.service,
|
||||
});
|
||||
|
||||
Future<Either<App, FlowyError>> createApp(String name, String desc) {
|
||||
final request = CreateAppPayload.create()
|
||||
..name = name
|
||||
..workspaceId = workspaceId
|
||||
..desc = desc;
|
||||
return FolderEventCreateApp(request).send();
|
||||
void start({WorkspaceAppsChangedCallback? addAppCallback, WorkspaceUpdatedCallback? updatedCallback}) {
|
||||
service.startListening(appsChanged: addAppCallback, update: updatedCallback);
|
||||
}
|
||||
|
||||
Future<Either<Workspace, FlowyError>> getWorkspace() {
|
||||
final request = WorkspaceId.create()..value = workspaceId;
|
||||
return FolderEventReadWorkspaces(request).send().then((result) {
|
||||
return result.fold(
|
||||
(workspaces) {
|
||||
assert(workspaces.items.length == 1);
|
||||
|
||||
if (workspaces.items.isEmpty) {
|
||||
return right(FlowyError.create()..msg = LocaleKeys.workspace_notFoundError.tr());
|
||||
} else {
|
||||
return left(workspaces.items[0]);
|
||||
}
|
||||
},
|
||||
(error) => right(error),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Future<Either<List<App>, FlowyError>> getApps() {
|
||||
final request = WorkspaceId.create()..value = workspaceId;
|
||||
return FolderEventReadWorkspaceApps(request).send().then((result) {
|
||||
return result.fold(
|
||||
(apps) => left(apps.items),
|
||||
(error) => right(error),
|
||||
);
|
||||
});
|
||||
Future<void> stop() async {
|
||||
await service.close();
|
||||
}
|
||||
}
|
||||
|
||||
class WorkspaceListenerRepo {
|
||||
|
||||
class WorkspaceListenerService {
|
||||
StreamSubscription<SubscribeObject>? _subscription;
|
||||
WorkspaceAppsChangedCallback? _appsChanged;
|
||||
WorkspaceUpdatedCallback? _update;
|
||||
@ -69,7 +40,7 @@ class WorkspaceListenerRepo {
|
||||
final UserProfile user;
|
||||
final String workspaceId;
|
||||
|
||||
WorkspaceListenerRepo({
|
||||
WorkspaceListenerService({
|
||||
required this.user,
|
||||
required this.workspaceId,
|
||||
});
|
||||
@ -125,22 +96,3 @@ class WorkspaceListenerRepo {
|
||||
// _update = null;
|
||||
}
|
||||
}
|
||||
|
||||
typedef WorkspaceAppsChangedCallback = void Function(Either<List<App>, FlowyError> appsOrFail);
|
||||
|
||||
typedef WorkspaceUpdatedCallback = void Function(String name, String desc);
|
||||
|
||||
class WorkspaceListener {
|
||||
WorkspaceListenerRepo repo;
|
||||
WorkspaceListener({
|
||||
required this.repo,
|
||||
});
|
||||
|
||||
void start({WorkspaceAppsChangedCallback? addAppCallback, WorkspaceUpdatedCallback? updatedCallback}) {
|
||||
repo.startListening(appsChanged: addAppCallback, update: updatedCallback);
|
||||
}
|
||||
|
||||
Future<void> stop() async {
|
||||
await repo.close();
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
import 'dart:async';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:easy_localization/easy_localization.dart';
|
||||
import 'package:flowy_sdk/dispatch/dispatch.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/app.pb.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:app_flowy/generated/locale_keys.g.dart';
|
||||
|
||||
class WorkspaceService {
|
||||
Future<Either<App, FlowyError>> createApp({required String workspaceId, required String name, required String desc}) {
|
||||
final request = CreateAppPayload.create()
|
||||
..name = name
|
||||
..workspaceId = workspaceId
|
||||
..desc = desc;
|
||||
return FolderEventCreateApp(request).send();
|
||||
}
|
||||
|
||||
Future<Either<Workspace, FlowyError>> getWorkspace({required String workspaceId}) {
|
||||
final request = WorkspaceId.create()..value = workspaceId;
|
||||
return FolderEventReadWorkspaces(request).send().then((result) {
|
||||
return result.fold(
|
||||
(workspaces) {
|
||||
assert(workspaces.items.length == 1);
|
||||
|
||||
if (workspaces.items.isEmpty) {
|
||||
return right(FlowyError.create()..msg = LocaleKeys.workspace_notFoundError.tr());
|
||||
} else {
|
||||
return left(workspaces.items[0]);
|
||||
}
|
||||
},
|
||||
(error) => right(error),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Future<Either<List<App>, FlowyError>> getApps({required String workspaceId}) {
|
||||
final request = WorkspaceId.create()..value = workspaceId;
|
||||
return FolderEventReadWorkspaceApps(request).send().then((result) {
|
||||
return result.fold(
|
||||
(apps) => left(apps.items),
|
||||
(error) => right(error),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
@ -12,10 +12,11 @@ import 'package:app_flowy/workspace/application/trash/trash_listener.dart';
|
||||
import 'package:app_flowy/workspace/application/trash/trash_service.dart';
|
||||
import 'package:app_flowy/workspace/application/view/view_bloc.dart';
|
||||
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:app_flowy/workspace/infrastructure/repos/view_repo.dart';
|
||||
import 'package:app_flowy/workspace/infrastructure/repos/workspace_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';
|
||||
@ -42,8 +43,8 @@ class HomeDepsResolver {
|
||||
);
|
||||
|
||||
//workspace
|
||||
getIt.registerFactoryParam<WorkspaceListener, UserProfile, String>(
|
||||
(user, workspaceId) => WorkspaceListener(repo: WorkspaceListenerRepo(user: user, workspaceId: workspaceId)));
|
||||
getIt.registerFactoryParam<WorkspaceListener, UserProfile, String>((user, workspaceId) =>
|
||||
WorkspaceListener(service: WorkspaceListenerService(user: user, workspaceId: workspaceId)));
|
||||
|
||||
// View
|
||||
getIt.registerFactoryParam<ViewListener, View, void>(
|
||||
@ -60,7 +61,8 @@ class HomeDepsResolver {
|
||||
//Menu Bloc
|
||||
getIt.registerFactoryParam<MenuBloc, UserProfile, String>(
|
||||
(user, workspaceId) => MenuBloc(
|
||||
repo: WorkspaceRepo(user: user, workspaceId: workspaceId),
|
||||
workspaceId: workspaceId,
|
||||
service: WorkspaceService(),
|
||||
listener: getIt<WorkspaceListener>(param1: user, param2: workspaceId),
|
||||
),
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user