mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
feat: fetch the publish info to show the publish status
This commit is contained in:
parent
9119e4a926
commit
02199e3a73
@ -13,12 +13,27 @@ import 'package:freezed_annotation/freezed_annotation.dart';
|
|||||||
|
|
||||||
part 'document_share_bloc.freezed.dart';
|
part 'document_share_bloc.freezed.dart';
|
||||||
|
|
||||||
|
const _url = 'https://test.appflowy.io';
|
||||||
|
|
||||||
class DocumentShareBloc extends Bloc<DocumentShareEvent, DocumentShareState> {
|
class DocumentShareBloc extends Bloc<DocumentShareEvent, DocumentShareState> {
|
||||||
DocumentShareBloc({
|
DocumentShareBloc({
|
||||||
required this.view,
|
required this.view,
|
||||||
}) : super(DocumentShareState.initial()) {
|
}) : super(DocumentShareState.initial()) {
|
||||||
on<DocumentShareEvent>((event, emit) async {
|
on<DocumentShareEvent>((event, emit) async {
|
||||||
await event.when(
|
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 {
|
share: (type, path) async {
|
||||||
if (DocumentShareType.unimplemented.contains(type)) {
|
if (DocumentShareType.unimplemented.contains(type)) {
|
||||||
Log.error('DocumentShareType $type is not implemented');
|
Log.error('DocumentShareType $type is not implemented');
|
||||||
@ -38,22 +53,25 @@ class DocumentShareBloc extends Bloc<DocumentShareEvent, DocumentShareState> {
|
|||||||
},
|
},
|
||||||
publish: (url) async {
|
publish: (url) async {
|
||||||
// todo: optimize the logic
|
// todo: optimize the logic
|
||||||
const spaceName = 'appflowy';
|
const prefix = 'appflowy';
|
||||||
final name = '${view.name}-${uuid()}';
|
final name = '${view.name}-${uuid()}'.substring(0, 19);
|
||||||
|
|
||||||
// set space name
|
// set space name
|
||||||
|
|
||||||
FlowyResult<void, FlowyError>? result;
|
|
||||||
try {
|
try {
|
||||||
await ViewBackendService.setPublishNameSpace(spaceName)
|
final nameSpace =
|
||||||
.getOrThrow();
|
await ViewBackendService.getPublishNameSpace().getOrThrow();
|
||||||
result = await ViewBackendService.publish(view, name: name);
|
if (nameSpace.namespace != prefix) {
|
||||||
|
await ViewBackendService.setPublishNameSpace(prefix).getOrThrow();
|
||||||
|
}
|
||||||
|
|
||||||
|
final result = await ViewBackendService.publish(view, name: name);
|
||||||
|
|
||||||
emit(
|
emit(
|
||||||
state.copyWith(
|
state.copyWith(
|
||||||
isPublished: true,
|
isPublished: true,
|
||||||
publishResult: result,
|
publishResult: result,
|
||||||
url: 'https://test.appflowy.io/$spaceName/$name',
|
url: '$_url/${nameSpace.namespace}/$name',
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@ -75,6 +93,7 @@ class DocumentShareBloc extends Bloc<DocumentShareEvent, DocumentShareState> {
|
|||||||
emit(
|
emit(
|
||||||
state.copyWith(
|
state.copyWith(
|
||||||
isPublished: false,
|
isPublished: false,
|
||||||
|
publishResult: null,
|
||||||
url: '',
|
url: '',
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@ -149,6 +168,7 @@ enum DocumentShareType {
|
|||||||
|
|
||||||
@freezed
|
@freezed
|
||||||
class DocumentShareEvent with _$DocumentShareEvent {
|
class DocumentShareEvent with _$DocumentShareEvent {
|
||||||
|
const factory DocumentShareEvent.initial() = _Initial;
|
||||||
const factory DocumentShareEvent.share(
|
const factory DocumentShareEvent.share(
|
||||||
DocumentShareType type,
|
DocumentShareType type,
|
||||||
String? path,
|
String? path,
|
||||||
|
@ -30,7 +30,8 @@ class DocumentShareButton extends StatelessWidget {
|
|||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return BlocProvider(
|
return BlocProvider(
|
||||||
create: (context) => getIt<DocumentShareBloc>(param1: view),
|
create: (context) => getIt<DocumentShareBloc>(param1: view)
|
||||||
|
..add(const DocumentShareEvent.initial()),
|
||||||
child: BlocListener<DocumentShareBloc, DocumentShareState>(
|
child: BlocListener<DocumentShareBloc, DocumentShareState>(
|
||||||
listener: (context, state) {
|
listener: (context, state) {
|
||||||
if (state.isLoading == false && state.exportResult != null) {
|
if (state.isLoading == false && state.exportResult != null) {
|
||||||
|
@ -279,6 +279,13 @@ class ViewBackendService {
|
|||||||
return FolderEventUpdateViewVisibilityStatus(payload).send();
|
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(
|
static Future<FlowyResult<void, FlowyError>> publish(
|
||||||
ViewPB view, {
|
ViewPB view, {
|
||||||
String? name,
|
String? name,
|
||||||
@ -288,7 +295,6 @@ class ViewBackendService {
|
|||||||
if (name != null) {
|
if (name != null) {
|
||||||
payload.publishName = name;
|
payload.publishName = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
return FolderEventPublishView(payload).send();
|
return FolderEventPublishView(payload).send();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -305,4 +311,9 @@ class ViewBackendService {
|
|||||||
final payload = SetPublishNamespacePayloadPB()..newNamespace = name;
|
final payload = SetPublishNamespacePayloadPB()..newNamespace = name;
|
||||||
return FolderEventSetPublishNamespace(payload).send();
|
return FolderEventSetPublishNamespace(payload).send();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Future<FlowyResult<PublishNamespacePB, FlowyError>>
|
||||||
|
getPublishNameSpace() async {
|
||||||
|
return FolderEventGetPublishNamespace().send();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user