Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 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.5.0
ref: v0.5.1

dev_dependencies:
build_runner:
Expand Down
121 changes: 81 additions & 40 deletions example/lib/nstack.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,58 +14,91 @@ export 'package:nstack/models/app_open_platform.dart';
// Update this file by running:
// - `flutter pub run build_runner build`, if your package depends on Flutter
// - `pub run build_runner build` otherwise

class Localization {
final defaultSection = const _DefaultSection();
final test = const _Test();
final defaultSection = const _DefaultSection();
final test = const _Test();

const Localization();
const Localization();
}

class _DefaultSection extends SectionKeyDelegate {
const _DefaultSection(): super('default');
const _DefaultSection() : super('default');

String get title => get('title', "NStack SDK Demo");
String get test => get('test', "test");
String get title => get('title', "NStack SDK Demo");
String get test => get('test', "test");
}

class _Test extends SectionKeyDelegate {
const _Test(): super('test');

String get testDollarSign => get('testDollarSign', "\$testing again new");
String get testSingleQuotationMark => get('testSingleQuotationMark', "\'testing\'");
String get testDoubleQuotationMark => get('testDoubleQuotationMark', "\"testing\"");
String get testMultipleLines => get('testMultipleLines', "testing\nmultiple\nlines\nupdated");
const _Test() : super('test');

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

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

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

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)),
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 again new","testSingleQuotationMark":"'testing'","testDoubleQuotationMark":"\"testing\"","testMultipleLines":"testing\nmultiple\nlines\nupdated"}},"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"}}}''',
'en-EN':
r'''{"data":{"default":{"title":"NStack SDK Demo","test":"test"},"test":{"testDollarSign":"$testing again new","testSingleQuotationMark":"'testing'","testDoubleQuotationMark":"\"testing\"","testMultipleLines":"testing\nmultiple\nlines\nupdated"}},"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":"\u00a0","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"}}}''',
};

final _nstack = NStack<Localization>(
config: _config,
localization: const Localization(),
availableLanguages: _languages,
bundledTranslations: _bundledTranslations,
pickedLanguageLocale: '',
debug: kDebugMode
);
config: _config,
localization: const Localization(),
availableLanguages: _languages,
bundledTranslations: _bundledTranslations,
pickedLanguageLocale: '',
debug: kDebugMode);

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

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

static NStackState of(BuildContext context) =>
context.dependOnInheritedWidgetOfExactType<NStackScope>()!.state;
Expand All @@ -80,7 +113,8 @@ class NStackWidget extends StatefulWidget {
final AppOpenPlatform? platformOverride;
final VoidCallback? onComplete;

const NStackWidget({Key? key, required Widget child, this.platformOverride, this.onComplete})
const NStackWidget(
{Key? key, required Widget child, this.platformOverride, this.onComplete})
: child = child,
super(key: key);

Expand All @@ -89,35 +123,43 @@ class NStackWidget extends StatefulWidget {
}

class NStackState extends State<NStackWidget> {
final NStack<Localization> nstack = _nstack;
final NStack<Localization> nstack = _nstack;
bool _initializedNStack = false;

late Future<bool> _nstackInitFuture;

@override
void initState() {
super.initState();
_nstackInitFuture = _nstack.init();
_nstackInitFuture = _nstack.init();
}

changeLanguage(Locale locale) async {
await _nstack.changeLocalization(locale).whenComplete(() => setState(() {}));
}
changeLanguage(Locale locale) async {
await _nstack
.changeLocalization(locale)
.whenComplete(() => setState(() {}));
}

@override
Widget build(BuildContext context) {
if (!_initializedNStack) {
_nstack
.appOpen(Localizations.localeOf(context), platformOverride: widget.platformOverride)
.appOpen(Localizations.localeOf(context),
platformOverride: widget.platformOverride)
.whenComplete(() => widget.onComplete?.call());
_initializedNStack = true;
}

return FutureBuilder(
future: _nstackInitFuture,
builder: (context, snapshot) {
if(snapshot.connectionState == ConnectionState.done) {
return NStackScope(child: widget.child, state: this, nstack: this.nstack, checksum: nstack.checksum,);
if (snapshot.connectionState == ConnectionState.done) {
return NStackScope(
child: widget.child,
state: this,
nstack: this.nstack,
checksum: nstack.checksum,
);
} else {
return SizedBox();
}
Expand All @@ -127,11 +169,10 @@ class NStackState extends State<NStackWidget> {

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

/// Allows to access the Nstack Localization from StatefulWidget's State
extension NStackStateExtension<T extends StatefulWidget> on State<T> {
Localization get localization => context.localization;
Localization get localization => context.localization;
}

4 changes: 3 additions & 1 deletion lib/src/nstack_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:nstack/models/nstack_config.dart';
import 'package:nstack/other/extensions.dart';
import 'package:nstack/other/reserved_keywords.dart';
import 'package:nstack/src/nstack_repository.dart';
import 'package:dart_style/dart_style.dart';

/// A builder which outputs a 'nstack.dart' file. This file provides a NStack instance, type safe section key accessors and all bundled translations.
class NstackBuilder implements Builder {
Expand Down Expand Up @@ -81,7 +82,8 @@ class NstackBuilder implements Builder {
print(s);
}

await buildStep.writeAsString(outputId, output.toString());
final outputFormatted = DartFormatter().format(output.toString());
await buildStep.writeAsString(outputId, outputFormatted);
}

void _writeHeader(StringBuffer output) {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: nstack
description: Nstack plugin for Flutter.
version: 0.5.0
version: 0.5.1
homepage: https://github.com/nstack-io/flutter-sdk

environment:
Expand Down