Skip to content

Commit

Permalink
Refactor import statements for common files
Browse files Browse the repository at this point in the history
  • Loading branch information
TechAurelian committed Sep 28, 2024
1 parent c5dab34 commit dd353e8
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 38 deletions.
1 change: 1 addition & 0 deletions lib/common/app_const.dart → lib/common/consts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ 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 height of the color list item.
const double colorListItemExtent = 128.0;
File renamed without changes.
6 changes: 4 additions & 2 deletions lib/common/ui_strings.dart → lib/common/strings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// license that can be found in the LICENSE file or at
// https://www.tecdrop.com/colorhap/license/.

/// User interface strings for the ColorHap app.
library;

import '../models/color_type.dart';

// -----------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -36,8 +39,6 @@ const Map<ColorType, String> colorTypePlural = {
ColorType.trueColor: 'True Colors',
};

String availableColors(ColorType value) => 'Available ${colorTypePlural[value]}';

// -----------------------------------------------------------------------------------------------
// Drawer items
// -----------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -123,3 +124,4 @@ const String favoritesExported = 'Favorites exported to clipboard in CSV format'
// Available Colors Screen
// -----------------------------------------------------------------------------------------------

String availableColors(ColorType value) => 'Available ${colorTypePlural[value]}';
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

import 'package:flutter/material.dart';

import 'common/app_settings.dart' as settings;
import 'common/app_theme.dart';
import 'common/ui_strings.dart' as strings;
import 'common/preferences.dart' as preferences;
import 'common/strings.dart' as strings;
import 'common/theme.dart';
import 'screens/random_color_screen.dart';

