view_service.dart 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 ViewBackendService {
  8. static Future<Either<ViewPB, FlowyError>> createView({
  9. /// The [layoutType] is the type of the view.
  10. required ViewLayoutPB layoutType,
  11. /// The [parentViewId] is the parent view id.
  12. required String parentViewId,
  13. /// The [name] is the name of the view.
  14. required String name,
  15. String? desc,
  16. /// The default value of [openAfterCreate] is false, meaning the view will
  17. /// not be opened nor set as the current view. However, if set to true, the
  18. /// view will be opened and set as the current view. Upon relaunching the
  19. /// app, this view will be opened
  20. bool openAfterCreate = false,
  21. /// The initial data should be a JSON that represent the DocumentDataPB.
  22. /// Currently, only support create document with initial data.
  23. List<int>? initialDataBytes,
  24. /// The [ext] is used to pass through the custom configuration
  25. /// to the backend.
  26. /// Linking the view to the existing database, it needs to pass
  27. /// the database id. For example: "database_id": "xxx"
  28. ///
  29. Map<String, String> ext = const {},
  30. }) {
  31. final payload = CreateViewPayloadPB.create()
  32. ..parentViewId = parentViewId
  33. ..name = name
  34. ..desc = desc ?? ""
  35. ..layout = layoutType
  36. ..setAsCurrent = openAfterCreate
  37. ..initialData = initialDataBytes ?? [];
  38. if (ext.isNotEmpty) {
  39. payload.meta.addAll(ext);
  40. }
  41. return FolderEventCreateView(payload).send();
  42. }
  43. /// The orphan view is meant to be a view that is not attached to any parent view. By default, this
  44. /// view will not be shown in the view list unless it is attached to a parent view that is shown in
  45. /// the view list.
  46. static Future<Either<ViewPB, FlowyError>> createOrphanView({
  47. required String viewId,
  48. required ViewLayoutPB layoutType,
  49. required String name,
  50. String? desc,
  51. /// The initial data should be a JSON that represent the DocumentDataPB.
  52. /// Currently, only support create document with initial data.
  53. List<int>? initialDataBytes,
  54. }) {
  55. final payload = CreateOrphanViewPayloadPB.create()
  56. ..viewId = viewId
  57. ..name = name
  58. ..desc = desc ?? ""
  59. ..layout = layoutType
  60. ..initialData = initialDataBytes ?? [];
  61. return FolderEventCreateOrphanView(payload).send();
  62. }
  63. static Future<Either<ViewPB, FlowyError>> createDatabaseReferenceView({
  64. required String parentViewId,
  65. required String databaseId,
  66. required ViewLayoutPB layoutType,
  67. required String name,
  68. }) {
  69. return ViewBackendService.createView(
  70. layoutType: layoutType,
  71. parentViewId: parentViewId,
  72. name: name,
  73. openAfterCreate: false,
  74. ext: {
  75. 'database_id': databaseId,
  76. },
  77. );
  78. }
  79. /// Returns a list of views that are the children of the given [viewId].
  80. static Future<Either<List<ViewPB>, FlowyError>> getViews({
  81. required String viewId,
  82. }) {
  83. final payload = ViewIdPB.create()..value = viewId;
  84. return FolderEventReadView(payload).send().then((result) {
  85. return result.fold(
  86. (app) => left(app.childViews),
  87. (error) => right(error),
  88. );
  89. });
  90. }
  91. static Future<Either<Unit, FlowyError>> delete({required String viewId}) {
  92. final request = RepeatedViewIdPB.create()..items.add(viewId);
  93. return FolderEventDeleteView(request).send();
  94. }
  95. static Future<Either<Unit, FlowyError>> deleteView({required String viewId}) {
  96. final request = RepeatedViewIdPB.create()..items.add(viewId);
  97. return FolderEventDeleteView(request).send();
  98. }
  99. static Future<Either<Unit, FlowyError>> duplicate({required ViewPB view}) {
  100. return FolderEventDuplicateView(view).send();
  101. }
  102. static Future<Either<ViewPB, FlowyError>> updateView({
  103. required String viewId,
  104. String? name,
  105. String? iconURL,
  106. String? coverURL,
  107. }) {
  108. final payload = UpdateViewPayloadPB.create()..viewId = viewId;
  109. if (name != null) {
  110. payload.name = name;
  111. }
  112. if (iconURL != null) {
  113. payload.iconUrl = iconURL;
  114. }
  115. if (coverURL != null) {
  116. payload.coverUrl = coverURL;
  117. }
  118. return FolderEventUpdateView(payload).send();
  119. }
  120. static Future<Either<Unit, FlowyError>> moveView({
  121. required String viewId,
  122. required int fromIndex,
  123. required int toIndex,
  124. }) {
  125. final payload = MoveViewPayloadPB.create()
  126. ..viewId = viewId
  127. ..from = fromIndex
  128. ..to = toIndex;
  129. return FolderEventMoveView(payload).send();
  130. }
  131. Future<List<(ViewPB, List<ViewPB>)>> fetchViews(
  132. ViewLayoutPB layoutType,
  133. ) async {
  134. final result = <(ViewPB, List<ViewPB>)>[];
  135. return FolderEventGetCurrentWorkspace().send().then((value) async {
  136. final workspaces = value.getLeftOrNull<WorkspaceSettingPB>();
  137. if (workspaces != null) {
  138. final views = workspaces.workspace.views;
  139. for (final view in views) {
  140. final childViews = await getViews(viewId: view.id).then(
  141. (value) => value
  142. .getLeftOrNull<List<ViewPB>>()
  143. ?.where((e) => e.layout == layoutType)
  144. .toList(),
  145. );
  146. if (childViews != null && childViews.isNotEmpty) {
  147. result.add((view, childViews));
  148. }
  149. }
  150. }
  151. return result;
  152. });
  153. }
  154. static Future<Either<ViewPB, FlowyError>> getView(
  155. String viewID,
  156. ) async {
  157. final payload = ViewIdPB.create()..value = viewID;
  158. return FolderEventReadView(payload).send();
  159. }
  160. Future<Either<ViewPB, FlowyError>> getChildView({
  161. required String parentViewId,
  162. required String childViewId,
  163. }) async {
  164. final payload = ViewIdPB.create()..value = parentViewId;
  165. return FolderEventReadView(payload).send().then((result) {
  166. return result.fold(
  167. (app) => left(
  168. app.childViews.firstWhere((e) => e.id == childViewId),
  169. ),
  170. (error) => right(error),
  171. );
  172. });
  173. }
  174. }
  175. extension AppFlowy on Either {
  176. T? getLeftOrNull<T>() {
  177. if (isLeft()) {
  178. final result = fold<T?>((l) => l, (r) => null);
  179. return result;
  180. }
  181. return null;
  182. }
  183. }