view_service.dart 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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>> createDatabaseLinkedView({
  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>> getChildViews({
  81. required String viewId,
  82. }) {
  83. final payload = ViewIdPB.create()..value = viewId;
  84. return FolderEventReadView(payload).send().then((result) {
  85. return result.fold(
  86. (view) => left(view.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>)>> fetchViewsWithLayoutType(
  132. ViewLayoutPB? layoutType,
  133. ) async {
  134. return fetchViews((workspace, view) {
  135. if (layoutType != null) {
  136. return view.layout == layoutType;
  137. }
  138. return true;
  139. });
  140. }
  141. Future<List<(ViewPB, List<ViewPB>)>> fetchViews(
  142. bool Function(WorkspaceSettingPB workspace, ViewPB view) filter,
  143. ) async {
  144. final result = <(ViewPB, List<ViewPB>)>[];
  145. return FolderEventGetCurrentWorkspace().send().then((value) async {
  146. final workspaces = value.getLeftOrNull<WorkspaceSettingPB>();
  147. if (workspaces != null) {
  148. final views = workspaces.workspace.views;
  149. for (final view in views) {
  150. final childViews = await getChildViews(viewId: view.id).then(
  151. (value) => value
  152. .getLeftOrNull<List<ViewPB>>()
  153. ?.where((e) => filter(workspaces, e))
  154. .toList(),
  155. );
  156. if (childViews != null && childViews.isNotEmpty) {
  157. result.add((view, childViews));
  158. }
  159. }
  160. }
  161. return result;
  162. });
  163. }
  164. static Future<Either<ViewPB, FlowyError>> getView(
  165. String viewID,
  166. ) async {
  167. final payload = ViewIdPB.create()..value = viewID;
  168. return FolderEventReadView(payload).send();
  169. }
  170. Future<Either<ViewPB, FlowyError>> getChildView({
  171. required String parentViewId,
  172. required String childViewId,
  173. }) async {
  174. final payload = ViewIdPB.create()..value = parentViewId;
  175. return FolderEventReadView(payload).send().then((result) {
  176. return result.fold(
  177. (app) => left(
  178. app.childViews.firstWhere((e) => e.id == childViewId),
  179. ),
  180. (error) => right(error),
  181. );
  182. });
  183. }
  184. }
  185. extension AppFlowy on Either {
  186. T? getLeftOrNull<T>() {
  187. if (isLeft()) {
  188. final result = fold<T?>((l) => l, (r) => null);
  189. return result;
  190. }
  191. return null;
  192. }
  193. }