Skip to content

Commit

Permalink
Use OS share to export the favorite colors as CSV
Browse files Browse the repository at this point in the history
  • Loading branch information
TechAurelian committed Sep 30, 2024
1 parent bb52cca commit 76b2fae
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
3 changes: 3 additions & 0 deletions lib/common/consts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
1 change: 0 additions & 1 deletion lib/common/strings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/screens/available_colors_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 {
Expand Down
43 changes: 28 additions & 15 deletions lib/screens/color_favorites_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -27,26 +30,36 @@ class ColorFavoritesScreen extends StatefulWidget {
}

class _ColorFavoritesScreenState extends State<ColorFavoritesScreen> {
/// 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<void> _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;
}
}
Expand Down
1 change: 0 additions & 1 deletion lib/widgets/color_list_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// https://www.tecdrop.com/colorhap/license/.

import 'dart:math';

import 'package:flutter/material.dart';

import '../common/consts.dart' as consts;
Expand Down

0 comments on commit 76b2fae

Please sign in to comment.