app_service.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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.belongings),
  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, String? name}) {
  59. var payload = UpdateViewPayloadPB.create()..viewId = appId;
  60. if (name != null) {
  61. payload.name = name;
  62. }
  63. return FolderEventUpdateView(payload).send();
  64. }
  65. Future<Either<Unit, FlowyError>> moveView({
  66. required String viewId,
  67. required int fromIndex,
  68. required int toIndex,
  69. }) {
  70. final payload = MoveFolderItemPayloadPB.create()
  71. ..itemId = viewId
  72. ..from = fromIndex
  73. ..to = toIndex
  74. ..ty = MoveFolderItemType.MoveView;
  75. return FolderEventMoveItem(payload).send();
  76. }
  77. Future<List<Tuple2<ViewPB, List<ViewPB>>>> fetchViews(
  78. ViewLayoutPB layoutType) async {
  79. final result = <Tuple2<ViewPB, List<ViewPB>>>[];
  80. return FolderEventReadCurrentWorkspace().send().then((value) async {
  81. final workspaces = value.getLeftOrNull<WorkspaceSettingPB>();
  82. if (workspaces != null) {
  83. final views = workspaces.workspace.views;
  84. for (var view in views) {
  85. final childViews = await getViews(viewId: view.id).then(
  86. (value) => value
  87. .getLeftOrNull<List<ViewPB>>()
  88. ?.where((e) => e.layout == layoutType)
  89. .toList(),
  90. );
  91. if (childViews != null && childViews.isNotEmpty) {
  92. result.add(Tuple2(view, childViews));
  93. }
  94. }
  95. }
  96. return result;
  97. });
  98. }
  99. Future<Either<ViewPB, FlowyError>> getView(
  100. String viewID,
  101. ) async {
  102. final payload = ViewIdPB.create()..value = viewID;
  103. return FolderEventReadView(payload).send();
  104. }
  105. Future<Either<ViewPB, FlowyError>> getChildView(
  106. String viewID,
  107. String childViewID,
  108. ) async {
  109. final payload = ViewIdPB.create()..value = viewID;
  110. return FolderEventReadView(payload).send().then((result) {
  111. return result.fold(
  112. (app) => left(
  113. app.belongings.firstWhere((e) => e.id == childViewID),
  114. ),
  115. (error) => right(error),
  116. );
  117. });
  118. }
  119. }
  120. extension AppFlowy on Either {
  121. T? getLeftOrNull<T>() {
  122. if (isLeft()) {
  123. final result = fold<T?>((l) => l, (r) => null);
  124. return result;
  125. }
  126. return null;
  127. }
  128. }