app_repo.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import 'dart:async';
  2. import 'package:dartz/dartz.dart';
  3. import 'package:flowy_infra/flowy_logger.dart';
  4. import 'package:flowy_sdk/dispatch/dispatch.dart';
  5. import 'package:flowy_sdk/protobuf/flowy-observable/subject.pb.dart';
  6. import 'package:flowy_sdk/protobuf/flowy-workspace/app_create.pb.dart';
  7. import 'package:flowy_sdk/protobuf/flowy-workspace/app_query.pb.dart';
  8. import 'package:flowy_sdk/protobuf/flowy-workspace/errors.pb.dart';
  9. import 'package:flowy_sdk/protobuf/flowy-workspace/observable.pb.dart';
  10. import 'package:flowy_sdk/protobuf/flowy-workspace/view_create.pb.dart';
  11. import 'package:flowy_sdk/protobuf/flowy-workspace/view_create.pbenum.dart';
  12. import 'package:flowy_sdk/rust_stream.dart';
  13. typedef AppUpdatedCallback = void Function(String name, String desc);
  14. typedef ViewUpdatedCallback = void Function(List<View> views);
  15. class AppRepository {
  16. StreamSubscription<ObservableSubject>? _subscription;
  17. ViewUpdatedCallback? _viewUpdatedCallback;
  18. AppUpdatedCallback? _appUpdatedCallback;
  19. String appId;
  20. AppRepository({
  21. required this.appId,
  22. });
  23. Future<Either<App, WorkspaceError>> getAppDesc() {
  24. final request = QueryAppRequest.create()
  25. ..appId = appId
  26. ..readViews = false;
  27. return WorkspaceEventGetApp(request).send();
  28. }
  29. Future<Either<View, WorkspaceError>> createView(
  30. String appId, String name, String desc, ViewTypeIdentifier viewType) {
  31. final request = CreateViewRequest.create()
  32. ..appId = appId
  33. ..name = name
  34. ..desc = desc
  35. ..viewType = viewType;
  36. return WorkspaceEventCreateView(request).send();
  37. }
  38. Future<Either<List<View>, WorkspaceError>> getViews({required String appId}) {
  39. final request = QueryAppRequest.create()
  40. ..appId = appId
  41. ..readViews = true;
  42. return WorkspaceEventGetApp(request).send().then((result) {
  43. return result.fold(
  44. (app) => left(app.views.items),
  45. (error) => right(error),
  46. );
  47. });
  48. }
  49. void startWatching(
  50. {ViewUpdatedCallback? viewUpdatedCallback,
  51. AppUpdatedCallback? appUpdatedCallback}) {
  52. _viewUpdatedCallback = viewUpdatedCallback;
  53. _appUpdatedCallback = appUpdatedCallback;
  54. _subscription = RustStreamReceiver.listen((observable) {
  55. if (observable.subjectId != appId) {
  56. return;
  57. }
  58. final ty = WorkspaceObservableType.valueOf(observable.ty);
  59. if (ty != null) {
  60. _handleObservableType(ty);
  61. }
  62. });
  63. }
  64. void _handleObservableType(WorkspaceObservableType ty) {
  65. switch (ty) {
  66. case WorkspaceObservableType.ViewUpdated:
  67. if (_viewUpdatedCallback == null) {
  68. return;
  69. }
  70. getViews(appId: appId).then((result) {
  71. result.fold(
  72. (views) => _viewUpdatedCallback!(views),
  73. (error) => Log.error(error),
  74. );
  75. });
  76. break;
  77. case WorkspaceObservableType.AppDescUpdated:
  78. if (_appUpdatedCallback == null) {
  79. return;
  80. }
  81. getAppDesc().then((result) {
  82. result.fold(
  83. (app) => _appUpdatedCallback!(app.name, app.desc),
  84. (error) => Log.error(error),
  85. );
  86. });
  87. break;
  88. default:
  89. break;
  90. }
  91. }
  92. Future<void> close() async {
  93. await _subscription?.cancel();
  94. }
  95. }