Skip to content

Commit

Permalink
Refactor UI strings and urls
Browse files Browse the repository at this point in the history
  • Loading branch information
TechAurelian committed Oct 29, 2024
1 parent 150357e commit 95077ec
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 87 deletions.
55 changes: 0 additions & 55 deletions lib/common/app_strings.dart

This file was deleted.

49 changes: 49 additions & 0 deletions lib/common/strings.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2020-2024 Hellogramming. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://hellogramming.com/helloworldcounters/license/.

/// App wide UI string constants.
library;

import '../screens/home.dart';

const String appName = 'Hello World Counters';

// -----------------------------------------------------------------------------------------------
// App Drawer
// -----------------------------------------------------------------------------------------------

const String drawerTitle = appName;
const String settingsItemTitle = 'Settings';
const String aboutItemTitle = 'About This App';
const String viewSourceItemTitle = 'View Source Code';
const String feedbackItemTitle = 'Send Feedback';

const Map<MenuAction, String> menuActions = {
MenuAction.reset: 'Reset counter',
MenuAction.share: 'Share...',
};

const String resetConfirm = 'Reset counter to zero?';
const String resetConfirmReset = 'Reset';
const String resetConfirmCancel = 'Cancel';

String shareText(String name, String value) => 'The $name is $value';

// -----------------------------------------------------------------------------------------------
// Home Screen - Main
// -----------------------------------------------------------------------------------------------

const String incrementTooltip = 'Increment';
const String incrementHeroTag = 'incrementHeroTag';
const String decrementTooltip = 'Decrement';
const String decrementHeroTag = 'decrementHeroTag';

// -----------------------------------------------------------------------------------------------
// Settings Screen
// -----------------------------------------------------------------------------------------------

const String settingsTitle = 'Settings';
const String counterTapModeTitle = 'Counter tap mode';
const String counterTapModeSubtitle = 'Tap anywhere to increase counter';
16 changes: 16 additions & 0 deletions lib/common/urls.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2020-2024 Hellogramming. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://hellogramming.com/helloworldcounters/license/.

/// URLs used in the app.
library;

/// The app's home page URL.
const String aboutURL = 'https://hellogramming.com/helloworldcounters/';

/// The URL to the source code repository of the app.
const String viewSourceURL = 'https://github.com/Hellogramming/hello_world_counters';

/// The URL for sending feedback.
const String feedbackURL = 'https://hellogramming.com/helloworldcounters/feedback/';
4 changes: 2 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import 'package:flutter/material.dart';

import 'common/app_strings.dart';
import 'common/strings.dart' as strings;
import 'screens/home.dart';

