share_button.dart 4.2 KB

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