view_service.dart 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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>> fetchViewsWithLayoutType(
  149. ViewLayoutPB? layoutType,
  150. ) async {
  151. final views = await fetchViews();
  152. if (layoutType == null) {
  153. return views;
  154. }
  155. return views
  156. .where(
  157. (element) => layoutType == element.layout,
  158. )
  159. .toList();
  160. }
  161. Future<List<ViewPB>> fetchViews() async {
  162. final result = <ViewPB>[];
  163. return FolderEventGetCurrentWorkspace().send().then((value) async {
  164. final workspaces = value.getLeftOrNull<WorkspaceSettingPB>();
  165. if (workspaces != null) {
  166. final views = workspaces.workspace.views;
  167. for (final view in views) {
  168. result.add(view);
  169. final childViews = await getAllViews(view);
  170. result.addAll(childViews);
  171. }
  172. }
  173. return result;
  174. });
  175. }
  176. Future<List<ViewPB>> getAllViews(ViewPB view) async {
  177. final result = <ViewPB>[];
  178. final childViews = await getChildViews(viewId: view.id).then(
  179. (value) => value.getLeftOrNull<List<ViewPB>>()?.toList(),
  180. );
  181. if (childViews != null && childViews.isNotEmpty) {
  182. result.addAll(childViews);
  183. final views = await Future.wait(
  184. childViews.map((e) async => await getAllViews(e)),
  185. );
  186. result.addAll(views.expand((element) => element));
  187. }
  188. return result;
  189. }
  190. static Future<Either<ViewPB, FlowyError>> getView(
  191. String viewID,
  192. ) async {
  193. final payload = ViewIdPB.create()..value = viewID;
  194. return FolderEventReadView(payload).send();
  195. }
  196. Future<Either<ViewPB, FlowyError>> getChildView({
  197. required String parentViewId,
  198. required String childViewId,
  199. }) async {
  200. final payload = ViewIdPB.create()..value = parentViewId;
  201. return FolderEventReadView(payload).send().then((result) {
  202. return result.fold(
  203. (app) => left(
  204. app.childViews.firstWhere((e) => e.id == childViewId),
  205. ),
  206. (error) => right(error),
  207. );
  208. });
  209. }
  210. }
  211. extension AppFlowy on Either {
  212. T? getLeftOrNull<T>() {
  213. if (isLeft()) {
  214. final result = fold<T?>((l) => l, (r) => null);
  215. return result;
  216. }
  217. return null;
  218. }
  219. }