Skip to content

Commit

Permalink
Merge pull request #1 from tecdrop/v2
Browse files Browse the repository at this point in the history
  • Loading branch information
TechAurelian authored Jul 26, 2023
2 parents 412804e + b039b5f commit 39ced44
Show file tree
Hide file tree
Showing 29 changed files with 1,298 additions and 617 deletions.
7 changes: 5 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"dart.lineLength": 100,
"cSpell.words": [
"Tecdrop",
"colorhap",
"fullscreen"
"consts",
"fullscreen",
"noname",
"Tecdrop",
"writeln"
]
}
15 changes: 7 additions & 8 deletions lib/common/app_const.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
// Use of this source code is governed by an MIT-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';
/// Application-wide constants.
library;

/// Application-wide constants (e.g. app routes).
class AppConst {
AppConst._();
import 'package:flutter/material.dart';

static const String randomColorRoute = '/';
static const String colorInfoRoute = '/color-info';
/// The default "random" color when the app starts and no real random color has been generated yet.
const Color defaultColor = Colors.black;

static const Color defaultColor = Colors.black;
}
/// The parameter name for colors with no name.
const String noNameColorParam = 'noname';
139 changes: 139 additions & 0 deletions lib/common/app_routes.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// Copyright 2020-2023 Tecdrop (www.tecdrop.com)
// Use of this source code is governed by an MIT-style license that can be
// found in the LICENSE file.

import 'dart:async';
import 'package:flutter/material.dart';

import 'package:go_router/go_router.dart';

import '../common/app_const.dart' as consts;
import '../common/app_settings.dart' as settings;
import '../common/ui_strings.dart' as strings;
import '../models/color_type.dart';
import '../models/random_color.dart';
import '../screens/color_favorites_screen.dart';
import '../screens/color_info_screen.dart';
import '../screens/invalid_screen.dart';
import '../screens/random_color_screen.dart';
import '../utils/color_utils.dart' as color_utils;

/// The route configuration for the app.
final GoRouter appRouter = GoRouter(
// The error handler redirects to the Invalid screen
onException: (_, GoRouterState state, GoRouter router) {
router.go('/404', extra: state.uri.toString());
},

// The routes configuration
routes: <RouteBase>[
// The home route of the app is the Random Color screen
GoRoute(
path: '/',
builder: _randomColorRouteBuilder,
routes: [
// The child route for the Color Info screen
GoRoute(
path: 'color/:colorType/:colorHex/:colorName',
builder: _colorInfoRouteBuilder,
),
// The child route for the Favorite Colors screen
GoRoute(
path: 'fav',
builder: _colorFavoritesRouteBuilder,
routes: [
// The child route for the Color Info screen (from the Favorite Colors screen)
GoRoute(
path: 'color/:colorType/:colorHex/:colorName',
builder: _colorInfoRouteBuilder,
),
],
),
],
),

// The route for the Invalid screen, when the route is invalid
GoRoute(
path: '/404',
builder: (BuildContext context, GoRouterState state) {
return InvalidScreen(message: strings.invalidPage(state.extra as String?));
},
),

// The route for the Random Color screen, when the color type is specified in the route
GoRoute(
path: '/:colorType',
redirect: _randomColorRouteRedirect,
),
],
);

// -----------------------------------------------------------------------------------------------
// Random Color Route (Home)
// -----------------------------------------------------------------------------------------------

/// The route builder for the Random Color screen, which is the home screen of the app.
Widget _randomColorRouteBuilder(BuildContext context, GoRouterState state) {
return RandomColorScreen(
colorType: settings.colorType,
);
}

/// The route redirect for the Random Color screen, when the color type is specified in the route.
FutureOr<String?> _randomColorRouteRedirect(BuildContext context, GoRouterState state) {
settings.colorType = ColorType.fromShortString(state.pathParameters['colorType']);
return '/';
}

/// Navigates to the Random Color screen to generate random colors of the specified type.
void gotoRandomColorRoute(BuildContext context, ColorType colorType) {
settings.colorType = colorType;
context.go('/${colorType.toShortString()}');
}

