diff --git a/lib/common/consts.dart b/lib/common/consts.dart index 89a00ad..bd97db6 100644 --- a/lib/common/consts.dart +++ b/lib/common/consts.dart @@ -12,5 +12,8 @@ const String noNameColorParam = 'noname'; /// The color swatch image file name for a given hex code. String colorSwatchFileName(String hexCode) => 'colorhap_${hexCode}_random_color_swatch.png'; +/// The name of the favorites CSV export file. +const String favoritesCSVFileName = 'colorhap_favorites.csv'; + /// The height of the color list item. const double colorListItemExtent = 128.0; diff --git a/lib/common/strings.dart b/lib/common/strings.dart index f3c9c90..c27b102 100644 --- a/lib/common/strings.dart +++ b/lib/common/strings.dart @@ -121,7 +121,6 @@ const String clearFavoritesDialogTitle = 'Clear favorites?'; const String clearFavoritesDialogMessage = 'This will remove all colors from your favorites list.'; const String clearFavoritesDialogPositiveAction = 'Clear'; const String exportFavoritesAsCsv = 'Export favorites as CSV'; -const String favoritesExported = 'Favorites exported to clipboard in CSV format'; // ----------------------------------------------------------------------------------------------- // Available Colors Screen diff --git a/lib/screens/available_colors_screen.dart b/lib/screens/available_colors_screen.dart index 6019790..18b47d7 100644 --- a/lib/screens/available_colors_screen.dart +++ b/lib/screens/available_colors_screen.dart @@ -6,6 +6,8 @@ import 'dart:math'; import 'package:flutter/material.dart'; +import '../../common/urls.dart' as urls; +import '../../utils/utils.dart' as utils; import '../common/consts.dart' as consts; import '../common/strings.dart' as strings; import '../models/color_type.dart'; @@ -19,8 +21,6 @@ import '../models/random_color_generators/random_web_color_generator.dart' as rw import '../models/random_color.dart'; import '../utils/color_utils.dart' as color_utils; import '../widgets/color_list_view.dart'; -import '../../utils/utils.dart' as utils; -import '../../common/urls.dart' as urls; /// A screen that displays all the available colors of a specific type in a list view. class AvailableColorsScreen extends StatefulWidget { diff --git a/lib/screens/color_favorites_screen.dart b/lib/screens/color_favorites_screen.dart index 41138f1..2eb8e8c 100644 --- a/lib/screens/color_favorites_screen.dart +++ b/lib/screens/color_favorites_screen.dart @@ -3,12 +3,15 @@ // license that can be found in the LICENSE file or at // https://www.tecdrop.com/colorhap/license/. +import 'dart:convert'; + import 'package:flutter/material.dart'; +import 'package:share_plus/share_plus.dart'; +import '../common/consts.dart' as consts; import '../common/preferences.dart' as preferences; import '../common/strings.dart' as strings; import '../models/random_color.dart'; -import '../utils/utils.dart' as utils; import '../widgets/color_list_view.dart'; import '../widgets/confirmation_dialog_box.dart'; @@ -27,26 +30,36 @@ class ColorFavoritesScreen extends StatefulWidget { } class _ColorFavoritesScreenState extends State { + /// Exports the favorite colors as a CSV file and uses the platform's share sheet to share it. + void _exportFavorites() { + final String favoritesCsv = preferences.colorFavoritesList.toCsvString(); + Share.shareXFiles( + [XFile.fromData(utf8.encode(favoritesCsv), mimeType: 'text/plain')], + fileNameOverrides: [consts.favoritesCSVFileName], + ); + } + + /// Clears all the favorite colors. + void _clearFavorites() async { + bool? showConfirmation = await showConfirmationDialogBox( + context, + title: strings.clearFavoritesDialogTitle, + content: strings.clearFavoritesDialogMessage, + positiveActionText: strings.clearFavoritesDialogPositiveAction, + ); + if (showConfirmation == true) { + setState(() => preferences.colorFavoritesList.clear()); + } + } + /// Performs the specified action on the app bar. Future _onAppBarAction(_AppBarActions action) async { switch (action) { - // Exports the favorite colors as a CSV file case _AppBarActions.exportFavoritesAsCsv: - ScaffoldMessengerState messengerState = ScaffoldMessenger.of(context); - await utils.copyToClipboard(context, preferences.colorFavoritesList.toCsvString()); - utils.showSnackBarForAsync(messengerState, strings.favoritesExported); + _exportFavorites(); break; - // Clears all the favorite colors case _AppBarActions.clearFavorites: - bool? showConfirmation = await showConfirmationDialogBox( - context, - title: strings.clearFavoritesDialogTitle, - content: strings.clearFavoritesDialogMessage, - positiveActionText: strings.clearFavoritesDialogPositiveAction, - ); - if (showConfirmation == true) { - setState(() => preferences.colorFavoritesList.clear()); - } + _clearFavorites(); break; } } diff --git a/lib/widgets/color_list_view.dart b/lib/widgets/color_list_view.dart index d8a0c81..17afb8c 100644 --- a/lib/widgets/color_list_view.dart +++ b/lib/widgets/color_list_view.dart @@ -4,7 +4,6 @@ // https://www.tecdrop.com/colorhap/license/. import 'dart:math'; - import 'package:flutter/material.dart'; import '../common/consts.dart' as consts;