app_repo.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import 'dart:async';
  2. import 'dart:typed_data';
  3. import 'package:app_flowy/workspace/domain/i_app.dart';
  4. import 'package:dartz/dartz.dart';
  5. import 'package:flowy_log/flowy_log.dart';
  6. import 'package:flowy_sdk/dispatch/dispatch.dart';
  7. import 'package:flowy_sdk/protobuf/flowy-dart-notify/subject.pb.dart';
  8. import 'package:flowy_sdk/protobuf/flowy-workspace/app_create.pb.dart';
  9. import 'package:flowy_sdk/protobuf/flowy-workspace/app_delete.pb.dart';
  10. import 'package:flowy_sdk/protobuf/flowy-workspace/app_query.pb.dart';
  11. import 'package:flowy_sdk/protobuf/flowy-workspace/app_update.pb.dart';
  12. import 'package:flowy_sdk/protobuf/flowy-workspace/errors.pb.dart';
  13. import 'package:flowy_sdk/protobuf/flowy-workspace/observable.pb.dart';
  14. import 'package:flowy_sdk/protobuf/flowy-workspace/view_create.pb.dart';
  15. import 'package:flowy_sdk/protobuf/flowy-workspace/view_create.pbenum.dart';
  16. import 'package:flowy_sdk/rust_stream.dart';
  17. import 'helper.dart';
  18. class AppRepository {
  19. String appId;
  20. AppRepository({
  21. required this.appId,
  22. });
  23. Future<Either<App, WorkspaceError>> getAppDesc() {
  24. final request = QueryAppRequest.create()..appIds.add(appId);
  25. return WorkspaceEventReadApp(request).send();
  26. }
  27. Future<Either<View, WorkspaceError>> createView(String name, String desc, ViewType viewType) {
  28. final request = CreateViewRequest.create()
  29. ..belongToId = appId
  30. ..name = name
  31. ..desc = desc
  32. ..viewType = viewType;
  33. return WorkspaceEventCreateView(request).send();
  34. }
  35. Future<Either<List<View>, WorkspaceError>> getViews() {
  36. final request = QueryAppRequest.create()..appIds.add(appId);
  37. return WorkspaceEventReadApp(request).send().then((result) {
  38. return result.fold(
  39. (app) => left(app.belongings.items),
  40. (error) => right(error),
  41. );
  42. });
  43. }
  44. Future<Either<Unit, WorkspaceError>> delete() {
  45. final request = QueryAppRequest.create()..appIds.add(appId);
  46. return WorkspaceEventDeleteApp(request).send();
  47. }
  48. Future<Either<Unit, WorkspaceError>> updateApp({String? name}) {
  49. UpdateAppRequest request = UpdateAppRequest.create()..appId = appId;
  50. if (name != null) {
  51. request.name = name;
  52. }
  53. return WorkspaceEventUpdateApp(request).send();
  54. }
  55. }
  56. class AppListenerRepository {
  57. StreamSubscription<SubscribeObject>? _subscription;
  58. AppViewsChangeCallback? _viewsChanged;
  59. AppUpdatedCallback? _update;
  60. late WorkspaceNotificationParser _parser;
  61. String appId;
  62. AppListenerRepository({
  63. required this.appId,
  64. });
  65. void startListening({AppViewsChangeCallback? viewsChanged, AppUpdatedCallback? update}) {
  66. _viewsChanged = viewsChanged;
  67. _update = update;
  68. _parser = WorkspaceNotificationParser(id: appId, callback: _bservableCallback);
  69. _subscription = RustStreamReceiver.listen((observable) => _parser.parse(observable));
  70. }
  71. void _bservableCallback(WorkspaceNotification ty, Either<Uint8List, WorkspaceError> result) {
  72. switch (ty) {
  73. case WorkspaceNotification.AppViewsChanged:
  74. if (_viewsChanged != null) {
  75. result.fold(
  76. (payload) {
  77. final repeatedView = RepeatedView.fromBuffer(payload);
  78. _viewsChanged!(left(repeatedView.items));
  79. },
  80. (error) => _viewsChanged!(right(error)),
  81. );
  82. }
  83. break;
  84. case WorkspaceNotification.AppUpdated:
  85. if (_update != null) {
  86. result.fold(
  87. (payload) {
  88. final app = App.fromBuffer(payload);
  89. _update!(app.name, app.desc);
  90. },
  91. (error) => Log.error(error),
  92. );
  93. }
  94. break;
  95. default:
  96. break;
  97. }
  98. }
  99. Future<void> close() async {
  100. await _subscription?.cancel();
  101. }
  102. }