From 5dec6ea545787f9757abbda646f08384bac67be8 Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Fri, 21 Jun 2024 16:25:06 +0800 Subject: [PATCH] feat: call the publish event --- .../application/document_share_bloc.dart | 62 +++++++++++------- .../presentation/share/publish_tab.dart | 28 ++++---- .../presentation/share/share_button.dart | 42 +++++++----- .../presentation/share/share_menu.dart | 65 ++++++++++--------- 4 files changed, 112 insertions(+), 85 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 7898888ff6..1dff1fbe34 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 @@ -23,30 +23,9 @@ class DocumentShareBloc extends Bloc { return; } - emit( - state.copyWith(isLoading: true), - ); + emit(state.copyWith(isLoading: true)); - final exporter = DocumentExporter(view); - final FlowyResult result = - await exporter.export(type.exportType).then((value) { - return value.fold( - (s) { - if (path != null) { - switch (type) { - case DocumentShareType.markdown: - return FlowyResult.success(_saveMarkdownToPath(s, path)); - case DocumentShareType.html: - return FlowyResult.success(_saveHTMLToPath(s, path)); - default: - break; - } - } - return FlowyResult.failure(FlowyError()); - }, - (f) => FlowyResult.failure(f), - ); - }); + final result = await _export(type, path); emit( state.copyWith( @@ -55,11 +34,41 @@ class DocumentShareBloc extends Bloc { ), ); }, + publish: (url) async { + emit(state.copyWith(isPublished: true)); + }, + unPublish: () async { + emit(state.copyWith(isPublished: false)); + }, ); }); } final ViewPB view; + late final exporter = DocumentExporter(view); + + Future> _export( + DocumentShareType type, + String? path, + ) async { + final result = await exporter.export(type.exportType); + return result.fold( + (s) { + if (path != null) { + switch (type) { + case DocumentShareType.markdown: + return FlowySuccess(_saveMarkdownToPath(s, path)); + case DocumentShareType.html: + return FlowySuccess(_saveHTMLToPath(s, path)); + default: + break; + } + } + return FlowyResult.failure(FlowyError()); + }, + (f) => FlowyResult.failure(f), + ); + } ExportDataPB _saveMarkdownToPath(String markdown, String path) { File(path).writeAsStringSync(markdown); @@ -103,7 +112,9 @@ class DocumentShareEvent with _$DocumentShareEvent { const factory DocumentShareEvent.share( DocumentShareType type, String? path, - ) = Share; + ) = _Share; + const factory DocumentShareEvent.publish(String url) = _Publish; + const factory DocumentShareEvent.unPublish() = _UnPublish; } @freezed @@ -111,9 +122,12 @@ class DocumentShareState with _$DocumentShareState { const factory DocumentShareState({ required bool isLoading, FlowyResult? exportResult, + required bool isPublished, + FlowyResult? publishResult, }) = _DocumentShareState; factory DocumentShareState.initial() => const DocumentShareState( isLoading: false, + isPublished: false, ); } 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 50e4ac7f9a..c1fefbc3fe 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,25 +1,31 @@ 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:easy_localization/easy_localization.dart'; import 'package:flowy_infra_ui/flowy_infra_ui.dart'; import 'package:flowy_infra_ui/widget/rounded_button.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; -class PublishTab extends StatefulWidget { +class PublishTab extends StatelessWidget { const PublishTab({super.key}); - @override - State createState() => _PublishTabState(); -} - -class _PublishTabState extends State { - bool isPublished = false; - @override Widget build(BuildContext context) { - return isPublished - ? _PublishedWidget(onUnPublish: () {}, onVisitSite: () {}) - : _UnPublishWidget(onPublish: () => setState(() => isPublished = true)); + return context.watch().state.isPublished + ? _PublishedWidget( + onUnPublish: () { + context + .read() + .add(const DocumentShareEvent.unPublish()); + }, + onVisitSite: () {}, + ) + : _UnPublishWidget( + onPublish: () => context + .read() + .add(const DocumentShareEvent.publish('')), + ); } } 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 6ad931c644..333d5908cb 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 @@ -41,26 +41,32 @@ class DocumentShareButton extends StatelessWidget { } }, child: BlocBuilder( - builder: (context, state) => SizedBox( - height: 32.0, - child: IntrinsicWidth( - child: AppFlowyPopover( - direction: PopoverDirection.bottomWithRightAligned, - constraints: const BoxConstraints( - maxWidth: 422, - ), - margin: const EdgeInsets.all(16), - offset: const Offset(0, 8), - popupBuilder: (context) => const ShareMenu(), - child: RoundedTextButton( - title: LocaleKeys.shareAction_buttonText.tr(), - padding: const EdgeInsets.symmetric(horizontal: 12.0), - fontSize: 14.0, - textColor: Theme.of(context).colorScheme.onPrimary, + builder: (context, state) { + final shareBloc = context.read(); + return SizedBox( + height: 32.0, + child: IntrinsicWidth( + child: AppFlowyPopover( + direction: PopoverDirection.bottomWithRightAligned, + constraints: const BoxConstraints( + maxWidth: 422, + ), + margin: const EdgeInsets.all(16), + offset: const Offset(0, 8), + popupBuilder: (context) => BlocProvider.value( + value: shareBloc, + child: const ShareMenu(), + ), + child: RoundedTextButton( + title: LocaleKeys.shareAction_buttonText.tr(), + padding: const EdgeInsets.symmetric(horizontal: 12.0), + fontSize: 14.0, + textColor: Theme.of(context).colorScheme.onPrimary, + ), ), ), - ), - ), + ); + }, ), ), ); diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/share/share_menu.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/share/share_menu.dart index de60dba041..d1b353c7ed 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/share/share_menu.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/share/share_menu.dart @@ -51,32 +51,36 @@ class _ShareMenuState extends State { return Column( mainAxisSize: MainAxisSize.min, children: [ - CustomSlidingSegmentedControl( - initialValue: selectedTab, - curve: Curves.linear, - padding: 0, - innerPadding: const EdgeInsets.all(3.0), - children: children, - decoration: BoxDecoration( - color: const Color(0xFFEEF0F3), - borderRadius: BorderRadius.circular(8), + SizedBox( + height: 34, + child: CustomSlidingSegmentedControl( + initialValue: selectedTab, + curve: Curves.linear, + padding: 0, + fixedWidth: 128, + innerPadding: const EdgeInsets.all(3.0), + children: children, + decoration: BoxDecoration( + color: const Color(0xFFEEF0F3), + borderRadius: BorderRadius.circular(8), + ), + thumbDecoration: BoxDecoration( + color: Colors.white, + boxShadow: const [ + BoxShadow( + color: Color(0x141F2225), + blurRadius: 8, + offset: Offset(0, 2), + ), + ], + borderRadius: BorderRadius.circular(8), + ), + onValueChanged: (v) { + setState(() { + selectedTab = v; + }); + }, ), - thumbDecoration: BoxDecoration( - color: Colors.white, - boxShadow: const [ - BoxShadow( - color: Color(0x141F2225), - blurRadius: 8, - offset: Offset(0, 2), - ), - ], - borderRadius: BorderRadius.circular(8), - ), - onValueChanged: (v) { - setState(() { - selectedTab = v; - }); - }, ), _buildTab(context), ], @@ -108,13 +112,10 @@ class _Segment extends StatelessWidget { @override Widget build(BuildContext context) { final textColor = isSelected ? null : Theme.of(context).hintColor; - return SizedBox( - width: 128, - child: FlowyText( - title, - textAlign: TextAlign.center, - color: textColor, - ), + return FlowyText( + title, + textAlign: TextAlign.center, + color: textColor, ); } }