2021-07-21 17:05:51 +08:00
|
|
|
import 'dart:async';
|
2021-09-07 17:12:03 +08:00
|
|
|
import 'dart:typed_data';
|
2021-07-22 17:06:53 +08:00
|
|
|
import 'package:app_flowy/workspace/domain/i_app.dart';
|
2021-07-21 17:05:51 +08:00
|
|
|
import 'package:dartz/dartz.dart';
|
2021-09-12 23:04:34 +08:00
|
|
|
import 'package:flowy_log/flowy_log.dart';
|
2021-07-21 17:05:51 +08:00
|
|
|
import 'package:flowy_sdk/dispatch/dispatch.dart';
|
2021-10-05 11:52:50 +08:00
|
|
|
import 'package:flowy_sdk/protobuf/flowy-dart-notify/subject.pb.dart';
|
2021-11-08 10:25:10 +08:00
|
|
|
import 'package:flowy_sdk/protobuf/flowy-workspace-infra/app_create.pb.dart';
|
|
|
|
import 'package:flowy_sdk/protobuf/flowy-workspace-infra/app_query.pb.dart';
|
|
|
|
import 'package:flowy_sdk/protobuf/flowy-workspace-infra/app_update.pb.dart';
|
|
|
|
import 'package:flowy_sdk/protobuf/flowy-workspace-infra/view_create.pb.dart';
|
|
|
|
import 'package:flowy_sdk/protobuf/flowy-workspace-infra/view_create.pbenum.dart';
|
2021-07-21 17:05:51 +08:00
|
|
|
import 'package:flowy_sdk/protobuf/flowy-workspace/errors.pb.dart';
|
|
|
|
import 'package:flowy_sdk/protobuf/flowy-workspace/observable.pb.dart';
|
|
|
|
import 'package:flowy_sdk/rust_stream.dart';
|
2021-09-07 17:12:03 +08:00
|
|
|
import 'helper.dart';
|
2021-07-21 17:05:51 +08:00
|
|
|
|
|
|
|
class AppRepository {
|
|
|
|
String appId;
|
|
|
|
AppRepository({
|
|
|
|
required this.appId,
|
|
|
|
});
|
|
|
|
|
|
|
|
Future<Either<App, WorkspaceError>> getAppDesc() {
|
2021-10-30 17:19:50 +08:00
|
|
|
final request = QueryAppRequest.create()..appIds.add(appId);
|
2021-07-21 17:05:51 +08:00
|
|
|
|
2021-07-29 17:46:20 +08:00
|
|
|
return WorkspaceEventReadApp(request).send();
|
2021-07-21 17:05:51 +08:00
|
|
|
}
|
|
|
|
|
2021-10-12 13:07:41 +08:00
|
|
|
Future<Either<View, WorkspaceError>> createView(String name, String desc, ViewType viewType) {
|
2021-07-21 17:05:51 +08:00
|
|
|
final request = CreateViewRequest.create()
|
2021-07-28 13:41:39 +08:00
|
|
|
..belongToId = appId
|
2021-07-21 17:05:51 +08:00
|
|
|
..name = name
|
|
|
|
..desc = desc
|
|
|
|
..viewType = viewType;
|
|
|
|
|
|
|
|
return WorkspaceEventCreateView(request).send();
|
|
|
|
}
|
|
|
|
|
2021-07-22 17:06:53 +08:00
|
|
|
Future<Either<List<View>, WorkspaceError>> getViews() {
|
2021-10-30 17:19:50 +08:00
|
|
|
final request = QueryAppRequest.create()..appIds.add(appId);
|
2021-07-21 17:05:51 +08:00
|
|
|
|
2021-07-29 17:46:20 +08:00
|
|
|
return WorkspaceEventReadApp(request).send().then((result) {
|
2021-07-21 17:05:51 +08:00
|
|
|
return result.fold(
|
2021-07-29 22:22:35 +08:00
|
|
|
(app) => left(app.belongings.items),
|
2021-07-21 17:05:51 +08:00
|
|
|
(error) => right(error),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2021-10-30 17:19:50 +08:00
|
|
|
|
|
|
|
Future<Either<Unit, WorkspaceError>> delete() {
|
|
|
|
final request = QueryAppRequest.create()..appIds.add(appId);
|
|
|
|
return WorkspaceEventDeleteApp(request).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Either<Unit, WorkspaceError>> updateApp({String? name}) {
|
|
|
|
UpdateAppRequest request = UpdateAppRequest.create()..appId = appId;
|
|
|
|
|
|
|
|
if (name != null) {
|
|
|
|
request.name = name;
|
|
|
|
}
|
|
|
|
return WorkspaceEventUpdateApp(request).send();
|
|
|
|
}
|
2021-07-22 11:23:15 +08:00
|
|
|
}
|
|
|
|
|
2021-10-12 13:07:41 +08:00
|
|
|
class AppListenerRepository {
|
2021-10-14 14:34:22 +08:00
|
|
|
StreamSubscription<SubscribeObject>? _subscription;
|
2021-10-13 23:11:45 +08:00
|
|
|
AppViewsChangeCallback? _viewsChanged;
|
2021-07-29 22:22:35 +08:00
|
|
|
AppUpdatedCallback? _update;
|
2021-10-14 14:34:22 +08:00
|
|
|
late WorkspaceNotificationParser _parser;
|
2021-07-22 11:23:15 +08:00
|
|
|
String appId;
|
2021-09-07 17:12:03 +08:00
|
|
|
|
2021-10-12 13:07:41 +08:00
|
|
|
AppListenerRepository({
|
2021-07-22 11:23:15 +08:00
|
|
|
required this.appId,
|
2021-09-07 17:12:03 +08:00
|
|
|
});
|
2021-07-21 17:05:51 +08:00
|
|
|
|
2021-10-14 14:34:22 +08:00
|
|
|
void startListening({AppViewsChangeCallback? viewsChanged, AppUpdatedCallback? update}) {
|
2021-10-13 23:11:45 +08:00
|
|
|
_viewsChanged = viewsChanged;
|
2021-07-29 22:22:35 +08:00
|
|
|
_update = update;
|
2021-10-14 14:34:22 +08:00
|
|
|
_parser = WorkspaceNotificationParser(id: appId, callback: _bservableCallback);
|
|
|
|
_subscription = RustStreamReceiver.listen((observable) => _parser.parse(observable));
|
2021-07-21 17:05:51 +08:00
|
|
|
}
|
|
|
|
|
2021-10-14 14:34:22 +08:00
|
|
|
void _bservableCallback(WorkspaceNotification ty, Either<Uint8List, WorkspaceError> result) {
|
2021-07-21 17:05:51 +08:00
|
|
|
switch (ty) {
|
2021-10-14 14:34:22 +08:00
|
|
|
case WorkspaceNotification.AppViewsChanged:
|
2021-10-13 23:11:45 +08:00
|
|
|
if (_viewsChanged != null) {
|
2021-07-21 17:05:51 +08:00
|
|
|
result.fold(
|
2021-09-07 17:12:03 +08:00
|
|
|
(payload) {
|
|
|
|
final repeatedView = RepeatedView.fromBuffer(payload);
|
2021-10-13 23:11:45 +08:00
|
|
|
_viewsChanged!(left(repeatedView.items));
|
2021-09-07 17:12:03 +08:00
|
|
|
},
|
2021-10-13 23:11:45 +08:00
|
|
|
(error) => _viewsChanged!(right(error)),
|
2021-07-21 17:05:51 +08:00
|
|
|
);
|
2021-09-07 17:12:03 +08:00
|
|
|
}
|
2021-07-21 17:05:51 +08:00
|
|
|
break;
|
2021-10-14 14:34:22 +08:00
|
|
|
case WorkspaceNotification.AppUpdated:
|
2021-09-07 17:12:03 +08:00
|
|
|
if (_update != null) {
|
2021-07-21 17:05:51 +08:00
|
|
|
result.fold(
|
2021-09-07 17:12:03 +08:00
|
|
|
(payload) {
|
|
|
|
final app = App.fromBuffer(payload);
|
2021-10-31 14:44:33 +08:00
|
|
|
_update!(app);
|
2021-09-07 17:12:03 +08:00
|
|
|
},
|
2021-07-21 17:05:51 +08:00
|
|
|
(error) => Log.error(error),
|
|
|
|
);
|
2021-09-07 17:12:03 +08:00
|
|
|
}
|
2021-07-21 17:05:51 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> close() async {
|
|
|
|
await _subscription?.cancel();
|
|
|
|
}
|
|
|
|
}
|