view_service.dart 7.9 KB

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