app_service.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import 'dart:async';
  2. import 'package:appflowy_backend/protobuf/flowy-folder2/workspace.pb.dart';
  3. import 'package:dartz/dartz.dart';
  4. import 'package:appflowy_backend/dispatch/dispatch.dart';
  5. import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
  6. import 'package:appflowy_backend/protobuf/flowy-folder2/view.pb.dart';
  7. class AppBackendService {
  8. Future<Either<ViewPB, FlowyError>> createView({
  9. required String appId,
  10. required String name,
  11. String? desc,
  12. required ViewLayoutPB layoutType,
  13. /// The initial data should be a JSON that represent the DocumentDataPB.
  14. /// Currently, only support create document with initial data.
  15. List<int>? initialDataBytes,
  16. /// The [ext] is used to pass through the custom configuration
  17. /// to the backend.
  18. /// Linking the view to the existing database, it needs to pass
  19. /// the database id. For example: "database_id": "xxx"
  20. ///
  21. Map<String, String> ext = const {},
  22. }) {
  23. final payload = CreateViewPayloadPB.create()
  24. ..belongToId = appId
  25. ..name = name
  26. ..desc = desc ?? ""
  27. ..layout = layoutType
  28. ..initialData = initialDataBytes ?? [];
  29. if (ext.isNotEmpty) {
  30. payload.ext.addAll(ext);
  31. }
  32. return FolderEventCreateView(payload).send();
  33. }
  34. Future<Either<List<ViewPB>, FlowyError>> getViews({required String viewId}) {
  35. final payload = ViewIdPB.create()..value = viewId;
  36. return FolderEventReadView(payload).send().then((result) {
  37. return result.fold(
  38. (app) => left(app.childViews),
  39. (error) => right(error),
  40. );
  41. });
  42. }
  43. Future<Either<Unit, FlowyError>> delete({required String viewId}) {
  44. final request = RepeatedViewIdPB.create()..items.add(viewId);
  45. return FolderEventDeleteView(request).send();
  46. }
  47. Future<Either<Unit, FlowyError>> deleteView({required String viewId}) {
  48. final request = RepeatedViewIdPB.create()..items.add(viewId);
  49. return FolderEventDeleteView(request).send();
  50. }
  51. Future<Either<ViewPB, FlowyError>> updateApp({
  52. required String appId,
  53. String? name,
  54. }) {
  55. var payload = UpdateViewPayloadPB.create()..viewId = appId;
  56. if (name != null) {
  57. payload.name = name;
  58. }
  59. return FolderEventUpdateView(payload).send();
  60. }
  61. Future<Either<Unit, FlowyError>> moveView({
  62. required String viewId,
  63. required int fromIndex,
  64. required int toIndex,
  65. }) {
  66. final payload = MoveFolderItemPayloadPB.create()
  67. ..itemId = viewId
  68. ..from = fromIndex
  69. ..to = toIndex
  70. ..ty = MoveFolderItemType.MoveView;
  71. return FolderEventMoveItem(payload).send();
  72. }
  73. Future<List<(ViewPB, List<ViewPB>)>> fetchViews(
  74. ViewLayoutPB layoutType,
  75. ) async {
  76. final result = <(ViewPB, List<ViewPB>)>[];
  77. return FolderEventReadCurrentWorkspace().send().then((value) async {
  78. final workspaces = value.getLeftOrNull<WorkspaceSettingPB>();
  79. if (workspaces != null) {
  80. final views = workspaces.workspace.views;
  81. for (var view in views) {
  82. final childViews = await getViews(viewId: view.id).then(
  83. (value) => value
  84. .getLeftOrNull<List<ViewPB>>()
  85. ?.where((e) => e.layout == layoutType)
  86. .toList(),
  87. );
  88. if (childViews != null && childViews.isNotEmpty) {
  89. result.add((view, childViews));
  90. }
  91. }
  92. }
  93. return result;
  94. });
  95. }
  96. Future<Either<ViewPB, FlowyError>> getView(
  97. String viewID,
  98. ) async {
  99. final payload = ViewIdPB.create()..value = viewID;
  100. return FolderEventReadView(payload).send();
  101. }
  102. Future<Either<ViewPB, FlowyError>> getChildView(
  103. String viewID,
  104. String childViewID,
  105. ) async {
  106. final payload = ViewIdPB.create()..value = viewID;
  107. return FolderEventReadView(payload).send().then((result) {
  108. return result.fold(
  109. (app) => left(
  110. app.childViews.firstWhere((e) => e.id == childViewID),
  111. ),
  112. (error) => right(error),
  113. );
  114. });
  115. }
  116. }
  117. extension AppFlowy on Either {
  118. T? getLeftOrNull<T>() {
  119. if (isLeft()) {
  120. final result = fold<T?>((l) => l, (r) => null);
  121. return result;
  122. }
  123. return null;
  124. }
  125. }