mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
Merge pull request #401 from MikeWallaceDev/put_business_logic_in_services
Put business logic in services
This commit is contained in:
commit
cace6248e0
@ -1,24 +1,29 @@
|
||||
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';
|
||||
import 'package:app_flowy/workspace/application/doc/doc_bloc.dart';
|
||||
import 'package:app_flowy/workspace/application/doc/doc_service.dart';
|
||||
import 'package:app_flowy/workspace/application/doc/share_bloc.dart';
|
||||
import 'package:app_flowy/workspace/application/doc/share_service.dart';
|
||||
import 'package:app_flowy/workspace/application/home/home_listen_bloc.dart';
|
||||
import 'package:app_flowy/workspace/application/menu/menu_bloc.dart';
|
||||
import 'package:app_flowy/workspace/application/menu/menu_user_bloc.dart';
|
||||
import 'package:app_flowy/workspace/application/trash/trash_bloc.dart';
|
||||
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/view/view_listener.dart';
|
||||
import 'package:app_flowy/workspace/application/view/view_service.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/app_repo.dart';
|
||||
import 'package:app_flowy/workspace/infrastructure/repos/document_repo.dart';
|
||||
import 'package:app_flowy/workspace/infrastructure/repos/trash_repo.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';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'repos/share_repo.dart';
|
||||
|
||||
class HomeDepsResolver {
|
||||
static Future<void> resolve(GetIt getIt) async {
|
||||
@ -34,14 +39,14 @@ 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),
|
||||
),
|
||||
);
|
||||
|
||||
//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>(
|
||||
@ -50,7 +55,8 @@ class HomeDepsResolver {
|
||||
|
||||
getIt.registerFactoryParam<ViewBloc, View, void>(
|
||||
(view, _) => ViewBloc(
|
||||
repo: ViewRepository(view: view),
|
||||
view: view,
|
||||
service: ViewService(),
|
||||
listener: getIt<ViewListener>(param1: view),
|
||||
),
|
||||
);
|
||||
@ -58,14 +64,16 @@ 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),
|
||||
),
|
||||
);
|
||||
|
||||
getIt.registerFactoryParam<MenuUserBloc, UserProfile, void>(
|
||||
(user, _) => MenuUserBloc(
|
||||
UserRepo(user: user),
|
||||
user,
|
||||
UserService(),
|
||||
getIt<UserListener>(param1: user),
|
||||
),
|
||||
);
|
||||
@ -74,7 +82,7 @@ class HomeDepsResolver {
|
||||
getIt.registerFactoryParam<AppBloc, App, void>(
|
||||
(app, _) => AppBloc(
|
||||
app: app,
|
||||
repo: AppRepository(appId: app.id),
|
||||
service: AppService(),
|
||||
listener: AppListener(appId: app.id),
|
||||
),
|
||||
);
|
||||
@ -83,25 +91,25 @@ class HomeDepsResolver {
|
||||
getIt.registerFactoryParam<DocumentBloc, View, void>(
|
||||
(view, _) => DocumentBloc(
|
||||
view: view,
|
||||
repo: DocumentRepository(docId: view.id),
|
||||
service: DocumentService(),
|
||||
listener: getIt<ViewListener>(param1: view),
|
||||
trashRepo: getIt<TrashRepo>(),
|
||||
trashService: getIt<TrashService>(),
|
||||
),
|
||||
);
|
||||
|
||||
// trash
|
||||
getIt.registerLazySingleton<TrashRepo>(() => TrashRepo());
|
||||
getIt.registerLazySingleton<TrashService>(() => TrashService());
|
||||
getIt.registerLazySingleton<TrashListener>(() => TrashListener());
|
||||
getIt.registerFactory<TrashBloc>(
|
||||
() => TrashBloc(
|
||||
repo: getIt<TrashRepo>(),
|
||||
service: getIt<TrashService>(),
|
||||
listener: getIt<TrashListener>(),
|
||||
),
|
||||
);
|
||||
|
||||
// share
|
||||
getIt.registerLazySingleton<ShareRepo>(() => ShareRepo());
|
||||
getIt.registerLazySingleton<ShareService>(() => ShareService());
|
||||
getIt.registerFactoryParam<DocShareBloc, View, void>(
|
||||
(view, _) => DocShareBloc(view: view, repo: getIt<ShareRepo>()));
|
||||
(view, _) => DocShareBloc(view: view, service: getIt<ShareService>()));
|
||||
}
|
||||
}
|
@ -4,7 +4,7 @@ import 'package:app_flowy/startup/tasks/prelude.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:app_flowy/workspace/infrastructure/deps_resolver.dart';
|
||||
import 'package:app_flowy/startup/deps_resolver.dart';
|
||||
import 'package:app_flowy/user/infrastructure/deps_resolver.dart';
|
||||
import 'package:flowy_sdk/flowy_sdk.dart';
|
||||
|
||||
|
@ -1,74 +1,17 @@
|
||||
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';
|
||||
import 'package:app_flowy/workspace/infrastructure/repos/helper.dart';
|
||||
import 'package:app_flowy/core/helper.dart';
|
||||
import 'package:flowy_infra/notifier.dart';
|
||||
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,5 @@
|
||||
import 'package:app_flowy/workspace/infrastructure/repos/app_repo.dart';
|
||||
import 'package:app_flowy/workspace/application/app/app_listener.dart';
|
||||
import 'package:app_flowy/workspace/application/app/app_service.dart';
|
||||
import 'package:flowy_sdk/log.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';
|
||||
@ -10,9 +11,12 @@ import 'package:dartz/dartz.dart';
|
||||
part 'app_bloc.freezed.dart';
|
||||
|
||||
class AppBloc extends Bloc<AppEvent, AppState> {
|
||||
final AppRepository repo;
|
||||
final App app;
|
||||
final AppService service;
|
||||
final AppListener listener;
|
||||
AppBloc({required App app, required this.repo, required this.listener}) : super(AppState.initial(app)) {
|
||||
|
||||
AppBloc({required this.app, required this.service, required this.listener})
|
||||
: super(AppState.initial(app)) {
|
||||
on<AppEvent>((event, emit) async {
|
||||
await event.map(initial: (e) async {
|
||||
listener.startListening(
|
||||
@ -21,7 +25,8 @@ class AppBloc extends Bloc<AppEvent, AppState> {
|
||||
);
|
||||
await _fetchViews(emit);
|
||||
}, createView: (CreateView value) async {
|
||||
final viewOrFailed = await repo.createView(name: value.name, desc: value.desc, viewType: value.viewType);
|
||||
final viewOrFailed =
|
||||
await service.createView(appId: app.id, name: value.name, desc: value.desc, viewType: value.viewType);
|
||||
viewOrFailed.fold(
|
||||
(view) => emit(state.copyWith(
|
||||
latestCreatedView: view,
|
||||
@ -35,13 +40,13 @@ class AppBloc extends Bloc<AppEvent, AppState> {
|
||||
}, didReceiveViews: (e) async {
|
||||
await handleDidReceiveViews(e.views, emit);
|
||||
}, delete: (e) async {
|
||||
final result = await repo.delete();
|
||||
final result = await service.delete(appId: app.id);
|
||||
result.fold(
|
||||
(unit) => emit(state.copyWith(successOrFailure: left(unit))),
|
||||
(error) => emit(state.copyWith(successOrFailure: right(error))),
|
||||
);
|
||||
}, rename: (e) async {
|
||||
final result = await repo.updateApp(name: e.newName);
|
||||
final result = await service.updateApp(appId: app.id, name: e.newName);
|
||||
result.fold(
|
||||
(l) => emit(state.copyWith(successOrFailure: left(unit))),
|
||||
(error) => emit(state.copyWith(successOrFailure: right(error))),
|
||||
@ -81,7 +86,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {
|
||||
}
|
||||
|
||||
Future<void> _fetchViews(Emitter<AppState> emit) async {
|
||||
final viewsOrFailed = await repo.getViews();
|
||||
final viewsOrFailed = await service.getViews(appId: app.id);
|
||||
viewsOrFailed.fold(
|
||||
(apps) => emit(state.copyWith(views: apps)),
|
||||
(error) {
|
||||
|
@ -1,67 +1,14 @@
|
||||
import 'dart:async';
|
||||
import 'dart:typed_data';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:app_flowy/core/helper.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-error/errors.pb.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-error/errors.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-folder/dart_notification.pb.dart';
|
||||
import 'package:flowy_sdk/rust_stream.dart';
|
||||
import 'helper.dart';
|
||||
|
||||
class AppRepository {
|
||||
String appId;
|
||||
AppRepository({
|
||||
required this.appId,
|
||||
});
|
||||
|
||||
Future<Either<App, FlowyError>> getAppDesc() {
|
||||
final request = AppId.create()..value = appId;
|
||||
|
||||
return FolderEventReadApp(request).send();
|
||||
}
|
||||
|
||||
Future<Either<View, FlowyError>> createView({
|
||||
required String name,
|
||||
required String desc,
|
||||
required ViewType viewType,
|
||||
}) {
|
||||
final request = CreateViewPayload.create()
|
||||
..belongToId = appId
|
||||
..name = name
|
||||
..desc = desc
|
||||
..viewType = viewType;
|
||||
|
||||
return FolderEventCreateView(request).send();
|
||||
}
|
||||
|
||||
Future<Either<List<View>, FlowyError>> getViews() {
|
||||
final request = AppId.create()..value = appId;
|
||||
|
||||
return FolderEventReadApp(request).send().then((result) {
|
||||
return result.fold(
|
||||
(app) => left(app.belongings.items),
|
||||
(error) => right(error),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Future<Either<Unit, FlowyError>> delete() {
|
||||
final request = AppId.create()..value = appId;
|
||||
return FolderEventDeleteApp(request).send();
|
||||
}
|
||||
|
||||
Future<Either<Unit, FlowyError>> updateApp({String? name}) {
|
||||
UpdateAppPayload request = UpdateAppPayload.create()..appId = appId;
|
||||
|
||||
if (name != null) {
|
||||
request.name = name;
|
||||
}
|
||||
return FolderEventUpdateApp(request).send();
|
||||
}
|
||||
}
|
||||
|
||||
typedef AppDidUpdateCallback = void Function(App app);
|
||||
typedef ViewsDidChangeCallback = void Function(Either<List<View>, FlowyError> viewsOrFailed);
|
@ -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/app.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/view.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
|
||||
|
||||
class AppService {
|
||||
|
||||
Future<Either<App, FlowyError>> getAppDesc({required String appId}) {
|
||||
final request = AppId.create()..value = appId;
|
||||
|
||||
return FolderEventReadApp(request).send();
|
||||
}
|
||||
|
||||
Future<Either<View, FlowyError>> createView({
|
||||
required String appId,
|
||||
required String name,
|
||||
required String desc,
|
||||
required ViewType viewType,
|
||||
}) {
|
||||
final request = CreateViewPayload.create()
|
||||
..belongToId = appId
|
||||
..name = name
|
||||
..desc = desc
|
||||
..viewType = viewType;
|
||||
|
||||
return FolderEventCreateView(request).send();
|
||||
}
|
||||
|
||||
Future<Either<List<View>, FlowyError>> getViews({required String appId}) {
|
||||
final request = AppId.create()..value = appId;
|
||||
|
||||
return FolderEventReadApp(request).send().then((result) {
|
||||
return result.fold(
|
||||
(app) => left(app.belongings.items),
|
||||
(error) => right(error),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Future<Either<Unit, FlowyError>> delete({required String appId}) {
|
||||
final request = AppId.create()..value = appId;
|
||||
return FolderEventDeleteApp(request).send();
|
||||
}
|
||||
|
||||
Future<Either<Unit, FlowyError>> updateApp({required String appId, String? name}) {
|
||||
UpdateAppPayload request = UpdateAppPayload.create()..appId = appId;
|
||||
|
||||
if (name != null) {
|
||||
request.name = name;
|
||||
}
|
||||
return FolderEventUpdateApp(request).send();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
import 'dart:convert';
|
||||
import 'package:app_flowy/workspace/infrastructure/repos/document_repo.dart';
|
||||
import 'package:app_flowy/workspace/infrastructure/repos/trash_repo.dart';
|
||||
import 'package:app_flowy/workspace/infrastructure/repos/view_repo.dart';
|
||||
import 'package:app_flowy/workspace/application/doc/doc_service.dart';
|
||||
import 'package:app_flowy/workspace/application/trash/trash_service.dart';
|
||||
import 'package:app_flowy/workspace/application/view/view_listener.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/trash.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/view.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
|
||||
@ -11,23 +11,24 @@ import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'dart:async';
|
||||
|
||||
part 'doc_bloc.freezed.dart';
|
||||
|
||||
typedef FlutterQuillDocument = Document;
|
||||
|
||||
class DocumentBloc extends Bloc<DocumentEvent, DocumentState> {
|
||||
final View view;
|
||||
final DocumentRepository repo;
|
||||
final DocumentService service;
|
||||
final ViewListener listener;
|
||||
final TrashRepo trashRepo;
|
||||
final TrashService trashService;
|
||||
late FlutterQuillDocument document;
|
||||
StreamSubscription? _subscription;
|
||||
|
||||
DocumentBloc({
|
||||
required this.view,
|
||||
required this.repo,
|
||||
required this.service,
|
||||
required this.listener,
|
||||
required this.trashRepo,
|
||||
required this.trashService,
|
||||
}) : super(DocumentState.initial()) {
|
||||
on<DocumentEvent>((event, emit) async {
|
||||
await event.map(
|
||||
@ -41,12 +42,12 @@ class DocumentBloc extends Bloc<DocumentEvent, DocumentState> {
|
||||
emit(state.copyWith(isDeleted: false));
|
||||
},
|
||||
deletePermanently: (DeletePermanently value) async {
|
||||
final result = await trashRepo.deleteViews([Tuple2(view.id, TrashType.TrashView)]);
|
||||
final result = await trashService.deleteViews([Tuple2(view.id, TrashType.TrashView)]);
|
||||
final newState = result.fold((l) => state.copyWith(forceClose: true), (r) => state);
|
||||
emit(newState);
|
||||
},
|
||||
restorePage: (RestorePage value) async {
|
||||
final result = await trashRepo.putback(view.id);
|
||||
final result = await trashService.putback(view.id);
|
||||
final newState = result.fold((l) => state.copyWith(isDeleted: false), (r) => state);
|
||||
emit(newState);
|
||||
},
|
||||
@ -62,7 +63,7 @@ class DocumentBloc extends Bloc<DocumentEvent, DocumentState> {
|
||||
await _subscription?.cancel();
|
||||
}
|
||||
|
||||
repo.closeDocument();
|
||||
service.closeDocument(docId: view.id);
|
||||
return super.close();
|
||||
}
|
||||
|
||||
@ -82,7 +83,7 @@ class DocumentBloc extends Bloc<DocumentEvent, DocumentState> {
|
||||
});
|
||||
|
||||
listener.start();
|
||||
final result = await repo.openDocument();
|
||||
final result = await service.openDocument(docId: view.id);
|
||||
result.fold(
|
||||
(doc) {
|
||||
document = _decodeJsonToDocument(doc.deltaJson);
|
||||
@ -108,7 +109,7 @@ class DocumentBloc extends Bloc<DocumentEvent, DocumentState> {
|
||||
void _composeDelta(Delta composedDelta, Delta documentDelta) async {
|
||||
final json = jsonEncode(composedDelta.toJson());
|
||||
Log.debug("doc_id: $view.id - Send json: $json");
|
||||
final result = await repo.composeDelta(data: json);
|
||||
final result = await service.composeDelta(docId: view.id, data: json);
|
||||
|
||||
result.fold((rustDoc) {
|
||||
// final json = utf8.decode(doc.data);
|
||||
|
@ -4,25 +4,20 @@ import 'package:flowy_sdk/protobuf/flowy-collaboration/document_info.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/view.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
|
||||
|
||||
class DocumentRepository {
|
||||
final String docId;
|
||||
DocumentRepository({
|
||||
required this.docId,
|
||||
});
|
||||
|
||||
Future<Either<DocumentDelta, FlowyError>> openDocument() {
|
||||
class DocumentService {
|
||||
Future<Either<DocumentDelta, FlowyError>> openDocument({required String docId}) {
|
||||
final request = ViewId(value: docId);
|
||||
return FolderEventOpenView(request).send();
|
||||
}
|
||||
|
||||
Future<Either<DocumentDelta, FlowyError>> composeDelta({required String data}) {
|
||||
Future<Either<DocumentDelta, FlowyError>> composeDelta({required String docId, required String data}) {
|
||||
final request = DocumentDelta.create()
|
||||
..docId = docId
|
||||
..deltaJson = data;
|
||||
return FolderEventApplyDocDelta(request).send();
|
||||
}
|
||||
|
||||
Future<Either<Unit, FlowyError>> closeDocument() {
|
||||
Future<Either<Unit, FlowyError>> closeDocument({required String docId}) {
|
||||
final request = ViewId(value: docId);
|
||||
return FolderEventCloseView(request).send();
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
import 'package:app_flowy/workspace/infrastructure/markdown/delta_markdown.dart';
|
||||
import 'package:app_flowy/workspace/infrastructure/repos/share_repo.dart';
|
||||
import 'package:app_flowy/workspace/application/doc/share_service.dart';
|
||||
import 'package:app_flowy/workspace/application/markdown/delta_markdown.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/share.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/view.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
|
||||
@ -9,13 +9,13 @@ import 'package:dartz/dartz.dart';
|
||||
part 'share_bloc.freezed.dart';
|
||||
|
||||
class DocShareBloc extends Bloc<DocShareEvent, DocShareState> {
|
||||
ShareRepo repo;
|
||||
ShareService service;
|
||||
View view;
|
||||
DocShareBloc({required this.view, required this.repo}) : super(const DocShareState.initial()) {
|
||||
DocShareBloc({required this.view, required this.service}) : super(const DocShareState.initial()) {
|
||||
on<DocShareEvent>((event, emit) async {
|
||||
await event.map(
|
||||
shareMarkdown: (ShareMarkdown value) async {
|
||||
await repo.exportMarkdown(view.id).then((result) {
|
||||
await service.exportMarkdown(view.id).then((result) {
|
||||
result.fold(
|
||||
(value) => emit(DocShareState.finish(left(_convertDeltaToMarkdown(value)))),
|
||||
(error) => emit(DocShareState.finish(right(error))),
|
||||
|
@ -4,7 +4,7 @@ import 'package:flowy_sdk/dispatch/dispatch.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/protobuf.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
|
||||
|
||||
class ShareRepo {
|
||||
class ShareService {
|
||||
Future<Either<ExportData, FlowyError>> export(String docId, ExportType type) {
|
||||
final request = ExportPayload.create()
|
||||
..docId = docId
|
@ -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,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,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,20 +1,21 @@
|
||||
import 'package:app_flowy/workspace/infrastructure/repos/trash_repo.dart';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:flowy_sdk/log.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/trash.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import 'package:app_flowy/workspace/application/trash/trash_service.dart';
|
||||
import 'package:app_flowy/workspace/application/trash/trash_listener.dart';
|
||||
part 'trash_bloc.freezed.dart';
|
||||
|
||||
class TrashBloc extends Bloc<TrashEvent, TrashState> {
|
||||
final TrashRepo repo;
|
||||
final TrashService service;
|
||||
final TrashListener listener;
|
||||
TrashBloc({required this.repo, required this.listener}) : super(TrashState.init()) {
|
||||
TrashBloc({required this.service, required this.listener}) : super(TrashState.init()) {
|
||||
on<TrashEvent>((event, emit) async {
|
||||
await event.map(initial: (e) async {
|
||||
listener.startListening(trashUpdated: _listenTrashUpdated);
|
||||
final result = await repo.readTrash();
|
||||
final result = await service.readTrash();
|
||||
emit(result.fold(
|
||||
(object) => state.copyWith(objects: object.items, successOrFailure: left(unit)),
|
||||
(error) => state.copyWith(successOrFailure: right(error)),
|
||||
@ -22,16 +23,16 @@ class TrashBloc extends Bloc<TrashEvent, TrashState> {
|
||||
}, didReceiveTrash: (e) async {
|
||||
emit(state.copyWith(objects: e.trash));
|
||||
}, putback: (e) async {
|
||||
final result = await repo.putback(e.trashId);
|
||||
final result = await service.putback(e.trashId);
|
||||
await _handleResult(result, emit);
|
||||
}, delete: (e) async {
|
||||
final result = await repo.deleteViews([Tuple2(e.trash.id, e.trash.ty)]);
|
||||
final result = await service.deleteViews([Tuple2(e.trash.id, e.trash.ty)]);
|
||||
await _handleResult(result, emit);
|
||||
}, deleteAll: (e) async {
|
||||
final result = await repo.deleteAll();
|
||||
final result = await service.deleteAll();
|
||||
await _handleResult(result, emit);
|
||||
}, restoreAll: (e) async {
|
||||
final result = await repo.restoreAll();
|
||||
final result = await service.restoreAll();
|
||||
await _handleResult(result, emit);
|
||||
});
|
||||
});
|
||||
|
@ -1,45 +1,13 @@
|
||||
import 'dart:async';
|
||||
import 'dart:typed_data';
|
||||
import 'package:app_flowy/workspace/infrastructure/repos/helper.dart';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:flowy_sdk/dispatch/dispatch.dart';
|
||||
import 'package:app_flowy/core/helper.dart';
|
||||
import 'package:flowy_sdk/protobuf/dart-notify/subject.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-folder/dart_notification.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/trash.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-folder/dart_notification.pb.dart';
|
||||
import 'package:flowy_sdk/rust_stream.dart';
|
||||
|
||||
class TrashRepo {
|
||||
Future<Either<RepeatedTrash, FlowyError>> readTrash() {
|
||||
return FolderEventReadTrash().send();
|
||||
}
|
||||
|
||||
Future<Either<Unit, FlowyError>> putback(String trashId) {
|
||||
final id = TrashId.create()..id = trashId;
|
||||
|
||||
return FolderEventPutbackTrash(id).send();
|
||||
}
|
||||
|
||||
Future<Either<Unit, FlowyError>> deleteViews(List<Tuple2<String, TrashType>> trashList) {
|
||||
final items = trashList.map((trash) {
|
||||
return TrashId.create()
|
||||
..id = trash.value1
|
||||
..ty = trash.value2;
|
||||
});
|
||||
|
||||
final ids = RepeatedTrashId(items: items);
|
||||
return FolderEventDeleteTrash(ids).send();
|
||||
}
|
||||
|
||||
Future<Either<Unit, FlowyError>> restoreAll() {
|
||||
return FolderEventRestoreAllTrash().send();
|
||||
}
|
||||
|
||||
Future<Either<Unit, FlowyError>> deleteAll() {
|
||||
return FolderEventDeleteAllTrash().send();
|
||||
}
|
||||
}
|
||||
|
||||
typedef TrashUpdatedCallback = void Function(Either<List<Trash>, FlowyError> trashOrFailed);
|
||||
|
||||
class TrashListener {
|
@ -0,0 +1,37 @@
|
||||
import 'dart:async';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:flowy_sdk/dispatch/dispatch.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/trash.pb.dart';
|
||||
|
||||
class TrashService {
|
||||
Future<Either<RepeatedTrash, FlowyError>> readTrash() {
|
||||
return FolderEventReadTrash().send();
|
||||
}
|
||||
|
||||
Future<Either<Unit, FlowyError>> putback(String trashId) {
|
||||
final id = TrashId.create()..id = trashId;
|
||||
|
||||
return FolderEventPutbackTrash(id).send();
|
||||
}
|
||||
|
||||
Future<Either<Unit, FlowyError>> deleteViews(List<Tuple2<String, TrashType>> trashList) {
|
||||
final items = trashList.map((trash) {
|
||||
return TrashId.create()
|
||||
..id = trash.value1
|
||||
..ty = trash.value2;
|
||||
});
|
||||
|
||||
final ids = RepeatedTrashId(items: items);
|
||||
return FolderEventDeleteTrash(ids).send();
|
||||
}
|
||||
|
||||
Future<Either<Unit, FlowyError>> restoreAll() {
|
||||
return FolderEventRestoreAllTrash().send();
|
||||
}
|
||||
|
||||
Future<Either<Unit, FlowyError>> deleteAll() {
|
||||
return FolderEventDeleteAllTrash().send();
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
import 'package:app_flowy/workspace/infrastructure/repos/view_repo.dart';
|
||||
import 'package:app_flowy/workspace/application/view/view_listener.dart';
|
||||
import 'package:app_flowy/workspace/application/view/view_service.dart';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/view.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
|
||||
@ -8,18 +9,19 @@ import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
part 'view_bloc.freezed.dart';
|
||||
|
||||
class ViewBloc extends Bloc<ViewEvent, ViewState> {
|
||||
final ViewRepository repo;
|
||||
|
||||
final ViewService service;
|
||||
final ViewListener listener;
|
||||
final View view;
|
||||
|
||||
ViewBloc({
|
||||
required this.repo,
|
||||
required this.view,
|
||||
required this.service,
|
||||
required this.listener,
|
||||
}) : super(ViewState.init(repo.view)) {
|
||||
}) : super(ViewState.init(view)) {
|
||||
on<ViewEvent>((event, emit) async {
|
||||
await event.map(
|
||||
initial: (e) {
|
||||
// TODO: Listener can be refctored to a stream.
|
||||
// TODO: Listener can be refactored to a stream.
|
||||
listener.updatedNotifier.addPublishListener((result) {
|
||||
// emit.forEach(stream, onData: onData)
|
||||
add(ViewEvent.viewDidUpdate(result));
|
||||
@ -37,7 +39,7 @@ class ViewBloc extends Bloc<ViewEvent, ViewState> {
|
||||
);
|
||||
},
|
||||
rename: (e) async {
|
||||
final result = await repo.updateView(name: e.newName);
|
||||
final result = await service.updateView(viewId: view.id, name: e.newName);
|
||||
emit(
|
||||
result.fold(
|
||||
(l) => state.copyWith(successOrFailure: left(unit)),
|
||||
@ -46,7 +48,7 @@ class ViewBloc extends Bloc<ViewEvent, ViewState> {
|
||||
);
|
||||
},
|
||||
delete: (e) async {
|
||||
final result = await repo.delete();
|
||||
final result = await service.delete(viewId: view.id);
|
||||
emit(
|
||||
result.fold(
|
||||
(l) => state.copyWith(successOrFailure: left(unit)),
|
||||
@ -55,7 +57,7 @@ class ViewBloc extends Bloc<ViewEvent, ViewState> {
|
||||
);
|
||||
},
|
||||
duplicate: (e) async {
|
||||
final result = await repo.duplicate();
|
||||
final result = await service.duplicate(viewId: view.id);
|
||||
emit(
|
||||
result.fold(
|
||||
(l) => state.copyWith(successOrFailure: left(unit)),
|
||||
|
@ -1,7 +1,7 @@
|
||||
import 'dart:async';
|
||||
import 'dart:typed_data';
|
||||
import 'package:app_flowy/core/helper.dart';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:flowy_sdk/dispatch/dispatch.dart';
|
||||
import 'package:flowy_sdk/protobuf/dart-notify/subject.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/view.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
|
||||
@ -9,44 +9,6 @@ import 'package:flowy_sdk/protobuf/flowy-folder/dart_notification.pb.dart';
|
||||
import 'package:flowy_sdk/rust_stream.dart';
|
||||
import 'package:flowy_infra/notifier.dart';
|
||||
|
||||
import 'helper.dart';
|
||||
|
||||
class ViewRepository {
|
||||
View view;
|
||||
ViewRepository({
|
||||
required this.view,
|
||||
});
|
||||
|
||||
Future<Either<View, FlowyError>> readView() {
|
||||
final request = ViewId(value: view.id);
|
||||
return FolderEventReadView(request).send();
|
||||
}
|
||||
|
||||
Future<Either<View, FlowyError>> updateView({String? name, String? desc}) {
|
||||
final request = UpdateViewPayload.create()..viewId = view.id;
|
||||
|
||||
if (name != null) {
|
||||
request.name = name;
|
||||
}
|
||||
|
||||
if (desc != null) {
|
||||
request.desc = desc;
|
||||
}
|
||||
|
||||
return FolderEventUpdateView(request).send();
|
||||
}
|
||||
|
||||
Future<Either<Unit, FlowyError>> delete() {
|
||||
final request = RepeatedViewId.create()..items.add(view.id);
|
||||
return FolderEventDeleteView(request).send();
|
||||
}
|
||||
|
||||
Future<Either<Unit, FlowyError>> duplicate() {
|
||||
final request = ViewId(value: view.id);
|
||||
return FolderEventDuplicateView(request).send();
|
||||
}
|
||||
}
|
||||
|
||||
typedef DeleteNotifierValue = Either<View, FlowyError>;
|
||||
typedef UpdateNotifierValue = Either<View, FlowyError>;
|
||||
typedef RestoreNotifierValue = Either<View, FlowyError>;
|
@ -0,0 +1,36 @@
|
||||
import 'dart:async';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:flowy_sdk/dispatch/dispatch.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/view.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
|
||||
|
||||
class ViewService {
|
||||
Future<Either<View, FlowyError>> readView({required String viewId}) {
|
||||
final request = ViewId(value: viewId);
|
||||
return FolderEventReadView(request).send();
|
||||
}
|
||||
|
||||
Future<Either<View, FlowyError>> updateView({required String viewId, String? name, String? desc}) {
|
||||
final request = UpdateViewPayload.create()..viewId = viewId;
|
||||
|
||||
if (name != null) {
|
||||
request.name = name;
|
||||
}
|
||||
|
||||
if (desc != null) {
|
||||
request.desc = desc;
|
||||
}
|
||||
|
||||
return FolderEventUpdateView(request).send();
|
||||
}
|
||||
|
||||
Future<Either<Unit, FlowyError>> delete({required String viewId}) {
|
||||
final request = RepeatedViewId.create()..items.add(viewId);
|
||||
return FolderEventDeleteView(request).send();
|
||||
}
|
||||
|
||||
Future<Either<Unit, FlowyError>> duplicate({required String viewId}) {
|
||||
final request = ViewId(value: viewId);
|
||||
return FolderEventDuplicateView(request).send();
|
||||
}
|
||||
}
|
@ -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,10 +1,9 @@
|
||||
import 'dart:async';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:app_flowy/core/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),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
@ -1,9 +1,10 @@
|
||||
import 'package:app_flowy/startup/startup.dart';
|
||||
import 'package:app_flowy/workspace/application/appearance.dart';
|
||||
import 'package:app_flowy/workspace/application/doc/share_bloc.dart';
|
||||
import 'package:app_flowy/workspace/application/view/view_listener.dart';
|
||||
import 'package:app_flowy/workspace/application/view/view_service.dart';
|
||||
import 'package:app_flowy/workspace/domain/page_stack/page_stack.dart';
|
||||
import 'package:app_flowy/workspace/domain/view_ext.dart';
|
||||
import 'package:app_flowy/workspace/infrastructure/repos/view_repo.dart';
|
||||
import 'package:app_flowy/workspace/presentation/widgets/dialogs.dart';
|
||||
import 'package:app_flowy/workspace/presentation/widgets/pop_up_action.dart';
|
||||
import 'package:easy_localization/easy_localization.dart';
|
||||
@ -92,11 +93,11 @@ class DocumentLeftBarItem extends StatefulWidget {
|
||||
class _DocumentLeftBarItemState extends State<DocumentLeftBarItem> {
|
||||
final _controller = TextEditingController();
|
||||
final _focusNode = FocusNode();
|
||||
late ViewRepository repo;
|
||||
late ViewService service;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
repo = ViewRepository(view: widget.view);
|
||||
service = ViewService(/*view: widget.view*/);
|
||||
_focusNode.addListener(_handleFocusChanged);
|
||||
super.initState();
|
||||
}
|
||||
@ -143,7 +144,7 @@ class _DocumentLeftBarItemState extends State<DocumentLeftBarItem> {
|
||||
}
|
||||
|
||||
if (_controller.text != widget.view.name) {
|
||||
repo.updateView(name: _controller.text);
|
||||
service.updateView(viewId: widget.view.id, name: _controller.text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user