Skip to content

Commit

Permalink
Merge pull request #34 from nstack-io/feature/languageswitch
Browse files Browse the repository at this point in the history
Language switch and proper InheritedWidget
  • Loading branch information
johsoe authored Sep 14, 2021
2 parents 1a2f4ea + 9406791 commit f3f7bf1
Show file tree
Hide file tree
Showing 13 changed files with 349 additions and 113 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ dependencies:
nstack:
git:
url: git://github.com/nstack-io/flutter-sdk.git
ref: v0.1.4
ref: v0.4.0

dev_dependencies:
build_runner:
Expand Down Expand Up @@ -70,7 +70,7 @@ Now increment the `"version"` number and save (⌘s) to trigger an update.
## Example

Import your `nstack.dart` file and plant your `NStackWidget` at the root of your application.\
Use `NStackInitWidget` for submitting [AppOpen] events.
Use `NStackAppOpen` for submitting [AppOpen] events.

```dart
import 'package:flutter/material.dart';
Expand All @@ -84,7 +84,7 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: NStackInitWidget(
home: NStackAppOpen(
child: Scaffold(
appBar: AppBar(
title: Text(context.localization.test.title),
Expand Down
2 changes: 1 addition & 1 deletion example/ios/Flutter/AppFrameworkInfo.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
<key>CFBundleVersion</key>
<string>1.0</string>
<key>MinimumOSVersion</key>
<string>8.0</string>
<string>9.0</string>
</dict>
</plist>
2 changes: 1 addition & 1 deletion example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/shared_preferences/ios"

SPEC CHECKSUMS:
Flutter: 434fef37c0980e73bb6479ef766c45957d4b510c
Flutter: 50d75fe2f02b26cc09d224853bb45737f8b3214a
nstack: a7ebf31d8c387ec76d4bfe5eb494d3ac81756d92
package_info: 873975fc26034f0b863a300ad47e7f1ac6c7ec62
shared_preferences: af6bfa751691cdc24be3045c43ec037377ada40d
Expand Down
6 changes: 3 additions & 3 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand Down Expand Up @@ -430,7 +430,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -479,7 +479,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand Down
20 changes: 16 additions & 4 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,27 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: NStackInitWidget(child: Scaffold(
home: MainScreen(),
);
}
}

class MainScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return NStackAppOpen(
child: Scaffold(
appBar: AppBar(
title: Text(context.localization.defaultSection.title),
title: Text(context.localization.test.testDollarSign),
),
body: Center(
child: Text(context.localization.test.testMultipleLines),
child: MaterialButton(onPressed: () async => {
NStackScope.of(context).changeLanguage(Locale("de-DE"))
},
child: Text("Selected locale: ${NStackScope.of(context).nstack.activeLanguage.name}")
,),
),
),
),
);
}
}
82 changes: 57 additions & 25 deletions example/lib/nstack.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/// Generated by NStack, do not modify this file.
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:nstack/models/language.dart';
import 'package:nstack/models/localize_index.dart';
import 'package:nstack/models/nstack_config.dart';
import 'package:nstack/nstack.dart';
import 'package:nstack/partial/section_key_delegate.dart';
Expand All @@ -27,21 +29,21 @@ class _DefaultSection extends SectionKeyDelegate {
class _Test extends SectionKeyDelegate {
const _Test(): super('test');

String get testDollarSign => get('testDollarSign', "\$testing");
String get testDollarSign => get('testDollarSign', "\$testing change back");
String get testSingleQuotationMark => get('testSingleQuotationMark', "\'testing\'");
String get testDoubleQuotationMark => get('testDoubleQuotationMark', "\"testing\"");
String get testMultipleLines => get('testMultipleLines', "testing\nmultiple\nlines");
}

const _config = NStackConfig(projectId: 'h6wJremI2TGFM88gbLkdyljWQuwf2hxhxvCH', apiKey: 'zp2S18H32b67eYAbRQh94tVw76ZzaKKXlHjd');

const _languages = [
Language(id: 56, name: 'English', locale: 'en-EN', direction: 'LRM', isDefault: true, isBestFit: true),
Language(id: 7, name: 'German (Austria)', locale: 'de-AT', direction: 'LRM', isDefault: false, isBestFit: false),
final _languages = [
LocalizeIndex(id: 1216, url: null, lastUpdatedAt: null, shouldUpdate: false, language: Language(id: 56, name: 'English', locale: 'en-EN', direction: 'LRM', isDefault: true, isBestFit: true)),
LocalizeIndex(id: 1270, url: null, lastUpdatedAt: null, shouldUpdate: false, language: Language(id: 7, name: 'German (Austria)', locale: 'de-AT', direction: 'LRM', isDefault: false, isBestFit: false)),
];

const _bundledTranslations = {
'en-EN': r'''{"data":{"default":{"title":"NStack SDK Demo","test":"test"},"test":{"testDollarSign":"$testing","testSingleQuotationMark":"'testing'","testDoubleQuotationMark":"\"testing\"","testMultipleLines":"testing\nmultiple\nlines"}},"meta":{"language":{"id":56,"name":"English","locale":"en-EN","direction":"LRM","is_default":false,"is_best_fit":false},"platform":{"id":515,"slug":"mobile"}}}''',
'en-EN': r'''{"data":{"default":{"title":"NStack SDK Demo","test":"test"},"test":{"testDollarSign":"$testing change back","testSingleQuotationMark":"'testing'","testDoubleQuotationMark":"\"testing\"","testMultipleLines":"testing\nmultiple\nlines"}},"meta":{"language":{"id":56,"name":"English","locale":"en-EN","direction":"LRM","is_default":false,"is_best_fit":false},"platform":{"id":515,"slug":"mobile"}}}''',
'de-AT': r'''{"data":{"default":{"title":"NStack SDK Demo","test":"test"},"test":{"testDollarSign":"__testDollarSign","testSingleQuotationMark":"__testSingleQuotationMark","testDoubleQuotationMark":"__testDoubleQuotationMark","testMultipleLines":"__testMultipleLines"}},"meta":{"language":{"id":7,"name":"German (Austria)","locale":"de-AT","direction":"LRM","is_default":false,"is_best_fit":false},"platform":{"id":515,"slug":"mobile"}}}''',
};

Expand All @@ -51,55 +53,85 @@ final _nstack = NStack<Localization>(
availableLanguages: _languages,
bundledTranslations: _bundledTranslations,
pickedLanguageLocale: '',
debug: kDebugMode
);

class NStackWidget extends InheritedWidget {
final NStack<Localization> nstack = _nstack;
class NStackScope extends InheritedWidget {
final NStack<Localization> nstack;
final NStackState state;
final String checksum;

NStackWidget({Key? key, required Widget child})
NStackScope({Key? key, required Widget child, required this.state, required this.nstack, required this.checksum})
: super(key: key, child: child);

static NStack of(BuildContext context) =>
context.dependOnInheritedWidgetOfExactType<NStackWidget>()!.nstack;
static NStackState of(BuildContext context) =>
context.dependOnInheritedWidgetOfExactType<NStackScope>()!.state;

@override
bool updateShouldNotify(NStackWidget oldWidget) =>
nstack != oldWidget.nstack;
bool updateShouldNotify(NStackScope oldWidget) =>
checksum != oldWidget.checksum;
}

class NStackInitWidget extends StatefulWidget {
class NStackWidget extends StatefulWidget {
final Widget child;

const NStackInitWidget({Key? key, required Widget child})
const NStackWidget({Key? key, required Widget child})
: child = child,
super(key: key);

@override
_NStackInitState createState() => _NStackInitState();
NStackState createState() => NStackState();
}

class _NStackInitState extends State<NStackInitWidget> {
static bool _initialized = false;
class NStackState extends State<NStackWidget> {
final NStack<Localization> nstack = _nstack;

void setupNStack(BuildContext context) {
final locale = Localizations.localeOf(context);
final nstack = NStackWidget.of(context);
nstack.appOpen(locale);
changeLanguage(Locale locale) async {
await _nstack.changeLocalization(locale).whenComplete(() => setState(() {}));
}

Future<void> appOpen(Locale locale) async {
await _nstack.appOpen(locale).whenComplete(() => setState(() {}));
}

@override
Widget build(BuildContext context) {
if (!_initialized) {
setupNStack(context);
_initialized = true;
return NStackScope(child: widget.child, state: this, nstack: this.nstack, checksum: nstack.checksum,);
}
}

class NStackAppOpen extends StatefulWidget {
const NStackAppOpen({
Key? key,
required this.child,
this.onComplete,
}) : super(key: key);

final Widget child;
final VoidCallback? onComplete;

@override
_NStackAppOpenState createState() => _NStackAppOpenState();
}

class _NStackAppOpenState extends State<NStackAppOpen> {
bool _initializedNStack = false;

@override
Widget build(BuildContext context) {
if (!_initializedNStack) {
NStackScope.of(context)
.appOpen(Localizations.localeOf(context))
.whenComplete(() => widget.onComplete?.call());
_initializedNStack = true;
}
return widget.child;
}
}

/// Allows to access the Nstack Localization using the BuildContext
extension NStackWidgetExtension on BuildContext {
Localization get localization => NStackWidget.of(this).localization;
Localization get localization => NStackScope.of(this).nstack.localization;
}

/// Allows to access the Nstack Localization from StatefulWidget's State
Expand Down
6 changes: 3 additions & 3 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ packages:
name: json_annotation
url: "https://pub.dartlang.org"
source: hosted
version: "4.1.0"
version: "4.0.1"
logging:
dependency: transitive
description:
Expand Down Expand Up @@ -302,7 +302,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.2.5"
version: "0.4.0"
package_config:
dependency: transitive
description:
Expand Down Expand Up @@ -503,7 +503,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.0"
version: "0.4.2"
timing:
dependency: transitive
description:
Expand Down
Loading

0 comments on commit f3f7bf1

Please sign in to comment.