// -----------------------------------------------------------------------------------------------
// Color Info Route
// -----------------------------------------------------------------------------------------------

/// The route builder for the Color Info screen.
Widget _colorInfoRouteBuilder(BuildContext context, GoRouterState state) {
ColorType colorType = ColorType.fromShortString(state.pathParameters['colorType']);
String? colorCode = state.pathParameters['colorHex'];
Color? color = color_utils.rgbHexToColor(colorCode);
String? colorName = state.pathParameters['colorName'];

// If the color code is invalid, return the Invalid Color screen
if (color == null) {
return InvalidScreen(message: strings.invalidColor(colorCode));
}

return ColorInfoScreen(
randomColor: RandomColor(
color: color,
type: colorType,
name: colorName != consts.noNameColorParam ? colorName : null,
),
);
}

/// Navigates to the Color Info screen to show information about the specified color.
void gotoColorInfoRoute(BuildContext context, RandomColor randomColor, {bool fromFav = false}) {
final String colorType = randomColor.type.toShortString();
final String colorCode = color_utils.toHexString(randomColor.color, withHash: false);
final String colorName = randomColor.name ?? consts.noNameColorParam;
context.go('${fromFav ? '/fav' : ''}/color/$colorType/$colorCode/$colorName');
}

// -----------------------------------------------------------------------------------------------
// Color Favorites Route
// -----------------------------------------------------------------------------------------------

/// The route builder for the Color Favorites screen.
Widget _colorFavoritesRouteBuilder(BuildContext context, GoRouterState state) {
return const ColorFavoritesScreen();
}

/// Navigates to the Color Favorites screen to show the list of favorite colors.
Future<void> gotoColorFavoritesRoute(BuildContext context) async {
return await context.push<void>('/fav');
}
45 changes: 32 additions & 13 deletions lib/common/app_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,17 @@

import 'package:shared_preferences/shared_preferences.dart';

import '../models/random_color.dart';

// -----------------------------------------------------------------------------------------------
// immersiveMode setting (not persistent)
// -----------------------------------------------------------------------------------------------

bool _immersiveMode = false;

/// Whether the main screens are in immersive mode.
bool get immersiveMode => _immersiveMode;
set immersiveMode(bool value) => _immersiveMode = value;
import '../models/color_favorites_list.dart';
import '../models/color_type.dart';

// -----------------------------------------------------------------------------------------------
// colorType setting
// -----------------------------------------------------------------------------------------------

const String _colorTypeKey = 'colorType';

/// The last type of random colors generated by the user.
ColorType _colorType = ColorType.webColor;

/// The last kind of random colors generated by the user.
ColorType get colorType => _colorType;
set colorType(ColorType value) {
_colorType = value;
Expand All @@ -37,12 +27,41 @@ Future<void> _saveColorType() async {
await preferences.setInt(_colorTypeKey, _colorType.index);
}

// -----------------------------------------------------------------------------------------------
// favList setting
// -----------------------------------------------------------------------------------------------

const String _colorFavoritesListKey = 'colorFavoritesList';

/// The list of favorite colors.
ColorFavoritesList colorFavoritesList = ColorFavoritesList();

/// Saves the favorite colors list to persistent storage.
///
/// This should be called whenever the list of favorite colors changes (add, remove, clear, etc.)
Future<void> saveColorFavoritesList() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
await preferences.setStringList(_colorFavoritesListKey, colorFavoritesList.toJsonStringList());
}

// -----------------------------------------------------------------------------------------------
// showColorInformation setting (not persistent)
// -----------------------------------------------------------------------------------------------

/// Whether to show the color information list on the Color Info screen.
bool showColorInformation = true;

// -----------------------------------------------------------------------------------------------
// Common
// -----------------------------------------------------------------------------------------------

