123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- import 'dart:async';
- import 'package:appflowy_backend/protobuf/flowy-folder2/workspace.pb.dart';
- import 'package:dartz/dartz.dart';
- import 'package:appflowy_backend/dispatch/dispatch.dart';
- import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
- import 'package:appflowy_backend/protobuf/flowy-folder2/view.pb.dart';
- class AppBackendService {
- Future<Either<ViewPB, FlowyError>> createView({
- required String appId,
- required String name,
- String? desc,
- required ViewLayoutPB layoutType,
- /// The initial data should be a JSON that represent the DocumentDataPB.
- /// Currently, only support create document with initial data.
- List<int>? initialDataBytes,
- /// The [ext] is used to pass through the custom configuration
- /// to the backend.
- /// Linking the view to the existing database, it needs to pass
- /// the database id. For example: "database_id": "xxx"
- ///
- Map<String, String> ext = const {},
- }) {
- final payload = CreateViewPayloadPB.create()
- ..belongToId = appId
- ..name = name
- ..desc = desc ?? ""
- ..layout = layoutType
- ..initialData = initialDataBytes ?? [];
- if (ext.isNotEmpty) {
- payload.ext.addAll(ext);
- }
- return FolderEventCreateView(payload).send();
- }
- Future<Either<List<ViewPB>, FlowyError>> getViews({required String viewId}) {
- final payload = ViewIdPB.create()..value = viewId;
- return FolderEventReadView(payload).send().then((result) {
- return result.fold(
- (app) => left(app.childViews),
- (error) => right(error),
- );
- });
- }
- Future<Either<Unit, FlowyError>> delete({required String viewId}) {
- final request = RepeatedViewIdPB.create()..items.add(viewId);
- return FolderEventDeleteView(request).send();
- }
- Future<Either<Unit, FlowyError>> deleteView({required String viewId}) {
- final request = RepeatedViewIdPB.create()..items.add(viewId);
- return FolderEventDeleteView(request).send();
- }
- Future<Either<ViewPB, FlowyError>> updateApp({
- required String appId,
- String? name,
- }) {
- var payload = UpdateViewPayloadPB.create()..viewId = appId;
- if (name != null) {
- payload.name = name;
- }
- return FolderEventUpdateView(payload).send();
- }
- Future<Either<Unit, FlowyError>> moveView({
- required String viewId,
- required int fromIndex,
- required int toIndex,
- }) {
- final payload = MoveFolderItemPayloadPB.create()
- ..itemId = viewId
- ..from = fromIndex
- ..to = toIndex
- ..ty = MoveFolderItemType.MoveView;
- return FolderEventMoveItem(payload).send();
- }
- Future<List<(ViewPB, List<ViewPB>)>> fetchViews(
- ViewLayoutPB layoutType,
- ) async {
- final result = <(ViewPB, List<ViewPB>)>[];
- return FolderEventReadCurrentWorkspace().send().then((value) async {
- final workspaces = value.getLeftOrNull<WorkspaceSettingPB>();
- if (workspaces != null) {
- final views = workspaces.workspace.views;
- for (var view in views) {
- final childViews = await getViews(viewId: view.id).then(
- (value) => value
- .getLeftOrNull<List<ViewPB>>()
- ?.where((e) => e.layout == layoutType)
- .toList(),
- );
- if (childViews != null && childViews.isNotEmpty) {
- result.add((view, childViews));
- }
- }
- }
- return result;
- });
- }
- Future<Either<ViewPB, FlowyError>> getView(
- String viewID,
- ) async {
- final payload = ViewIdPB.create()..value = viewID;
- return FolderEventReadView(payload).send();
- }
- Future<Either<ViewPB, FlowyError>> getChildView(
- String viewID,
- String childViewID,
- ) async {
- final payload = ViewIdPB.create()..value = viewID;
- return FolderEventReadView(payload).send().then((result) {
- return result.fold(
- (app) => left(
- app.childViews.firstWhere((e) => e.id == childViewID),
- ),
- (error) => right(error),
- );
- });
- }
- }
- extension AppFlowy on Either {
- T? getLeftOrNull<T>() {
- if (isLeft()) {
- final result = fold<T?>((l) => l, (r) => null);
- return result;
- }
- return null;
- }
- }
|