Skip to content

Commit

Permalink
Merge pull request #98 from Terence-D/translations
Browse files Browse the repository at this point in the history
Refactored code to better handle text and locales
  • Loading branch information
Terence-D authored Oct 18, 2019
2 parents 896fd5f + 5c5fc47 commit 926c7e3
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 77 deletions.
35 changes: 35 additions & 0 deletions gic_flutter/lib/app.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:gic_flutter/screens/main/main.dart';

import 'model/intl/localizations.dart';
import 'services/setting/settingRepository.dart';
import 'theme/theme.dart';

class GicApp extends StatelessWidget {
SettingRepository _settingRepository ;

GicApp (SettingRepository
sharedPreferences) {
_settingRepository = sharedPreferences;
}

@override
Widget build(BuildContext context) {
return MaterialApp(
onGenerateTitle: (BuildContext context) => Intl.of(context).title,
theme: CustomTheme.of(context),
//theme: lightTheme(),
//darkTheme: darkTheme(),
home: MainScreen(_settingRepository),
localizationsDelegates: [
const IntlDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
const Locale('en', ''),
],
);
}
}
23 changes: 2 additions & 21 deletions gic_flutter/lib/main_gplay.dart
Original file line number Diff line number Diff line change
@@ -1,37 +1,18 @@
import 'package:flutter/material.dart';

import 'package:gic_flutter/screens/main/main.dart';
import 'package:gic_flutter/services/setting/settingRepository.dart';
import 'package:gic_flutter/theme/theme.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'app.dart';
import 'flavor.dart';

void main() async {
BuildEnvironment.init(flavor: BuildFlavor.gplay);
var myApp = MyApp(SettingRepository(await SharedPreferences.getInstance()));
var myApp = GicApp(SettingRepository(await SharedPreferences.getInstance()));
var myTheme = CustomTheme(
initialThemeKey: ThemeKeys.DARK,
child: myApp,
);
WidgetsFlutterBinding.ensureInitialized();
runApp(myTheme);
}

class MyApp extends StatelessWidget {
SettingRepository _settingRepository ;

MyApp (SettingRepository sharedPreferences) {
_settingRepository = sharedPreferences;
}

@override
Widget build(BuildContext context) {
return MaterialApp(
theme: CustomTheme.of(context),
title: 'Gaming Interface Client',
//theme: lightTheme(),
//darkTheme: darkTheme(),
home: MainScreen(_settingRepository),
);
}
}
23 changes: 2 additions & 21 deletions gic_flutter/lib/main_other.dart
Original file line number Diff line number Diff line change
@@ -1,37 +1,18 @@
import 'package:flutter/material.dart';

import 'package:gic_flutter/screens/main/main.dart';
import 'package:gic_flutter/services/setting/settingRepository.dart';
import 'package:gic_flutter/theme/theme.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'app.dart';
import 'flavor.dart';

void main() async {
BuildEnvironment.init(flavor: BuildFlavor.other);
var myApp = MyApp(SettingRepository(await SharedPreferences.getInstance()));
var myApp = GicApp(SettingRepository(await SharedPreferences.getInstance()));
var myTheme = CustomTheme(
initialThemeKey: ThemeKeys.DARK,
child: myApp,
);
WidgetsFlutterBinding.ensureInitialized();
runApp(myTheme);
}

class MyApp extends StatelessWidget {
SettingRepository _settingRepository ;

MyApp (SettingRepository sharedPreferences) {
_settingRepository = sharedPreferences;
}

@override
Widget build(BuildContext context) {
return MaterialApp(
theme: CustomTheme.of(context),
title: 'Gaming Interface Client',
//theme: lightTheme(),
//darkTheme: darkTheme(),
home: MainScreen(_settingRepository),
);
}
}
95 changes: 95 additions & 0 deletions gic_flutter/lib/model/intl/localizations.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart' show SynchronousFuture;

class IntlDelegate extends LocalizationsDelegate<Intl> {
const IntlDelegate();

@override
bool isSupported(Locale locale) => ['en', 'es'].contains(locale.languageCode);

@override
Future<Intl> load(Locale locale) {
// Returning a SynchronousFuture here because an async "load" operation
// isn't needed to produce an instance of DemoLocalizations.
return SynchronousFuture<Intl>(Intl(locale));
}

@override
bool shouldReload(IntlDelegate old) => false;
}

