view_service.dart 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. // deprecated
  121. static Future<Either<Unit, FlowyError>> moveView({
  122. required String viewId,
  123. required int fromIndex,
  124. required int toIndex,
  125. }) {
  126. final payload = MoveViewPayloadPB.create()
  127. ..viewId = viewId
  128. ..from = fromIndex
  129. ..to = toIndex;
  130. return FolderEventMoveView(payload).send();
  131. }
  132. /// Move the view to the new parent view.
  133. ///
  134. /// supports nested view
  135. /// if the [prevViewId] is null, the view will be moved to the beginning of the list
  136. static Future<Either<Unit, FlowyError>> moveViewV2({
  137. required String viewId,
  138. required String newParentId,
  139. required String? prevViewId,
  140. }) {
  141. final payload = MoveNestedViewPayloadPB(
  142. viewId: viewId,
  143. newParentId: newParentId,
  144. prevViewId: prevViewId,
  145. );
  146. return FolderEventMoveNestedView(payload).send();
  147. }
  148. Future<List<(ViewPB, List<ViewPB>)>> fetchViewsWithLayoutType(
  149. ViewLayoutPB? layoutType,
  150. ) async {
  151. return fetchViews((workspace, view) {
  152. if (layoutType != null) {
  153. return view.layout == layoutType;
  154. }
  155. return true;
  156. });
  157. }
  158. Future<List<(ViewPB, List<ViewPB>)>> fetchViews(
  159. bool Function(WorkspaceSettingPB workspace, ViewPB view) filter,
  160. ) async {
  161. final result = <(ViewPB, List<ViewPB>)>[];
  162. return FolderEventGetCurrentWorkspace().send().then((value) async {
  163. final workspaces = value.getLeftOrNull<WorkspaceSettingPB>();
  164. if (workspaces != null) {
  165. final views = workspaces.workspace.views;
  166. for (final view in views) {
  167. final childViews = await getChildViews(viewId: view.id).then(
  168. (value) => value
  169. .getLeftOrNull<List<ViewPB>>()
  170. ?.where((e) => filter(workspaces, e))
  171. .toList(),
  172. );
  173. if (childViews != null && childViews.isNotEmpty) {
  174. result.add((view, childViews));
  175. }
  176. }
  177. }
  178. return result;
  179. });
  180. }
  181. static Future<Either<ViewPB, FlowyError>> getView(
  182. String viewID,
  183. ) async {
  184. final payload = ViewIdPB.create()..value = viewID;
  185. return FolderEventReadView(payload).send();
  186. }
  187. Future<Either<ViewPB, FlowyError>> getChildView({
  188. required String parentViewId,
  189. required String childViewId,
  190. }) async {
  191. final payload = ViewIdPB.create()..value = parentViewId;
  192. return FolderEventReadView(payload).send().then((result) {
  193. return result.fold(
  194. (app) => left(
  195. app.childViews.firstWhere((e) => e.id == childViewId),
  196. ),
  197. (error) => right(error),
  198. );
  199. });
  200. }
  201. }
  202. extension AppFlowy on Either {
  203. T? getLeftOrNull<T>() {
  204. if (isLeft()) {
  205. final result = fold<T?>((l) => l, (r) => null);
  206. return result;
  207. }
  208. return null;
  209. }
  210. }