share_button.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import 'package:appflowy/generated/locale_keys.g.dart';
  2. import 'package:appflowy/plugins/database_view/application/share_bloc.dart';
  3. import 'package:appflowy/startup/startup.dart';
  4. import 'package:appflowy/workspace/application/view/view_listener.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_popover/appflowy_popover.dart';
  8. import 'package:easy_localization/easy_localization.dart';
  9. import 'package:flowy_infra/file_picker/file_picker_service.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 DatabaseShareButton extends StatelessWidget {
  16. const DatabaseShareButton({
  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) => DatabaseShareBloc(view: view),
  25. child: BlocListener<DatabaseShareBloc, DatabaseShareState>(
  26. listener: (context, state) {
  27. state.mapOrNull(
  28. finish: (state) {
  29. state.successOrFail.fold(
  30. (data) => _handleExportData(context),
  31. _handleExportError,
  32. );
  33. },
  34. );
  35. },
  36. child: BlocBuilder<DatabaseShareBloc, DatabaseShareState>(
  37. builder: (context, state) => ConstrainedBox(
  38. constraints: const BoxConstraints.expand(
  39. height: 30,
  40. width: 100,
  41. ),
  42. child: DatabaseShareActionList(view: view),
  43. ),
  44. ),
  45. ),
  46. );
  47. }
  48. void _handleExportData(BuildContext context) {
  49. showSnackBarMessage(
  50. context,
  51. LocaleKeys.settings_files_exportFileSuccess.tr(),
  52. );
  53. }
  54. void _handleExportError(FlowyError error) {
  55. showMessageToast(error.msg);
  56. }
  57. }
  58. class DatabaseShareActionList extends StatefulWidget {
  59. const DatabaseShareActionList({
  60. super.key,
  61. required this.view,
  62. });
  63. final ViewPB view;
  64. @override
  65. State<DatabaseShareActionList> createState() =>
  66. DatabaseShareActionListState();
  67. }
  68. @visibleForTesting
  69. class DatabaseShareActionListState extends State<DatabaseShareActionList> {
  70. late String name;
  71. late final ViewListener viewListener = ViewListener(viewId: widget.view.id);
  72. @override
  73. void initState() {
  74. super.initState();
  75. listenOnViewUpdated();
  76. }
  77. @override
  78. void dispose() {
  79. viewListener.stop();
  80. super.dispose();
  81. }
  82. @override
  83. Widget build(BuildContext context) {
  84. final databaseShareBloc = context.read<DatabaseShareBloc>();
  85. return PopoverActionList<ShareActionWrapper>(
  86. direction: PopoverDirection.bottomWithCenterAligned,
  87. offset: const Offset(0, 8),
  88. actions: ShareAction.values
  89. .map((action) => ShareActionWrapper(action))
  90. .toList(),
  91. buildChild: (controller) {
  92. return RoundedTextButton(
  93. title: LocaleKeys.shareAction_buttonText.tr(),
  94. onPressed: () => controller.show(),
  95. );
  96. },
  97. onSelected: (action, controller) async {
  98. switch (action.inner) {
  99. case ShareAction.csv:
  100. final exportPath = await getIt<FilePickerService>().saveFile(
  101. dialogTitle: '',
  102. fileName: '${Uri.encodeComponent(name)}.csv',
  103. );
  104. if (exportPath != null) {
  105. databaseShareBloc.add(DatabaseShareEvent.shareCSV(exportPath));
  106. }
  107. break;
  108. }
  109. controller.close();
  110. },
  111. );
  112. }
  113. void listenOnViewUpdated() {
  114. name = widget.view.name;
  115. viewListener.start(
  116. onViewUpdated: (view) {
  117. name = view.name;
  118. },
  119. );
  120. }
  121. }
  122. enum ShareAction {
  123. csv,
  124. }
  125. class ShareActionWrapper extends ActionCell {
  126. final ShareAction inner;
  127. ShareActionWrapper(this.inner);
  128. Widget? icon(Color iconColor) => null;
  129. @override
  130. String get name {
  131. switch (inner) {
  132. case ShareAction.csv:
  133. return LocaleKeys.shareAction_csv.tr();
  134. }
  135. }
  136. }