/// Loads app settings from persistent storage.
Future<void> loadSettings() async {
SharedPreferences preferences = await SharedPreferences.getInstance();

// Load the last color type used by the user
_colorType = ColorType.values[preferences.getInt(_colorTypeKey) ?? 0];

// Load the list of favorite colors
colorFavoritesList.loadFromJsonStringList(preferences.getStringList(_colorFavoritesListKey));
}
45 changes: 45 additions & 0 deletions lib/common/app_theme.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2020-2023 Tecdrop (www.tecdrop.com)
// Use of this source code is governed by an MIT-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';

/// Returns the light or dark theme for the app.
ThemeData getAppTheme(Brightness brightness) {
final bool isDark = brightness == Brightness.dark;

return ThemeData(
// The color scheme used by the app
primaryColor: isDark ? Colors.white : Colors.black,
colorScheme: isDark
? ColorScheme.dark(
primary: Colors.black,
onPrimary: Colors.white,
secondary: const ColorScheme.dark().surface,
onSecondary: const ColorScheme.dark().onSurface,
)
: ColorScheme.light(
primary: Colors.white,
onPrimary: Colors.black,
secondary: const ColorScheme.light().surface,
onSecondary: const ColorScheme.light().onSurface,
),
textButtonTheme: TextButtonThemeData(
// The text color of text buttons
style: TextButton.styleFrom(
foregroundColor: isDark ? Colors.white : Colors.black,
),
),

// Always use the dark background snack bar theme
snackBarTheme: SnackBarThemeData(
// Based on https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/material/snack_bar.dart#L859
backgroundColor: Color.alphaBlend(
const ColorScheme.light().onSurface.withOpacity(0.80),
const ColorScheme.light().surface,
),
contentTextStyle: const TextStyle(color: Colors.white),
actionTextColor: Colors.white,
),
);
}
32 changes: 19 additions & 13 deletions lib/common/app_urls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@
// found in the LICENSE file.

/// URLs used in the app.
class AppUrls {
AppUrls._();
library;

static const String _appId = 'colorhap';
static const String _editionId = 'aa';
/// The app ID and edition ID used in the URLs.
const String _appId = 'colorhap';
const String _editionId = 'aa';

static const String setWallpaper =
'https://play.google.com/store/apps/details?id=com.tecdrop.rgbcolorwallpaperpro&referrer=utm_source%3D$_appId%26utm_medium%3Dapp%26utm_campaign%3D${_appId}_${_editionId}_drawer';
static const String rate =
'https://play.google.com/store/apps/details?id=com.tecdrop.colorhap&referrer=utm_source%3D$_appId%26utm_medium%3Dapp%26utm_campaign%3D${_appId}_${_editionId}_drawer';
static const String help =
'https://www.tecdrop.com/$_appId/?utm_source=$_appId&utm_medium=app&utm_campaign=${_appId}_${_editionId}_drawer';
/// The URL for the "Set Wallpaper" drawer item.
const String setWallpaper =
'https://play.google.com/store/apps/details?id=com.tecdrop.rgbcolorwallpaperpro&referrer=utm_source%3D$_appId%26utm_medium%3Dapp%26utm_campaign%3D${_appId}_${_editionId}_drawer';

static const String viewSource = 'https://github.com/tecdrop/color_hap';
/// The URL for the app's help page.
const String help =
'https://www.tecdrop.com/$_appId/?utm_source=$_appId&utm_medium=app&utm_campaign=${_appId}_${_editionId}_drawer';

static const String onlineSearch = 'https://www.google.com/search?q=';
}
/// The URL for the app's source code.
const String viewSource = 'https://github.com/tecdrop/color_hap';

/// The URL that allows the user to rate the app (currently Google Play Store).
const String rate =
'https://play.google.com/store/apps/details?id=com.tecdrop.colorhap&referrer=utm_source%3D$_appId%26utm_medium%3Dapp%26utm_campaign%3D${_appId}_${_editionId}_drawer';

/// The URL to search for a color on the web (currently Google).
const String onlineSearch = 'https://www.google.com/search?q=';
Loading

0 comments on commit 39ced44

Please sign in to comment.