app_service.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'package:appflowy_backend/protobuf/flowy-folder2/workspace.pb.dart';
  4. import 'package:dartz/dartz.dart';
  5. import 'package:appflowy_backend/dispatch/dispatch.dart';
  6. import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
  7. import 'package:appflowy_backend/protobuf/flowy-folder2/view.pb.dart';
  8. class AppBackendService {
  9. Future<Either<ViewPB, FlowyError>> createView({
  10. required String appId,
  11. required String name,
  12. String? desc,
  13. required ViewLayoutPB layoutType,
  14. /// The initial data should be the JSON of the document.
  15. /// Currently, only support create document with initial data.
  16. ///
  17. /// The initial data must be follow this format as shown below.
  18. /// {"document":{"type":"editor","children":[]}}
  19. String? initialData,
  20. /// The [ext] is used to pass through the custom configuration
  21. /// to the backend.
  22. /// Linking the view to the existing database, it needs to pass
  23. /// the database id. For example: "database_id": "xxx"
  24. ///
  25. Map<String, String> ext = const {},
  26. }) {
  27. final payload = CreateViewPayloadPB.create()
  28. ..belongToId = appId
  29. ..name = name
  30. ..desc = desc ?? ""
  31. ..layout = layoutType
  32. ..initialData = utf8.encode(
  33. initialData ?? "",
  34. );
  35. if (ext.isNotEmpty) {
  36. payload.ext.addAll(ext);
  37. }
  38. return FolderEventCreateView(payload).send();
  39. }
  40. Future<Either<List<ViewPB>, FlowyError>> getViews({required String viewId}) {
  41. final payload = ViewIdPB.create()..value = viewId;
  42. return FolderEventReadView(payload).send().then((result) {
  43. return result.fold(
  44. (app) => left(app.childViews),
  45. (error) => right(error),
  46. );
  47. });
  48. }
  49. Future<Either<Unit, FlowyError>> delete({required String viewId}) {
  50. final request = RepeatedViewIdPB.create()..items.add(viewId);
  51. return FolderEventDeleteView(request).send();
  52. }
  53. Future<Either<Unit, FlowyError>> deleteView({required String viewId}) {
  54. final request = RepeatedViewIdPB.create()..items.add(viewId);
  55. return FolderEventDeleteView(request).send();
  56. }
  57. Future<Either<ViewPB, FlowyError>> updateApp({
  58. required String appId,
  59. String? name,
  60. }) {
  61. var payload = UpdateViewPayloadPB.create()..viewId = appId;
  62. if (name != null) {
  63. payload.name = name;
  64. }
  65. return FolderEventUpdateView(payload).send();
  66. }
  67. Future<Either<Unit, FlowyError>> moveView({
  68. required String viewId,
  69. required int fromIndex,
  70. required int toIndex,
  71. }) {
  72. final payload = MoveFolderItemPayloadPB.create()
  73. ..itemId = viewId
  74. ..from = fromIndex
  75. ..to = toIndex
  76. ..ty = MoveFolderItemType.MoveView;
  77. return FolderEventMoveItem(payload).send();
  78. }
  79. Future<List<Tuple2<ViewPB, List<ViewPB>>>> fetchViews(
  80. ViewLayoutPB layoutType,
  81. ) async {
  82. final result = <Tuple2<ViewPB, List<ViewPB>>>[];
  83. return FolderEventReadCurrentWorkspace().send().then((value) async {
  84. final workspaces = value.getLeftOrNull<WorkspaceSettingPB>();
  85. if (workspaces != null) {
  86. final views = workspaces.workspace.views;
  87. for (var view in views) {
  88. final childViews = await getViews(viewId: view.id).then(
  89. (value) => value
  90. .getLeftOrNull<List<ViewPB>>()
  91. ?.where((e) => e.layout == layoutType)
  92. .toList(),
  93. );
  94. if (childViews != null && childViews.isNotEmpty) {
  95. result.add(Tuple2(view, childViews));
  96. }
  97. }
  98. }
  99. return result;
  100. });
  101. }
  102. Future<Either<ViewPB, FlowyError>> getView(
  103. String viewID,
  104. ) async {
  105. final payload = ViewIdPB.create()..value = viewID;
  106. return FolderEventReadView(payload).send();
  107. }
  108. Future<Either<ViewPB, FlowyError>> getChildView(
  109. String viewID,
  110. String childViewID,
  111. ) async {
  112. final payload = ViewIdPB.create()..value = viewID;
  113. return FolderEventReadView(payload).send().then((result) {
  114. return result.fold(
  115. (app) => left(
  116. app.childViews.firstWhere((e) => e.id == childViewID),
  117. ),
  118. (error) => right(error),
  119. );
  120. });
  121. }
  122. }
  123. extension AppFlowy on Either {
  124. T? getLeftOrNull<T>() {
  125. if (isLeft()) {
  126. final result = fold<T?>((l) => l, (r) => null);
  127. return result;
  128. }
  129. return null;
  130. }
  131. }