view_service.dart 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. /// The [index] is the index of the view in the parent view.
  31. /// If the index is null, the view will be added to the end of the list.
  32. int? index,
  33. }) {
  34. final payload = CreateViewPayloadPB.create()
  35. ..parentViewId = parentViewId
  36. ..name = name
  37. ..desc = desc ?? ""
  38. ..layout = layoutType
  39. ..setAsCurrent = openAfterCreate
  40. ..initialData = initialDataBytes ?? [];
  41. if (ext.isNotEmpty) {
  42. payload.meta.addAll(ext);
  43. }
  44. if (desc != null) {
  45. payload.desc = desc;
  46. }
  47. if (index != null) {
  48. payload.index = index;
  49. }
  50. return FolderEventCreateView(payload).send();
  51. }
  52. /// The orphan view is meant to be a view that is not attached to any parent view. By default, this
  53. /// view will not be shown in the view list unless it is attached to a parent view that is shown in
  54. /// the view list.
  55. static Future<Either<ViewPB, FlowyError>> createOrphanView({
  56. required String viewId,
  57. required ViewLayoutPB layoutType,
  58. required String name,
  59. String? desc,
  60. /// The initial data should be a JSON that represent the DocumentDataPB.
  61. /// Currently, only support create document with initial data.
  62. List<int>? initialDataBytes,
  63. }) {
  64. final payload = CreateOrphanViewPayloadPB.create()
  65. ..viewId = viewId
  66. ..name = name
  67. ..desc = desc ?? ""
  68. ..layout = layoutType
  69. ..initialData = initialDataBytes ?? [];
  70. return FolderEventCreateOrphanView(payload).send();
  71. }
  72. static Future<Either<ViewPB, FlowyError>> createDatabaseLinkedView({
  73. required String parentViewId,
  74. required String databaseId,
  75. required ViewLayoutPB layoutType,
  76. required String name,
  77. }) {
  78. return ViewBackendService.createView(
  79. layoutType: layoutType,
  80. parentViewId: parentViewId,
  81. name: name,
  82. openAfterCreate: false,
  83. ext: {
  84. 'database_id': databaseId,
  85. },
  86. );
  87. }
  88. /// Returns a list of views that are the children of the given [viewId].
  89. static Future<Either<List<ViewPB>, FlowyError>> getChildViews({
  90. required String viewId,
  91. }) {
  92. final payload = ViewIdPB.create()..value = viewId;
  93. return FolderEventReadView(payload).send().then((result) {
  94. return result.fold(
  95. (view) => left(view.childViews),
  96. (error) => right(error),
  97. );
  98. });
  99. }
  100. static Future<Either<Unit, FlowyError>> delete({required String viewId}) {
  101. final request = RepeatedViewIdPB.create()..items.add(viewId);
  102. return FolderEventDeleteView(request).send();
  103. }
  104. static Future<Either<Unit, FlowyError>> deleteView({required String viewId}) {
  105. final request = RepeatedViewIdPB.create()..items.add(viewId);
  106. return FolderEventDeleteView(request).send();
  107. }
  108. static Future<Either<Unit, FlowyError>> duplicate({required ViewPB view}) {
  109. return FolderEventDuplicateView(view).send();
  110. }
  111. static Future<Either<Unit, FlowyError>> favorite({required String viewId}) {
  112. final request = RepeatedViewIdPB.create()..items.add(viewId);
  113. return FolderEventToggleFavorite(request).send();
  114. }
  115. static Future<Either<ViewPB, FlowyError>> updateView({
  116. required String viewId,
  117. String? name,
  118. bool? isFavorite,
  119. }) {
  120. final payload = UpdateViewPayloadPB.create()..viewId = viewId;
  121. if (name != null) {
  122. payload.name = name;
  123. }
  124. if (isFavorite != null) {
  125. payload.isFavorite = isFavorite;
  126. }
  127. return FolderEventUpdateView(payload).send();
  128. }
  129. // deprecated
  130. static Future<Either<Unit, FlowyError>> moveView({
  131. required String viewId,
  132. required int fromIndex,
  133. required int toIndex,
  134. }) {
  135. final payload = MoveViewPayloadPB.create()
  136. ..viewId = viewId
  137. ..from = fromIndex
  138. ..to = toIndex;
  139. return FolderEventMoveView(payload).send();
  140. }
  141. /// Move the view to the new parent view.
  142. ///
  143. /// supports nested view
  144. /// if the [prevViewId] is null, the view will be moved to the beginning of the list
  145. static Future<Either<Unit, FlowyError>> moveViewV2({
  146. required String viewId,
  147. required String newParentId,
  148. required String? prevViewId,
  149. }) {
  150. final payload = MoveNestedViewPayloadPB(
  151. viewId: viewId,
  152. newParentId: newParentId,
  153. prevViewId: prevViewId,
  154. );
  155. return FolderEventMoveNestedView(payload).send();
  156. }
  157. Future<List<ViewPB>> fetchViewsWithLayoutType(
  158. ViewLayoutPB? layoutType,
  159. ) async {
  160. final views = await fetchViews();
  161. if (layoutType == null) {
  162. return views;
  163. }
  164. return views
  165. .where(
  166. (element) => layoutType == element.layout,
  167. )
  168. .toList();
  169. }
  170. Future<List<ViewPB>> fetchViews() async {
  171. final result = <ViewPB>[];
  172. return FolderEventGetCurrentWorkspace().send().then((value) async {
  173. final workspaces = value.getLeftOrNull<WorkspaceSettingPB>();
  174. if (workspaces != null) {
  175. final views = workspaces.workspace.views;
  176. for (final view in views) {
  177. result.add(view);
  178. final childViews = await getAllViews(view);
  179. result.addAll(childViews);
  180. }
  181. }
  182. return result;
  183. });
  184. }
  185. Future<List<ViewPB>> getAllViews(ViewPB view) async {
  186. final result = <ViewPB>[];
  187. final childViews = await getChildViews(viewId: view.id).then(
  188. (value) => value.getLeftOrNull<List<ViewPB>>()?.toList(),
  189. );
  190. if (childViews != null && childViews.isNotEmpty) {
  191. result.addAll(childViews);
  192. final views = await Future.wait(
  193. childViews.map((e) async => await getAllViews(e)),
  194. );
  195. result.addAll(views.expand((element) => element));
  196. }
  197. return result;
  198. }
  199. static Future<Either<ViewPB, FlowyError>> getView(
  200. String viewID,
  201. ) async {
  202. final payload = ViewIdPB.create()..value = viewID;
  203. return FolderEventReadView(payload).send();
  204. }
  205. Future<Either<ViewPB, FlowyError>> getChildView({
  206. required String parentViewId,
  207. required String childViewId,
  208. }) async {
  209. final payload = ViewIdPB.create()..value = parentViewId;
  210. return FolderEventReadView(payload).send().then((result) {
  211. return result.fold(
  212. (app) => left(
  213. app.childViews.firstWhere((e) => e.id == childViewId),
  214. ),
  215. (error) => right(error),
  216. );
  217. });
  218. }
  219. }
  220. extension AppFlowy on Either {
  221. T? getLeftOrNull<T>() {
  222. if (isLeft()) {
  223. final result = fold<T?>((l) => l, (r) => null);
  224. return result;
  225. }
  226. return null;
  227. }
  228. }