refactor: put document business logic into service

This commit is contained in:
MikeWallaceDev 2022-02-28 21:02:46 -05:00
parent 8128283ad4
commit 6509247fbd
3 changed files with 12 additions and 17 deletions

View File

@ -1,5 +1,5 @@
import 'dart:convert';
import 'package:app_flowy/workspace/infrastructure/repos/document_repo.dart';
import 'package:app_flowy/workspace/application/doc/doc_service.dart';
import 'package:app_flowy/workspace/infrastructure/repos/trash_repo.dart';
import 'package:app_flowy/workspace/infrastructure/repos/view_repo.dart';
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/trash.pb.dart';
@ -17,7 +17,7 @@ typedef FlutterQuillDocument = Document;
class DocumentBloc extends Bloc<DocumentEvent, DocumentState> {
final View view;
final DocumentRepository repo;
final DocumentService service;
final ViewListener listener;
final TrashRepo trashRepo;
late FlutterQuillDocument document;
@ -25,7 +25,7 @@ class DocumentBloc extends Bloc<DocumentEvent, DocumentState> {
DocumentBloc({
required this.view,
required this.repo,
required this.service,
required this.listener,
required this.trashRepo,
}) : super(DocumentState.initial()) {
@ -62,7 +62,7 @@ class DocumentBloc extends Bloc<DocumentEvent, DocumentState> {
await _subscription?.cancel();
}
repo.closeDocument();
service.closeDocument(docId: view.id);
return super.close();
}
@ -82,7 +82,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 +108,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);

View File

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

View File

@ -1,5 +1,6 @@
import 'package:app_flowy/workspace/application/app/app_bloc.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/home/home_listen_bloc.dart';
import 'package:app_flowy/workspace/application/menu/menu_bloc.dart';
@ -9,7 +10,6 @@ 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/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';
@ -83,7 +83,7 @@ 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>(),
),