share_button.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import 'package:appflowy/generated/locale_keys.g.dart';
  2. import 'package:appflowy/startup/startup.dart';
  3. import 'package:appflowy/plugins/document/application/share_bloc.dart';
  4. import 'package:appflowy/util/file_picker/file_picker_service.dart';
  5. import 'package:appflowy/workspace/presentation/home/toast.dart';
  6. import 'package:appflowy/workspace/presentation/widgets/pop_up_action.dart';
  7. import 'package:appflowy_backend/protobuf/flowy-document2/entities.pb.dart';
  8. import 'package:appflowy_popover/appflowy_popover.dart';
  9. import 'package:easy_localization/easy_localization.dart';
  10. import 'package:flowy_infra_ui/widget/rounded_button.dart';
  11. import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
  12. import 'package:appflowy_backend/protobuf/flowy-folder2/view.pb.dart';
  13. import 'package:flutter/material.dart';
  14. import 'package:flutter_bloc/flutter_bloc.dart';
  15. class DocumentShareButton extends StatelessWidget {
  16. const DocumentShareButton({
  17. super.key,
  18. required this.view,
  19. });
  20. final ViewPB view;
  21. @override
  22. Widget build(BuildContext context) {
  23. return BlocProvider(
  24. create: (context) => getIt<DocShareBloc>(param1: view),
  25. child: BlocListener<DocShareBloc, DocShareState>(
  26. listener: (context, state) {
  27. state.mapOrNull(
  28. finish: (state) {
  29. state.successOrFail.fold(
  30. (data) => _handleExportData(context, data),
  31. _handleExportError,
  32. );
  33. },
  34. );
  35. },
  36. child: BlocBuilder<DocShareBloc, DocShareState>(
  37. builder: (context, state) => ConstrainedBox(
  38. constraints: const BoxConstraints.expand(
  39. height: 30,
  40. width: 100,
  41. ),
  42. child: ShareActionList(view: view),
  43. ),
  44. ),
  45. ),
  46. );
  47. }
  48. void _handleExportData(BuildContext context, ExportDataPB exportData) {
  49. switch (exportData.exportType) {
  50. case ExportType.Markdown:
  51. showSnackBarMessage(
  52. context,
  53. LocaleKeys.settings_files_exportFileSuccess.tr(),
  54. );
  55. break;
  56. case ExportType.Link:
  57. case ExportType.Text:
  58. break;
  59. }
  60. }
  61. void _handleExportError(FlowyError error) {
  62. showMessageToast(error.msg);
  63. }
  64. }
  65. class ShareActionList extends StatelessWidget {
  66. const ShareActionList({
  67. super.key,
  68. required this.view,
  69. });
  70. final ViewPB view;
  71. @override
  72. Widget build(BuildContext context) {
  73. final docShareBloc = context.read<DocShareBloc>();
  74. return PopoverActionList<ShareActionWrapper>(
  75. direction: PopoverDirection.bottomWithCenterAligned,
  76. offset: const Offset(0, 8),
  77. actions: ShareAction.values
  78. .map((action) => ShareActionWrapper(action))
  79. .toList(),
  80. buildChild: (controller) {
  81. return RoundedTextButton(
  82. title: LocaleKeys.shareAction_buttonText.tr(),
  83. onPressed: () => controller.show(),
  84. );
  85. },
  86. onSelected: (action, controller) async {
  87. switch (action.inner) {
  88. case ShareAction.markdown:
  89. final exportPath = await getIt<FilePickerService>().saveFile(
  90. dialogTitle: '',
  91. fileName: '${view.name}.md',
  92. );
  93. if (exportPath != null) {
  94. docShareBloc.add(DocShareEvent.shareMarkdown(exportPath));
  95. }
  96. break;
  97. }
  98. controller.close();
  99. },
  100. );
  101. }
  102. }
  103. enum ShareAction {
  104. markdown,
  105. }
  106. class ShareActionWrapper extends ActionCell {
  107. final ShareAction inner;
  108. ShareActionWrapper(this.inner);
  109. Widget? icon(Color iconColor) => null;
  110. @override
  111. String get name {
  112. switch (inner) {
  113. case ShareAction.markdown:
  114. return LocaleKeys.shareAction_markdown.tr();
  115. }
  116. }
  117. }