void main() {
Expand All @@ -20,7 +20,7 @@ class HelloWorldCountersApp extends StatelessWidget {
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: AppStrings.appName,
title: strings.appName,

// A black and white theme to go with the app's colored counters
theme: ThemeData.from(
Expand Down
39 changes: 20 additions & 19 deletions lib/screens/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import 'package:flutter/material.dart';
import 'package:share_plus/share_plus.dart';

import '../common/app_settings.dart';
import '../common/app_strings.dart';
import '../common/strings.dart' as strings;
import '../common/urls.dart' as urls;
import '../models/counter.dart';
import '../utils/utils.dart';
import '../widgets/accept_cancel_dialog.dart';
Expand Down Expand Up @@ -61,17 +62,17 @@ class _HomeScreenState extends State<HomeScreen> {
// Reset the counter after asking for confirmation.
showAcceptCancelDialog(
context,
AppStrings.resetConfirm,
AppStrings.resetConfirmReset,
AppStrings.resetConfirmCancel,
strings.resetConfirm,
strings.resetConfirmReset,
strings.resetConfirmCancel,
() => setState(() => _counters.current.reset()),
);
break;
case MenuAction.share:
// Share the current counter value using the platform's share sheet.
final String name = _counters.current.name;
final String value = toDecimalString(context, _counters.current.value);
Share.share(AppStrings.shareText(name, value), subject: name);
Share.share(strings.shareText(name, value), subject: name);
break;
}
}
Expand All @@ -83,16 +84,16 @@ class _HomeScreenState extends State<HomeScreen> {
_loadSettingsScreen();
break;
case DrawerExtraActions.about:
// Launch the app's about page
launchUrlExternal(context, AppStrings.aboutURL);
break;
case DrawerExtraActions.rate:
// Launch the Google Play Store page to allow the user to rate the app
launchUrlExternal(context, AppStrings.rateAppURL);
// Open the app's about page
launchUrlExternal(context, urls.aboutURL);
break;
case DrawerExtraActions.viewSource:
// Launch the app source code repo url
launchUrlExternal(context, AppStrings.viewSourceURL);
// Open the app source code repo url
launchUrlExternal(context, urls.viewSourceURL);
break;
case DrawerExtraActions.feedback:
// Open the app feedback page
launchUrlExternal(context, urls.feedbackURL);
break;
}
}
Expand Down Expand Up @@ -151,7 +152,7 @@ class _HomeScreenState extends State<HomeScreen> {
(item) => PopupMenuItem<MenuAction>(
value: item,
enabled: !(item == MenuAction.reset && _counters.current.value == 0),
child: Text(AppStrings.menuActions[item]!),
child: Text(strings.menuActions[item]!),
),
)
.toList();
Expand All @@ -160,7 +161,7 @@ class _HomeScreenState extends State<HomeScreen> {
/// Builds the main drawer that lets the user switch between color counters.
Widget _buildDrawer() {
return CountersDrawer(
title: AppStrings.drawerTitle,
title: strings.drawerTitle,
counters: _counters,
onSelected: _onDrawerSelected,
onExtraSelected: _onDrawerExtraSelected,
Expand All @@ -179,16 +180,16 @@ class _HomeScreenState extends State<HomeScreen> {
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
FloatingActionButton.large(
heroTag: AppStrings.decrementHeroTag,
heroTag: strings.decrementHeroTag,
onPressed: () => setState(() => _counters.current.decrement()),
tooltip: AppStrings.decrementTooltip,
tooltip: strings.decrementTooltip,
child: const Icon(Icons.remove),
),
isPortrait ? const SizedBox(height: 16.0) : const SizedBox(width: 16.0),
FloatingActionButton.large(
heroTag: AppStrings.incrementHeroTag,
heroTag: strings.incrementHeroTag,
onPressed: () => setState(() => _counters.current.increment()),
tooltip: AppStrings.incrementTooltip,
tooltip: strings.incrementTooltip,
child: const Icon(Icons.add),
)
],
Expand Down
8 changes: 4 additions & 4 deletions lib/screens/settings_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import 'package:flutter/material.dart';

import '../common/app_settings.dart';
import '../common/app_strings.dart';
import '../common/strings.dart' as strings;

/// The settings screen widget that allows the user to change app settings.
///
Expand All @@ -31,7 +31,7 @@ class _SettingsScreenState extends State<SettingsScreen> {

return Scaffold(
appBar: AppBar(
title: const Text(AppStrings.settingsTitle),
title: const Text(strings.settingsTitle),
),
body: Container(
alignment: Alignment.topCenter,
Expand All @@ -44,8 +44,8 @@ class _SettingsScreenState extends State<SettingsScreen> {
SwitchListTile(
activeColor: Colors.black,
value: widget.appSettings.counterTapMode,
title: const Text(AppStrings.counterTapModeTitle),
subtitle: const Text(AppStrings.counterTapModeSubtitle),
title: const Text(strings.counterTapModeTitle),
subtitle: const Text(strings.counterTapModeSubtitle),
onChanged: (bool value) =>
setState(() => widget.appSettings.counterTapMode = value),
)
Expand Down
14 changes: 7 additions & 7 deletions lib/widgets/counters_drawer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

import 'package:flutter/material.dart';

import '../common/app_strings.dart';
import '../common/strings.dart' as strings;
import '../models/counter.dart';
import '../utils/utils.dart';
import 'color_list_tile.dart';

/// Drawer extra actions enumeration.
enum DrawerExtraActions { settings, about, rate, viewSource }
enum DrawerExtraActions { settings, about, feedback, viewSource }

/// A material design drawer that shows navigation links for all available counters.
class CountersDrawer extends StatelessWidget {
Expand Down Expand Up @@ -53,24 +53,24 @@ class CountersDrawer extends StatelessWidget {
const Divider(),
ListTile(
leading: const Icon(Icons.settings),
title: const Text(AppStrings.settingsItemTitle),
title: const Text(strings.settingsItemTitle),
onTap: () => _onExtraActionTap(context, DrawerExtraActions.settings),
),
ListTile(
leading: const Icon(Icons.help_outline),
title: const Text(AppStrings.aboutItemTitle),
title: const Text(strings.aboutItemTitle),
onTap: () => _onExtraActionTap(context, DrawerExtraActions.about),
),
ListTile(
leading: const Icon(Icons.flutter_dash),
title: const Text(AppStrings.viewSourceItemTitle),
title: const Text(strings.viewSourceItemTitle),
onTap: () => _onExtraActionTap(context, DrawerExtraActions.viewSource),
),
const Divider(),
ListTile(
leading: const Icon(Icons.stars),
title: const Text(AppStrings.rateItemTitle),
onTap: () => _onExtraActionTap(context, DrawerExtraActions.rate),
title: const Text(strings.feedbackItemTitle),
onTap: () => _onExtraActionTap(context, DrawerExtraActions.feedback),
),
],
),
Expand Down

0 comments on commit 95077ec

Please sign in to comment.