refactor: Put View business logic in a service

This commit is contained in:
MikeWallaceDev 2022-02-28 23:06:36 -05:00
parent 871d0bb7be
commit 289b3e31e7
6 changed files with 58 additions and 55 deletions

View File

@ -1,7 +1,7 @@
import 'dart:convert';
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/infrastructure/repos/view_repo.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';

View File

@ -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)),

View File

@ -1,7 +1,7 @@
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: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>;

View File

@ -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();
}
}

View File

@ -11,12 +11,13 @@ 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/user_repo.dart';
import 'package:app_flowy/workspace/infrastructure/repos/view_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';
@ -53,7 +54,8 @@ class HomeDepsResolver {
getIt.registerFactoryParam<ViewBloc, View, void>(
(view, _) => ViewBloc(
repo: ViewRepository(view: view),
view: view,
service: ViewService(),
listener: getIt<ViewListener>(param1: view),
),
);

View File

@ -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);
}
}
}