From 4a8536d403855cf331b40f60311338be10f920f5 Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Fri, 21 Jun 2024 21:02:05 +0800 Subject: [PATCH] feat: integrate publish api --- .../application/document_share_bloc.dart | 33 +++++++++++++++++-- .../presentation/share/publish_tab.dart | 5 ++- .../application/view/view_service.dart | 27 +++++++++++++++ 3 files changed, 62 insertions(+), 3 deletions(-) diff --git a/frontend/appflowy_flutter/lib/plugins/document/application/document_share_bloc.dart b/frontend/appflowy_flutter/lib/plugins/document/application/document_share_bloc.dart index 1dff1fbe34..a9b4a0d84f 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/application/document_share_bloc.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/application/document_share_bloc.dart @@ -1,11 +1,13 @@ import 'dart:io'; import 'package:appflowy/workspace/application/export/document_exporter.dart'; +import 'package:appflowy/workspace/application/view/view_service.dart'; import 'package:appflowy_backend/log.dart'; import 'package:appflowy_backend/protobuf/flowy-document/entities.pb.dart'; import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart'; import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart'; import 'package:appflowy_result/appflowy_result.dart'; +import 'package:flowy_infra/uuid.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; @@ -35,16 +37,41 @@ class DocumentShareBloc extends Bloc { ); }, publish: (url) async { - emit(state.copyWith(isPublished: true)); + // todo: optimize the logic + const spaceName = 'appflowy'; + final name = '${view.name}-${uuid()}'; + + // set space name + try { + await ViewBackendService.setPublishNameSpace(spaceName) + .getOrThrow(); + await ViewBackendService.publish(view, name: name).getOrThrow(); + } catch (e) { + Log.error('publish error: $e'); + } + + emit( + state.copyWith( + isPublished: true, + url: 'https://test.appflowy.io/$spaceName/$name', + ), + ); }, unPublish: () async { - emit(state.copyWith(isPublished: false)); + await ViewBackendService.unpublish(view); + emit( + state.copyWith( + isPublished: false, + url: '', + ), + ); }, ); }); } final ViewPB view; + late final exporter = DocumentExporter(view); Future> _export( @@ -124,10 +151,12 @@ class DocumentShareState with _$DocumentShareState { FlowyResult? exportResult, required bool isPublished, FlowyResult? publishResult, + required String url, }) = _DocumentShareState; factory DocumentShareState.initial() => const DocumentShareState( isLoading: false, isPublished: false, + url: '', ); } diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/share/publish_tab.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/share/publish_tab.dart index c1fefbc3fe..981276856b 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/share/publish_tab.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/share/publish_tab.dart @@ -1,6 +1,7 @@ import 'package:appflowy/generated/flowy_svgs.g.dart'; import 'package:appflowy/generated/locale_keys.g.dart'; import 'package:appflowy/plugins/document/application/document_share_bloc.dart'; +import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:flowy_infra_ui/flowy_infra_ui.dart'; import 'package:flowy_infra_ui/widget/rounded_button.dart'; @@ -81,7 +82,9 @@ class _PublishedWidgetState extends State<_PublishedWidget> { name: LocaleKeys.shareAction_visitSite.tr(), backgroundColor: Theme.of(context).colorScheme.primary, textColor: Colors.white, - onTap: () {}, + onTap: () { + safeLaunchUrl(controller.text); + }, ), ], ), diff --git a/frontend/appflowy_flutter/lib/workspace/application/view/view_service.dart b/frontend/appflowy_flutter/lib/workspace/application/view/view_service.dart index d123e72c4f..4c92f2b1cc 100644 --- a/frontend/appflowy_flutter/lib/workspace/application/view/view_service.dart +++ b/frontend/appflowy_flutter/lib/workspace/application/view/view_service.dart @@ -271,4 +271,31 @@ class ViewBackendService { ); return FolderEventUpdateViewVisibilityStatus(payload).send(); } + + static Future> publish( + ViewPB view, { + String? name, + }) async { + final payload = PublishViewParamsPB()..viewId = view.id; + + if (name != null) { + payload.publishName = name; + } + + return FolderEventPublishView(payload).send(); + } + + static Future> unpublish( + ViewPB view, + ) async { + final payload = UnpublishViewsPayloadPB(viewIds: [view.id]); + return FolderEventUnpublishViews(payload).send(); + } + + static Future> setPublishNameSpace( + String name, + ) async { + final payload = SetPublishNamespacePayloadPB()..newNamespace = name; + return FolderEventSetPublishNamespace(payload).send(); + } }