feat: integrate publish api

This commit is contained in:
Lucas.Xu 2024-06-21 21:02:05 +08:00
parent 9c3f083684
commit 4a8536d403
3 changed files with 62 additions and 3 deletions

View File

@ -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<DocumentShareEvent, DocumentShareState> {
);
},
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<FlowyResult<ExportDataPB, FlowyError>> _export(
@ -124,10 +151,12 @@ class DocumentShareState with _$DocumentShareState {
FlowyResult<ExportDataPB, FlowyError>? exportResult,
required bool isPublished,
FlowyResult<void, FlowyError>? publishResult,
required String url,
}) = _DocumentShareState;
factory DocumentShareState.initial() => const DocumentShareState(
isLoading: false,
isPublished: false,
url: '',
);
}

View File

@ -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);
},
),
],
),

View File

@ -271,4 +271,31 @@ class ViewBackendService {
);
return FolderEventUpdateViewVisibilityStatus(payload).send();
}
static Future<FlowyResult<void, FlowyError>> publish(
ViewPB view, {
String? name,
}) async {
final payload = PublishViewParamsPB()..viewId = view.id;
if (name != null) {
payload.publishName = name;
}
return FolderEventPublishView(payload).send();
}
static Future<FlowyResult<void, FlowyError>> unpublish(
ViewPB view,
) async {
final payload = UnpublishViewsPayloadPB(viewIds: [view.id]);
return FolderEventUnpublishViews(payload).send();
}
static Future<FlowyResult<void, FlowyError>> setPublishNameSpace(
String name,
) async {
final payload = SetPublishNamespacePayloadPB()..newNamespace = name;
return FolderEventSetPublishNamespace(payload).send();
}
}