feat: fetch the publish info to show the publish status

This commit is contained in:
Lucas.Xu 2024-06-21 21:26:52 +08:00 committed by Kilu
parent 9119e4a926
commit 02199e3a73
3 changed files with 41 additions and 9 deletions

View File

@ -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<DocumentShareEvent, DocumentShareState> {
DocumentShareBloc({
required this.view,
}) : super(DocumentShareState.initial()) {
on<DocumentShareEvent>((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<DocumentShareEvent, DocumentShareState> {
},
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<void, FlowyError>? 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<DocumentShareEvent, DocumentShareState> {
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,

View File

@ -30,7 +30,8 @@ class DocumentShareButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => getIt<DocumentShareBloc>(param1: view),
create: (context) => getIt<DocumentShareBloc>(param1: view)
..add(const DocumentShareEvent.initial()),
child: BlocListener<DocumentShareBloc, DocumentShareState>(
listener: (context, state) {
if (state.isLoading == false && state.exportResult != null) {

View File

@ -279,6 +279,13 @@ class ViewBackendService {
return FolderEventUpdateViewVisibilityStatus(payload).send();
}
static Future<FlowyResult<PublishInfoResponsePB, FlowyError>> getPublishInfo(
ViewPB view,
) async {
final payload = ViewIdPB()..value = view.id;
return FolderEventGetPublishInfo(payload).send();
}
static Future<FlowyResult<void, FlowyError>> 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<FlowyResult<PublishNamespacePB, FlowyError>>
getPublishNameSpace() async {
return FolderEventGetPublishNamespace().send();
}
}