refactor: put App business logic in a service.

This commit is contained in:
MikeWallaceDev 2022-02-28 21:42:46 -05:00
parent 95d33c75a5
commit a979037644
4 changed files with 74 additions and 64 deletions

View File

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

View File

@ -1,67 +1,14 @@
import 'dart:async';
import 'dart:typed_data';
import 'package:dartz/dartz.dart';
import 'package:app_flowy/workspace/infrastructure/repos/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);

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

View File

@ -1,4 +1,6 @@
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';
@ -11,7 +13,6 @@ 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/user_repo.dart';
import 'package:app_flowy/workspace/infrastructure/repos/view_repo.dart';
import 'package:app_flowy/workspace/infrastructure/repos/workspace_repo.dart';
@ -75,7 +76,7 @@ class HomeDepsResolver {
getIt.registerFactoryParam<AppBloc, App, void>(
(app, _) => AppBloc(
app: app,
repo: AppRepository(appId: app.id),
service: AppService(),
listener: AppListener(appId: app.id),
),
);