class Intl {
Intl(this.locale);

final Locale locale;

static Intl of(BuildContext context) {
return Localizations.of<Intl>(context, Intl);
}

String getText(String resource) {
return _localized[locale.languageCode][resource];
}

static Map<String, Map<String, String>> _localized = {
'en': {
'title': 'Gaming Interface Client ',

'mainPasswordError': 'invalid password, it must be at least 6 digits long',
'mainInvalidPort': 'invalid port number',
'mainFirewallError': 'Error connecting, is the server running and firewall ports opened?',
'mainAddress': 'Address',
'mainPort': 'Port',
'mainPassword': 'Password',
'mainPasswordWarning': 'Warning - do NOT use an existing password that you use ANYWHERE else',
'mainScreenManager': 'Screen Manager',
'mainErrorNoScreen': 'You need to add a screen from the screen manager first!',
'mainStart': 'Start',
'mainWrongVersion': 'Wrong Version',
'mainOutOfDate': 'The GIC Server appears to be out of date - please upgrade to the latest version by clicking on the "Website" link on the server. If you did not yet install the server, click the Help button',
'mainClose': 'Close',
'mainNext': 'Next',
'mainHelpIpAddress': 'IP Address: The network address of the computer running the server. This can be found in Windows 10 by going into Settings, then Network and Internet, and usually starts with "192"',
'mainHelpPassword': 'Password: this has to match on the server as well, and is used to provide some security',
'mainHelpScreenList': 'Screen List: This will let you select different screens you have created to use',
'mainHelpScreenManager': 'Manager: Tapping on this will open up the Screen Manager where you can create, edit, delete, and import/export other screens',
'mainHelpStart': 'Start: Tapping this will connect to the server and let you start with the screen you\'ve selected',

'menuTheme': 'Toggle Theme',
'menuIntro': 'Show Intro',
'menuAbout': 'About',
'menuDonate': 'Donate',
}
};

//these are used in view models where context aren't available... i should do all in here, but for now its as needed
static String get mainPasswordError { return 'mainPasswordError'; }
static String get mainInvalidPort { return 'mainInvalidPort'; }
static String get mainFirewallError { return 'mainFirewallError'; }
static String get menuTheme { return 'menuTheme'; }
static String get menuIntro { return 'menuIntro'; }
static String get menuAbout { return 'menuAbout'; }
static String get menuDonate { return 'menuDonate'; }

//useful where context is available.. as above i should do this for all
String get title { return _localized[locale.languageCode]['title']; }
String get mainAddress { return _localized[locale.languageCode]['mainAddress']; }
String get mainPassword { return _localized[locale.languageCode]['mainPassword']; }
String get mainPort { return _localized[locale.languageCode]['mainPort']; }
String get mainPasswordWarning { return _localized[locale.languageCode]['mainPasswordWarning']; }
String get mainScreenManager { return _localized[locale.languageCode]['mainScreenManager']; }
String get mainErrorNoScreen { return _localized[locale.languageCode]['mainErrorNoScreen']; }
String get mainStart { return _localized[locale.languageCode]['mainStart']; }
String get mainWrongVersion { return _localized[locale.languageCode]['mainWrongVersion']; }
String get mainOutOfDate { return _localized[locale.languageCode]['mainOutOfDate']; }
String get mainClose { return _localized[locale.languageCode]['mainClose']; }
String get mainHelpIpAddress { return _localized[locale.languageCode]['mainHelpIpAddress']; }
String get mainHelpPassword { return _localized[locale.languageCode]['mainHelpPassword']; }
String get mainHelpScreenList { return _localized[locale.languageCode]['mainHelpScreenList']; }
String get mainHelpScreenManager { return _localized[locale.languageCode]['mainHelpScreenManager']; }
String get mainHelpStart { return _localized[locale.languageCode]['mainHelpStart']; }
String get mainNext { return _localized[locale.languageCode]['mainNext']; }
}

59 changes: 29 additions & 30 deletions gic_flutter/lib/screens/main/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import 'dart:collection';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:gic_flutter/flavor.dart';

import 'package:gic_flutter/model/channel.dart';
import 'package:gic_flutter/model/intl/localizations.dart';
import 'package:gic_flutter/model/mainVM.dart';
import 'package:gic_flutter/screens/main/mainPresentation.dart';
import 'package:gic_flutter/services/setting/settingRepository.dart';
Expand All @@ -12,8 +14,6 @@ import 'package:gic_flutter/theme/theme.dart';
import 'package:highlighter_coachmark/highlighter_coachmark.dart';
import 'package:toast/toast.dart';

import '../../flavor.dart';

class MainScreen extends StatefulWidget {
final SettingRepository repository;

Expand Down Expand Up @@ -142,7 +142,7 @@ class MainScreenState extends State<MainScreen> {
return rv.map((_MenuOptions choice) {
return PopupMenuItem<_MenuOptions>(
value: choice,
child: Text(choice.title),
child: Text( Intl.of(context).getText(choice.title)),
);
}).toList();
},
Expand Down Expand Up @@ -179,27 +179,27 @@ class MainScreenState extends State<MainScreen> {
TextFormField(
key: _addressKey,
controller: addressController,
decoration: InputDecoration(hintText: "Address"),
decoration: InputDecoration(hintText: Intl.of(context).mainAddress),
),
TextFormField(
key: _portKey,
controller: portController,
decoration: InputDecoration(
hintText: "Port",
hintText: Intl.of(context).mainPort,
),
),
TextFormField(
key: _passwordKey,
controller: passwordController,
obscureText: true,
decoration: InputDecoration(
hintText: "Password",
hintText: Intl.of(context).mainPassword,
),
),
Padding(
padding: EdgeInsets.all(dim.activityMargin),
child: Text(
'Warning - do NOT use an existing password that you use ANYWHERE else',
Intl.of(context).mainPasswordWarning,
),
),
Row(
Expand Down Expand Up @@ -229,7 +229,7 @@ class MainScreenState extends State<MainScreen> {
presentation
.getNewActivity(Channel.actionViewManager);
},
child: Text('Screen Manager'),
child: Text(Intl.of(context).mainScreenManager),
),
],
),
Expand All @@ -242,14 +242,14 @@ class MainScreenState extends State<MainScreen> {
onPressed: () {
if (presentation.screenList.length < 1)
showMessage(
"You need to add a screen from the screen manager first!");
Intl.of(context).mainErrorNoScreen);
presentation.startGame(
passwordController.text,
addressController.text,
portController.text,
selectedScreen.id);
},
label: Text('Start'),
label: Text(Intl.of(context).mainStart),
)); //
}