Future<void> main() async {
Expand All @@ -20,7 +20,7 @@ Future<void> main() async {

// First try to load the app settings from Shared Preferences
await Future.any([
settings.loadSettings(),
preferences.loadSettings(),
Future.delayed(const Duration(seconds: 5)),
]);

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,8 +6,8 @@
import 'dart:math';
import 'package:flutter/material.dart';

import '../common/app_const.dart' as consts;
import '../common/ui_strings.dart' as strings;
import '../common/consts.dart' as consts;
import '../common/strings.dart' as strings;
import '../models/color_type.dart';
import '../models/more.dart';
import '../models/random_color_generator.dart';
Expand Down
22 changes: 11 additions & 11 deletions lib/screens/color_favorites_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

import 'package:flutter/material.dart';

import '../common/app_settings.dart' as settings;
import '../common/ui_strings.dart' as strings;
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';
Expand All @@ -33,7 +33,7 @@ class _ColorFavoritesScreenState extends State<ColorFavoritesScreen> {
// Exports the favorite colors as a CSV file
case _AppBarActions.exportFavoritesAsCsv:
ScaffoldMessengerState messengerState = ScaffoldMessenger.of(context);
await utils.copyToClipboard(context, settings.colorFavoritesList.toCsvString());
await utils.copyToClipboard(context, preferences.colorFavoritesList.toCsvString());
utils.showSnackBarForAsync(messengerState, strings.favoritesExported);
break;
// Clears all the favorite colors
Expand All @@ -45,7 +45,7 @@ class _ColorFavoritesScreenState extends State<ColorFavoritesScreen> {
positiveActionText: strings.clearFavoritesDialogPositiveAction,
);
if (showConfirmation == true) {
setState(() => settings.colorFavoritesList.clear());
setState(() => preferences.colorFavoritesList.clear());
}
break;
}
Expand All @@ -57,14 +57,14 @@ class _ColorFavoritesScreenState extends State<ColorFavoritesScreen> {
void _deleteFavoriteColor(int index) {
// First remove the color from the list
late final RandomColor randomColor;
setState(() => randomColor = settings.colorFavoritesList.removeAt(index));
setState(() => randomColor = preferences.colorFavoritesList.removeAt(index));

// Then display a snackbar with an undo action
final snackBar = SnackBar(
content: const Text(strings.removedFromFavorites),
action: SnackBarAction(
label: strings.undoRemoveFromFavorites,
onPressed: () => setState(() => settings.colorFavoritesList.insert(index, randomColor)),
onPressed: () => setState(() => preferences.colorFavoritesList.insert(index, randomColor)),
),
);
ScaffoldMessenger.of(context)
Expand All @@ -82,18 +82,18 @@ class _ColorFavoritesScreenState extends State<ColorFavoritesScreen> {
child: Scaffold(
appBar: _AppBar(
title: const Text(strings.favoriteColorsScreenTitle),
haveFavorites: settings.colorFavoritesList.length > 0,
haveFavorites: preferences.colorFavoritesList.length > 0,
onAction: _onAppBarAction,
),
body: settings.colorFavoritesList.length > 0
body: preferences.colorFavoritesList.length > 0
? _buildFavoritesListView()
: _buildNoFavoritesMessage(),
),
);
}

ColorListItemData _getItemData(int index) {
final RandomColor randomColor = settings.colorFavoritesList.elementAt(index);
final RandomColor randomColor = preferences.colorFavoritesList.elementAt(index);
return (
color: randomColor.color,
title: randomColor.longTitle,
Expand All @@ -105,11 +105,11 @@ class _ColorFavoritesScreenState extends State<ColorFavoritesScreen> {
Widget _buildFavoritesListView() {
return ColorListView(
key: const PageStorageKey<String>('favoritesListView'),
itemCount: () => settings.colorFavoritesList.length,
itemCount: () => preferences.colorFavoritesList.length,
itemData: _getItemData,
itemButton: (_) => (icon: Icons.delete, tooltip: strings.removeFavTooltip),
onItemTap: (int index) => Navigator.of(context).pop<RandomColor>(
settings.colorFavoritesList.elementAt(index),
preferences.colorFavoritesList.elementAt(index),
),
onItemButtonPressed: _deleteFavoriteColor,
);
Expand Down
6 changes: 3 additions & 3 deletions lib/screens/color_info_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import 'package:flutter/services.dart';

import 'package:share_plus/share_plus.dart';

import '../common/app_const.dart' as consts;
import '../common/app_urls.dart' as urls;
import '../common/ui_strings.dart' as strings;
import '../common/consts.dart' as consts;
import '../common/strings.dart' as strings;
import '../common/urls.dart' as urls;
import '../models/random_color.dart';
import '../utils/color_utils.dart' as color_utils;
import '../utils/utils.dart' as utils;
Expand Down
15 changes: 8 additions & 7 deletions lib/screens/random_color_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

import 'package:flutter/material.dart';

import '../common/app_settings.dart' as settings;
import '../common/ui_strings.dart' as strings;
import '../common/preferences.dart' as preferences;
import '../common/strings.dart' as strings;
import '../models/color_type.dart';
import '../models/random_color_generator.dart';
import '../models/random_color.dart';
Expand Down Expand Up @@ -50,7 +50,7 @@ class _RandomColorScreenState extends State<RandomColorScreen> {
super.initState();

// Restore the last selected color type
_colorType = settings.colorType;
_colorType = preferences.colorType;

// Generate the first random color
_shuffleColor();
Expand All @@ -59,7 +59,7 @@ class _RandomColorScreenState extends State<RandomColorScreen> {
void _updateState(RandomColor? randomColor) {
setState(() {
if (randomColor != null) _randomColor = randomColor;
_colorFavIndex = settings.colorFavoritesList.indexOf(_randomColor);
_colorFavIndex = preferences.colorFavoritesList.indexOf(_randomColor);
});
}

Expand Down Expand Up @@ -95,8 +95,9 @@ class _RandomColorScreenState extends State<RandomColorScreen> {
// Toggle the current color in the favorites list
case _AppBarActions.toggleFav:
setState(() {
_colorFavIndex = settings.colorFavoritesList.toggle(_randomColor, index: _colorFavIndex);
settings.saveColorFavoritesList();
_colorFavIndex =
preferences.colorFavoritesList.toggle(_randomColor, index: _colorFavIndex);
preferences.saveColorFavoritesList();
});
break;

Expand Down Expand Up @@ -143,7 +144,7 @@ class _RandomColorScreenState extends State<RandomColorScreen> {
randomColor: _randomColor,
colorType: _colorType,
onColorTypeChange: (ColorType colorType) {
settings.colorType = _colorType = colorType;
preferences.colorType = _colorType = colorType;
_shuffleColor();
},
onColorFavoritesTap: _gotoColorFavoritesScreen,
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import 'package:flutter/services.dart';
import 'package:intl/intl.dart';
import 'package:url_launcher/url_launcher.dart';

import '../common/ui_strings.dart' as strings;
import '../common/strings.dart' as strings;

final NumberFormat _numberFormat = NumberFormat.decimalPattern();

Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/color_info_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import 'dart:math';
import 'package:flutter/material.dart';

import '../common/ui_strings.dart' as strings;
import '../common/strings.dart' as strings;
import '../models/random_color.dart';
import '../utils/color_utils.dart' as color_utils;

Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/color_list_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'dart:math';

import 'package:flutter/material.dart';

import '../common/app_const.dart' as consts;
import '../common/consts.dart' as consts;
import '../utils/color_utils.dart' as color_utils;

typedef ColorListItemData = ({
Expand Down
13 changes: 7 additions & 6 deletions lib/widgets/internal/app_drawer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

import 'package:flutter/material.dart';

import '../../common/app_settings.dart' as settings;
import '../../common/app_urls.dart' as urls;
import '../../common/custom_icons.dart' as custom_icons;
import '../../common/ui_strings.dart' as strings;
import '../../common/preferences.dart' as preferences;
import '../../common/strings.dart' as strings;
import '../../common/urls.dart' as urls;
import '../../models/color_type.dart';
import '../../models/random_color_generator.dart';
import '../../models/random_color.dart';
Expand Down Expand Up @@ -238,11 +238,12 @@ class AppDrawer extends StatelessWidget {
// Color Favorites drawer item
_buildItem(
context,
icon: settings.colorFavoritesList.length > 0 ? Icons.favorite : Icons.favorite_border,
icon:
preferences.colorFavoritesList.length > 0 ? Icons.favorite : Icons.favorite_border,
title: strings.colorFavoritesDrawer,
subtitle: strings.colorFavoritesSubtitle(
utils.intToCommaSeparatedString(settings.colorFavoritesList.length),
isPlural: settings.colorFavoritesList.length != 1,
utils.intToCommaSeparatedString(preferences.colorFavoritesList.length),
isPlural: preferences.colorFavoritesList.length != 1,
),
item: AppDrawerItems.colorFavorites,
),
Expand Down

0 comments on commit dd353e8

Please sign in to comment.