From 02199e3a738e206c8f806d468458116137867580 Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Fri, 21 Jun 2024 21:26:52 +0800 Subject: [PATCH] feat: fetch the publish info to show the publish status --- .../application/document_share_bloc.dart | 34 +++++++++++++++---- .../presentation/share/share_button.dart | 3 +- .../application/view/view_service.dart | 13 ++++++- 3 files changed, 41 insertions(+), 9 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 4a90cb8fb8..01621a64cb 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 @@ -13,12 +13,27 @@ import 'package:freezed_annotation/freezed_annotation.dart'; part 'document_share_bloc.freezed.dart'; +const _url = 'https://test.appflowy.io'; + class DocumentShareBloc extends Bloc { DocumentShareBloc({ required this.view, }) : super(DocumentShareState.initial()) { on((event, emit) async { await event.when( + initial: () async { + final publishInfo = await ViewBackendService.getPublishInfo(view); + publishInfo.fold((s) { + emit( + state.copyWith( + isPublished: true, + url: '$_url/${s.namespace}/${s.publishName}', + ), + ); + }, (f) { + emit(state.copyWith(isPublished: false, url: '')); + }); + }, share: (type, path) async { if (DocumentShareType.unimplemented.contains(type)) { Log.error('DocumentShareType $type is not implemented'); @@ -38,22 +53,25 @@ class DocumentShareBloc extends Bloc { }, publish: (url) async { // todo: optimize the logic - const spaceName = 'appflowy'; - final name = '${view.name}-${uuid()}'; + const prefix = 'appflowy'; + final name = '${view.name}-${uuid()}'.substring(0, 19); // set space name - FlowyResult? result; try { - await ViewBackendService.setPublishNameSpace(spaceName) - .getOrThrow(); - result = await ViewBackendService.publish(view, name: name); + final nameSpace = + await ViewBackendService.getPublishNameSpace().getOrThrow(); + if (nameSpace.namespace != prefix) { + await ViewBackendService.setPublishNameSpace(prefix).getOrThrow(); + } + + final result = await ViewBackendService.publish(view, name: name); emit( state.copyWith( isPublished: true, publishResult: result, - url: 'https://test.appflowy.io/$spaceName/$name', + url: '$_url/${nameSpace.namespace}/$name', ), ); } catch (e) { @@ -75,6 +93,7 @@ class DocumentShareBloc extends Bloc { emit( state.copyWith( isPublished: false, + publishResult: null, url: '', ), ); @@ -149,6 +168,7 @@ enum DocumentShareType { @freezed class DocumentShareEvent with _$DocumentShareEvent { + const factory DocumentShareEvent.initial() = _Initial; const factory DocumentShareEvent.share( DocumentShareType type, String? path, diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/share/share_button.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/share/share_button.dart index 333d5908cb..2fed083fe3 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/share/share_button.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/share/share_button.dart @@ -30,7 +30,8 @@ class DocumentShareButton extends StatelessWidget { @override Widget build(BuildContext context) { return BlocProvider( - create: (context) => getIt(param1: view), + create: (context) => getIt(param1: view) + ..add(const DocumentShareEvent.initial()), child: BlocListener( listener: (context, state) { if (state.isLoading == false && state.exportResult != null) { 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 e84fc3205e..8ae9ee077f 100644 --- a/frontend/appflowy_flutter/lib/workspace/application/view/view_service.dart +++ b/frontend/appflowy_flutter/lib/workspace/application/view/view_service.dart @@ -279,6 +279,13 @@ class ViewBackendService { return FolderEventUpdateViewVisibilityStatus(payload).send(); } + static Future> getPublishInfo( + ViewPB view, + ) async { + final payload = ViewIdPB()..value = view.id; + return FolderEventGetPublishInfo(payload).send(); + } + static Future> publish( ViewPB view, { String? name, @@ -288,7 +295,6 @@ class ViewBackendService { if (name != null) { payload.publishName = name; } - return FolderEventPublishView(payload).send(); } @@ -305,4 +311,9 @@ class ViewBackendService { final payload = SetPublishNamespacePayloadPB()..newNamespace = name; return FolderEventSetPublishNamespace(payload).send(); } + + static Future> + getPublishNameSpace() async { + return FolderEventGetPublishNamespace().send(); + } }