mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
refactor: Put Trash business logic in service
This commit is contained in:
parent
6509247fbd
commit
95d33c75a5
@ -1,6 +1,6 @@
|
||||
import 'dart:convert';
|
||||
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/application/trash/trash_service.dart';
|
||||
import 'package:app_flowy/workspace/infrastructure/repos/view_repo.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';
|
||||
@ -11,6 +11,7 @@ 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;
|
||||
@ -19,7 +20,7 @@ class DocumentBloc extends Bloc<DocumentEvent, DocumentState> {
|
||||
final View view;
|
||||
final DocumentService service;
|
||||
final ViewListener listener;
|
||||
final TrashRepo trashRepo;
|
||||
final TrashService trashService;
|
||||
late FlutterQuillDocument document;
|
||||
StreamSubscription? _subscription;
|
||||
|
||||
@ -27,7 +28,7 @@ class DocumentBloc extends Bloc<DocumentEvent, DocumentState> {
|
||||
required this.view,
|
||||
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);
|
||||
},
|
||||
|
@ -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/workspace/infrastructure/repos/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();
|
||||
}
|
||||
}
|
||||
|
@ -6,11 +6,12 @@ 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/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/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';
|
||||
@ -85,16 +86,16 @@ class HomeDepsResolver {
|
||||
view: view,
|
||||
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>(),
|
||||
),
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user