remove app interface

This commit is contained in:
appflowy 2022-01-31 08:47:06 +08:00
parent aaca3ba90d
commit 99cadf98d2
5 changed files with 29 additions and 106 deletions

View File

@ -1,4 +1,4 @@
import 'package:app_flowy/workspace/domain/i_app.dart'; import 'package:app_flowy/workspace/infrastructure/repos/app_repo.dart';
import 'package:flowy_log/flowy_log.dart'; import 'package:flowy_log/flowy_log.dart';
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/app.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-folder-data-model/view.pb.dart';
@ -10,18 +10,18 @@ import 'package:dartz/dartz.dart';
part 'app_bloc.freezed.dart'; part 'app_bloc.freezed.dart';
class AppBloc extends Bloc<AppEvent, AppState> { class AppBloc extends Bloc<AppEvent, AppState> {
final IApp appManager; final AppRepository repo;
final IAppListenr listener; final AppListenerRepository listener;
AppBloc({required App app, required this.appManager, required this.listener}) : super(AppState.initial(app)) { AppBloc({required App app, required this.repo, required this.listener}) : super(AppState.initial(app)) {
on<AppEvent>((event, emit) async { on<AppEvent>((event, emit) async {
await event.map(initial: (e) async { await event.map(initial: (e) async {
listener.start( listener.startListening(
viewsChangeCallback: _handleViewsChanged, viewsChanged: _handleViewsChanged,
updatedCallback: (app) => add(AppEvent.appDidUpdate(app)), appUpdated: (app) => add(AppEvent.appDidUpdate(app)),
); );
await _fetchViews(emit); await _fetchViews(emit);
}, createView: (CreateView value) async { }, createView: (CreateView value) async {
final viewOrFailed = await appManager.createView(name: value.name, desc: value.desc, viewType: value.viewType); final viewOrFailed = await repo.createView(name: value.name, desc: value.desc, viewType: value.viewType);
viewOrFailed.fold( viewOrFailed.fold(
(view) => emit(state.copyWith( (view) => emit(state.copyWith(
latestCreatedView: view, latestCreatedView: view,
@ -35,13 +35,13 @@ class AppBloc extends Bloc<AppEvent, AppState> {
}, didReceiveViews: (e) async { }, didReceiveViews: (e) async {
await handleDidReceiveViews(e.views, emit); await handleDidReceiveViews(e.views, emit);
}, delete: (e) async { }, delete: (e) async {
final result = await appManager.delete(); final result = await repo.delete();
result.fold( result.fold(
(unit) => emit(state.copyWith(successOrFailure: left(unit))), (unit) => emit(state.copyWith(successOrFailure: left(unit))),
(error) => emit(state.copyWith(successOrFailure: right(error))), (error) => emit(state.copyWith(successOrFailure: right(error))),
); );
}, rename: (e) async { }, rename: (e) async {
final result = await appManager.rename(e.newName); final result = await repo.updateApp(name: e.newName);
result.fold( result.fold(
(l) => emit(state.copyWith(successOrFailure: left(unit))), (l) => emit(state.copyWith(successOrFailure: left(unit))),
(error) => emit(state.copyWith(successOrFailure: right(error))), (error) => emit(state.copyWith(successOrFailure: right(error))),
@ -54,7 +54,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {
@override @override
Future<void> close() async { Future<void> close() async {
await listener.stop(); await listener.close();
return super.close(); return super.close();
} }
@ -81,7 +81,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {
} }
Future<void> _fetchViews(Emitter<AppState> emit) async { Future<void> _fetchViews(Emitter<AppState> emit) async {
final viewsOrFailed = await appManager.getViews(); final viewsOrFailed = await repo.getViews();
viewsOrFailed.fold( viewsOrFailed.fold(
(apps) => emit(state.copyWith(views: apps)), (apps) => emit(state.copyWith(views: apps)),
(error) { (error) {

View File

@ -1,22 +0,0 @@
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/protobuf.dart';
import 'package:dartz/dartz.dart';
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
typedef AppUpdatedCallback = void Function(App app);
typedef AppViewsChangeCallback = void Function(Either<List<View>, FlowyError> viewsOrFailed);
abstract class IApp {
Future<Either<List<View>, FlowyError>> getViews();
Future<Either<View, FlowyError>> createView({required String name, String? desc, required ViewType viewType});
Future<Either<Unit, FlowyError>> delete();
Future<Either<Unit, FlowyError>> rename(String newName);
}
abstract class IAppListenr {
void start({AppViewsChangeCallback? viewsChangeCallback, AppUpdatedCallback? updatedCallback});
Future<void> stop();
}

View File

@ -11,7 +11,6 @@ import 'package:app_flowy/workspace/domain/i_share.dart';
import 'package:app_flowy/workspace/domain/i_trash.dart'; import 'package:app_flowy/workspace/domain/i_trash.dart';
import 'package:app_flowy/workspace/domain/i_view.dart'; import 'package:app_flowy/workspace/domain/i_view.dart';
import 'package:app_flowy/workspace/domain/page_stack/page_stack.dart'; import 'package:app_flowy/workspace/domain/page_stack/page_stack.dart';
import 'package:app_flowy/workspace/infrastructure/i_app_impl.dart';
import 'package:app_flowy/workspace/infrastructure/i_doc_impl.dart'; import 'package:app_flowy/workspace/infrastructure/i_doc_impl.dart';
import 'package:app_flowy/workspace/infrastructure/i_trash_impl.dart'; import 'package:app_flowy/workspace/infrastructure/i_trash_impl.dart';
import 'package:app_flowy/workspace/infrastructure/repos/app_repo.dart'; import 'package:app_flowy/workspace/infrastructure/repos/app_repo.dart';
@ -39,11 +38,6 @@ class HomeDepsResolver {
), ),
); );
//App
getIt.registerFactoryParam<IApp, String, void>((appId, _) => IAppImpl(repo: AppRepository(appId: appId)));
getIt.registerFactoryParam<IAppListenr, String, void>(
(appId, _) => IAppListenerhImpl(repo: AppListenerRepository(appId: appId)));
//workspace //workspace
getIt.registerFactoryParam<WorkspaceListener, UserProfile, String>( getIt.registerFactoryParam<WorkspaceListener, UserProfile, String>(
(user, workspaceId) => WorkspaceListener(repo: WorkspaceListenerRepo(user: user, workspaceId: workspaceId))); (user, workspaceId) => WorkspaceListener(repo: WorkspaceListenerRepo(user: user, workspaceId: workspaceId)));
@ -81,8 +75,8 @@ class HomeDepsResolver {
getIt.registerFactoryParam<AppBloc, App, void>( getIt.registerFactoryParam<AppBloc, App, void>(
(app, _) => AppBloc( (app, _) => AppBloc(
app: app, app: app,
appManager: getIt<IApp>(param1: app.id), repo: AppRepository(appId: app.id),
listener: getIt<IAppListenr>(param1: app.id), listener: AppListenerRepository(appId: app.id),
), ),
); );

View File

@ -1,55 +0,0 @@
import 'package:app_flowy/workspace/infrastructure/repos/app_repo.dart';
import 'package:dartz/dartz.dart';
import 'package:app_flowy/workspace/domain/i_app.dart';
import 'package:flowy_sdk/protobuf/flowy-folder-data-model/view.pb.dart';
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
export 'package:app_flowy/workspace/domain/i_app.dart';
class IAppImpl extends IApp {
AppRepository repo;
IAppImpl({
required this.repo,
});
@override
Future<Either<List<View>, FlowyError>> getViews() {
return repo.getViews();
}
@override
Future<Either<View, FlowyError>> createView({required String name, String? desc, required ViewType viewType}) {
return repo.createView(name, desc ?? "", viewType).then((result) {
return result.fold(
(view) => left(view),
(r) => right(r),
);
});
}
@override
Future<Either<Unit, FlowyError>> delete() {
return repo.delete();
}
@override
Future<Either<Unit, FlowyError>> rename(String newName) {
return repo.updateApp(name: newName);
}
}
class IAppListenerhImpl extends IAppListenr {
AppListenerRepository repo;
IAppListenerhImpl({
required this.repo,
});
@override
Future<void> stop() async {
await repo.close();
}
@override
void start({AppViewsChangeCallback? viewsChangeCallback, AppUpdatedCallback? updatedCallback}) {
repo.startListening(viewsChanged: viewsChangeCallback, update: updatedCallback);
}
}

View File

@ -1,6 +1,5 @@
import 'dart:async'; import 'dart:async';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:app_flowy/workspace/domain/i_app.dart';
import 'package:dartz/dartz.dart'; import 'package:dartz/dartz.dart';
import 'package:flowy_log/flowy_log.dart'; import 'package:flowy_log/flowy_log.dart';
import 'package:flowy_sdk/dispatch/dispatch.dart'; import 'package:flowy_sdk/dispatch/dispatch.dart';
@ -24,7 +23,11 @@ class AppRepository {
return FolderEventReadApp(request).send(); return FolderEventReadApp(request).send();
} }
Future<Either<View, FlowyError>> createView(String name, String desc, ViewType viewType) { Future<Either<View, FlowyError>> createView({
required String name,
required String desc,
required ViewType viewType,
}) {
final request = CreateViewRequest.create() final request = CreateViewRequest.create()
..belongToId = appId ..belongToId = appId
..name = name ..name = name
@ -60,10 +63,13 @@ class AppRepository {
} }
} }
typedef AppDidUpdateCallback = void Function(App app);
typedef ViewsDidChangeCallback = void Function(Either<List<View>, FlowyError> viewsOrFailed);
class AppListenerRepository { class AppListenerRepository {
StreamSubscription<SubscribeObject>? _subscription; StreamSubscription<SubscribeObject>? _subscription;
AppViewsChangeCallback? _viewsChanged; ViewsDidChangeCallback? _viewsChanged;
AppUpdatedCallback? _update; AppDidUpdateCallback? _updated;
late FolderNotificationParser _parser; late FolderNotificationParser _parser;
String appId; String appId;
@ -71,9 +77,9 @@ class AppListenerRepository {
required this.appId, required this.appId,
}); });
void startListening({AppViewsChangeCallback? viewsChanged, AppUpdatedCallback? update}) { void startListening({ViewsDidChangeCallback? viewsChanged, AppDidUpdateCallback? appUpdated}) {
_viewsChanged = viewsChanged; _viewsChanged = viewsChanged;
_update = update; _updated = appUpdated;
_parser = FolderNotificationParser(id: appId, callback: _bservableCallback); _parser = FolderNotificationParser(id: appId, callback: _bservableCallback);
_subscription = RustStreamReceiver.listen((observable) => _parser.parse(observable)); _subscription = RustStreamReceiver.listen((observable) => _parser.parse(observable));
} }
@ -92,11 +98,11 @@ class AppListenerRepository {
} }
break; break;
case FolderNotification.AppUpdated: case FolderNotification.AppUpdated:
if (_update != null) { if (_updated != null) {
result.fold( result.fold(
(payload) { (payload) {
final app = App.fromBuffer(payload); final app = App.fromBuffer(payload);
_update!(app); _updated!(app);
}, },
(error) => Log.error(error), (error) => Log.error(error),
); );
@ -110,6 +116,6 @@ class AppListenerRepository {
Future<void> close() async { Future<void> close() async {
await _subscription?.cancel(); await _subscription?.cancel();
_viewsChanged = null; _viewsChanged = null;
_update = null; _updated = null;
} }
} }