Skip to content

Commit

Permalink
Merge pull request #33 from vanlooverenkoen/feature/#32-nullsafety
Browse files Browse the repository at this point in the history
#32 wrote the full implmentation for the nullsafe release
  • Loading branch information
vanlooverenkoen authored Mar 4, 2021
2 parents 41d3585 + 2986152 commit 250a032
Show file tree
Hide file tree
Showing 18 changed files with 65 additions and 127 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [3.0.0] - 2021-03-07
### Added
-#32 Nullsafety implementation. Support for flutter 2.0 & dart 2.12.0
### Removed
-#32 nullsafe flag is removed because from this version we are targeting 2.12 which will use nullsafety by default

## [2.1.0] - 2021-02-09
###
-Added option to generate null safety compatible code
Expand Down
19 changes: 0 additions & 19 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@
97C146EC1CF9000F007C117D /* Resources */,
9705A1C41CF9048500538489 /* Embed Frameworks */,
3B06AD1E1E4923F5004D2608 /* Thin Binary */,
F48304C09168E6E040649494 /* [CP] Embed Pods Frameworks */,
);
buildRules = (
);
Expand Down Expand Up @@ -258,24 +257,6 @@
shellPath = /bin/sh;
shellScript = "/bin/sh \"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh\" build";
};
F48304C09168E6E040649494 /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh",
"${PODS_ROOT}/../Flutter/Flutter.framework",
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Flutter.framework",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
/* End PBXShellScriptBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions example/lib/app.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:kiwi/kiwi.dart' as kiwi;
import 'package:locale_gen_example/repository/locale_repository.dart';
import 'package:locale_gen_example/screen/home_screen.dart';
import 'package:locale_gen_example/util/locale/localization_delegate.dart';
import 'package:locale_gen_example/viewmodel/locale/locale_viewmodel.dart';
Expand All @@ -26,7 +26,7 @@ class MyApp extends StatelessWidget {
home: HomeScreen(),
),
),
create: (context) => kiwi.Container().resolve()..init(),
create: (context) => LocaleViewModel(LocaleRepository())..init(),
);
}
}
19 changes: 0 additions & 19 deletions example/lib/di/injector.dart

This file was deleted.

19 changes: 0 additions & 19 deletions example/lib/di/injector.g.dart

This file was deleted.

2 changes: 0 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import 'package:flutter/material.dart';
import 'package:locale_gen_example/app.dart';
import 'package:locale_gen_example/di/injector.dart' as kiwi;

Future<void> main() async {
kiwi.setupDependencyTree();
runApp(MyApp());
}
9 changes: 6 additions & 3 deletions example/lib/repository/locale_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import 'package:shared_preferences/shared_preferences.dart';

class LocaleRepository {
static const STORE_LOCALE = 'locale';
static LocaleRepository? _instance;

LocaleRepository();
LocaleRepository._();

Future<void> setCustomLocale(Locale locale) async {
factory LocaleRepository() => _instance ??= LocaleRepository._();

Future<void> setCustomLocale(Locale? locale) async {
final prefs = await SharedPreferences.getInstance();
if (locale == null) {
print('Reset custom locale. Use system language');
Expand All @@ -17,7 +20,7 @@ class LocaleRepository {
}

//can be null
Future<Locale> getCustomLocale() async {
Future<Locale?> getCustomLocale() async {
final prefs = await SharedPreferences.getInstance();
final localeCode = prefs.getString(STORE_LOCALE);
if (localeCode == null || localeCode.isEmpty) return null;
Expand Down
6 changes: 3 additions & 3 deletions example/lib/screen/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ class HomeScreen extends StatelessWidget {
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FlatButton(
MaterialButton(
child: const Text('System Language (Not translated)'),
onPressed: Provider.of<LocaleViewModel>(context)
.onSwitchToSystemLanguage,
),
FlatButton(
MaterialButton(
child: const Text('English (Not translated)'),
onPressed:
Provider.of<LocaleViewModel>(context).onSwitchToEnglish,
),
FlatButton(
MaterialButton(
child: const Text('Nederlands (Not translated)'),
onPressed: Provider.of<LocaleViewModel>(context).onSwitchToDutch,
),
Expand Down
8 changes: 4 additions & 4 deletions example/lib/util/locale/localization.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Localization {
Map<String, dynamic> _localisedValues = Map();

static Localization of(BuildContext context) =>
Localizations.of<Localization>(context, Localization);
Localizations.of<Localization>(context, Localization)!;

static Future<Localization> load(Locale locale,
{bool showLocalizationKeys = false}) async {
Expand All @@ -27,7 +27,7 @@ class Localization {
return localizations;
}

String _t(String key, {List<dynamic> args}) {
String _t(String key, {List<dynamic>? args}) {
try {
// ignore: avoid_as
var value = _localisedValues[key] as String;
Expand Down Expand Up @@ -64,6 +64,6 @@ class Localization {
String testArg4(String arg1, num arg2) =>
_t(LocalizationKeys.testArg4, args: [arg1, arg2]);

String getTranslation(String key, {List<dynamic> args}) =>
_t(key, args: args ?? List());
String getTranslation(String key, {List<dynamic>? args}) =>
_t(key, args: args ?? []);
}
10 changes: 5 additions & 5 deletions example/lib/util/locale/localization_delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ class LocalizationDelegate extends LocalizationsDelegate<Localization> {
Locale('nl'),
];

Locale newLocale;
Locale activeLocale;
Locale? newLocale;
Locale? activeLocale;
bool showLocalizationKeys;

LocalizationDelegate({this.newLocale, this.showLocalizationKeys = false}) {
if (newLocale != null) {
activeLocale = newLocale;
}
showLocalizationKeys ??= false;
}

@override
Expand All @@ -35,8 +34,9 @@ class LocalizationDelegate extends LocalizationsDelegate<Localization> {

@override
Future<Localization> load(Locale locale) async {
activeLocale = newLocale ?? locale;
return Localization.load(activeLocale,
final newActiveLocale = newLocale ?? locale;
activeLocale = newActiveLocale;
return Localization.load(newActiveLocale,
showLocalizationKeys: showLocalizationKeys);
}

Expand Down
2 changes: 1 addition & 1 deletion example/lib/viewmodel/locale/locale_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class LocaleViewModel with ChangeNotifier {
await _onUpdateLocaleClicked(null);
}

Future<void> _onUpdateLocaleClicked(Locale locale) async {
Future<void> _onUpdateLocaleClicked(Locale? locale) async {
await _localeRepository.setCustomLocale(locale);
localeDelegate = LocalizationDelegate(newLocale: locale);
notifyListeners();
Expand Down
14 changes: 3 additions & 11 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,19 @@ description: A project used to demo the Todo Reporter
version: 1.0.0+1

environment:
sdk: ">=2.0.0-dev.68.0 <3.0.0"
sdk: ">=2.12.0 <3.0.0"

dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
provider: ^3.0.0+1
kiwi: ^0.1.0
shared_preferences: ^0.5.3+1
provider: ^5.0.0
shared_preferences: ^2.0.3

dev_dependencies:
flutter_test:
sdk: flutter
build_runner: ^1.3.1
built_value_generator: ^6.4.0
kiwi_generator:
git:
url: https://github.com/simolus3/kiwi.git
ref: 6ead6e68b535bf935df37b29f7d25b283337225e
path: kiwi_generator
locale_gen:
path: ../

Expand Down
2 changes: 1 addition & 1 deletion lib/src/case_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CaseUtil {

static List<String> _groupIntoWords(String text) {
final sb = StringBuffer();
final words = List<String>();
final words = <String>[];
final isAllCaps = !text.contains(RegExp('[a-z]'));

for (var i = 0; i < text.length; i++) {
Expand Down
29 changes: 16 additions & 13 deletions lib/src/locale_gen_params.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ final defaultLocaleAssetsDir = join('assets', 'locale');

class LocaleGenParams {
final String programName;
String outputDir = defaultOutputDir;
String? outputDir = defaultOutputDir;
String assetsDir = defaultAssetsDir;
String localeAssetsDir = defaultLocaleAssetsDir;
bool nullSafe = false;

String projectName;
String defaultLanguage;
List<String> languages;
late String projectName;
late String defaultLanguage;
late List<String> languages;

LocaleGenParams(this.programName) {
final pubspecYaml = File(join(Directory.current.path, 'pubspec.yaml'));
Expand All @@ -29,13 +28,14 @@ class LocaleGenParams {
final pubspecContent = pubspecYaml.readAsStringSync();

final doc = loadYaml(pubspecContent);
projectName = doc['name'];
final projectName = doc['name'];

if (projectName == null || projectName.isEmpty) {
throw Exception(
'Could not parse the pubspec.yaml, project name not found');
}

this.projectName = projectName;
final config = doc[programName];
if (config == null) {
languages = ['en'];
Expand All @@ -47,23 +47,23 @@ class LocaleGenParams {

@mustCallSuper
void configure(YamlMap config) {
final YamlList yamlList = config['languages'];
final YamlList? yamlList = config['languages'];
if (yamlList == null || yamlList.isEmpty) {
throw Exception(
"At least 1 language should be added to the 'languages' section in the pubspec.yaml\n"
'$programName\n'
" languages: ['en']");
}

languages = yamlList.map((item) => item.toString()).toList();
if (languages == null || languages.isEmpty) {
final languages = yamlList.map((item) => item.toString()).toList();
if (languages.isEmpty) {
throw Exception(
"At least 1 language should be added to the 'languages' section in the pubspec.yaml\n"
'$programName\n'
" languages: ['en']");
}

defaultLanguage = config['default_language'];
var defaultLanguage = config['default_language'];
if (defaultLanguage == null) {
if (languages.contains('en')) {
defaultLanguage = 'en';
Expand All @@ -78,17 +78,20 @@ class LocaleGenParams {

outputDir ??= defaultOutputDir;

assetsDir = config['assets_path'];
var assetsDir = config['assets_path'];
assetsDir ??= defaultAssetsDir;
if (!assetsDir.endsWith('/')) {
assetsDir += '/';
}
localeAssetsDir = config['locale_assets_path'];
var localeAssetsDir = config['locale_assets_path'];
localeAssetsDir ??= defaultLocaleAssetsDir;
if (!localeAssetsDir.endsWith('/')) {
localeAssetsDir += '/';
}

nullSafe = config['nullsafety'] == true;
this.localeAssetsDir = localeAssetsDir;
this.assetsDir = assetsDir;
this.languages = languages;
this.defaultLanguage = defaultLanguage;
}
}
Loading

0 comments on commit 250a032

Please sign in to comment.