Expand Down Expand Up @@ -281,7 +281,7 @@ class MainScreenState extends State<MainScreen> {
}

void showMessage(String text) {
Toast.show(text, context,
Toast.show(Intl.of(context).getText(text), context,
duration: Toast.LENGTH_LONG, gravity: Toast.BOTTOM);
}

Expand All @@ -290,12 +290,11 @@ class MainScreenState extends State<MainScreen> {
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: new Text("Wrong Version"),
content: new Text(
"The GIC Server appears to be out of date - please upgrade to the latest version by clicking on the \"Website\" link on the server. If you did not yet install the server, click the Help button"),
title: new Text(Intl.of(context).mainWrongVersion),
content: new Text(Intl.of(context).mainOutOfDate),
actions: <Widget>[
new FlatButton(
child: new Text("Close"),
child: new Text(Intl.of(context).mainClose),
onPressed: () {
Navigator.of(context).pop();
},
Expand All @@ -312,27 +311,27 @@ class MainScreenState extends State<MainScreen> {
void _loadHelp() {
highlights = new Queue();
highlights.add(new HighligherHelp(
"IP Address: The network address of the computer running the server. This can be found in Windows 10 by going into Settings, then Network and Internet, and usually starts with \"192\"",
Intl.of(context).mainHelpIpAddress,
_addressKey,
.25,
MainAxisAlignment.center));
highlights.add(new HighligherHelp(
"Password: this has to match on the server as well, and is used to provide some security",
Intl.of(context).mainHelpPassword,
_passwordKey,
.25,
MainAxisAlignment.center));
highlights.add(new HighligherHelp(
"Screen List: This will let you select different screens you have created to use",
Intl.of(context).mainHelpScreenList,
_listKey,
1,
MainAxisAlignment.end));
highlights.add(new HighligherHelp(
"Manager: Tapping on this will open up the Screen Manager where you can create, edit, delete, and import/export other screens",
Intl.of(context).mainHelpScreenManager,
_manageKey,
1,
MainAxisAlignment.end));
highlights.add(new HighligherHelp(
"Start: Tapping this will connect to the server and let you start with the screen you\'ve selected",
Intl.of(context).mainHelpStart,
_fabKey,
1,
MainAxisAlignment.center));
Expand Down Expand Up @@ -378,7 +377,7 @@ class MainScreenState extends State<MainScreen> {
color: Colors.white,
)),
RaisedButton(
child: new Text("Next"),
child: new Text(Intl.of(context).mainNext),
onPressed: () {
_showHelp();
}),
Expand All @@ -396,15 +395,15 @@ class _MenuOptions {
final IconData icon;
}

const List<_MenuOptions> _choices = const <_MenuOptions>[
const _MenuOptions(title: 'Toggle Theme', icon: Icons.color_lens),
const _MenuOptions(title: 'Show Intro', icon: Icons.thumb_up),
const _MenuOptions(title: 'About', icon: Icons.info_outline),
const _MenuOptions(title: 'Donate', icon: Icons.present_to_all),
List<_MenuOptions> _choices = <_MenuOptions>[
_MenuOptions(title: Intl.menuTheme, icon: Icons.color_lens),
_MenuOptions(title: Intl.menuIntro, icon: Icons.thumb_up),
_MenuOptions(title: Intl.menuAbout, icon: Icons.info_outline),
_MenuOptions(title: Intl.menuDonate, icon: Icons.present_to_all),
];

const List<_MenuOptions> _choicesOther = const <_MenuOptions>[
const _MenuOptions(title: 'Toggle Theme', icon: Icons.color_lens),
const _MenuOptions(title: 'Show Intro', icon: Icons.thumb_up),
const _MenuOptions(title: 'About', icon: Icons.info_outline),
List<_MenuOptions> _choicesOther = <_MenuOptions>[
_MenuOptions(title: Intl.menuTheme, icon: Icons.color_lens),
_MenuOptions(title: Intl.menuIntro, icon: Icons.thumb_up),
_MenuOptions(title: Intl.menuAbout, icon: Icons.info_outline),
];
Loading

0 comments on commit 926c7e3

Please sign in to comment.