diff --git a/README.md b/README.md index 5300b34..3311cc5 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/example/lib/nstack.dart b/example/lib/nstack.dart index e1112f1..b1b70c2 100644 --- a/example/lib/nstack.dart +++ b/example/lib/nstack.dart @@ -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( - 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 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()!.state; @@ -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); @@ -89,7 +123,7 @@ class NStackWidget extends StatefulWidget { } class NStackState extends State { - final NStack nstack = _nstack; + final NStack nstack = _nstack; bool _initializedNStack = false; late Future _nstackInitFuture; @@ -97,18 +131,21 @@ class NStackState extends State { @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; } @@ -116,8 +153,13 @@ class NStackState extends State { 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(); } @@ -127,11 +169,10 @@ class NStackState extends State { /// 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 on State { - Localization get localization => context.localization; + Localization get localization => context.localization; } - diff --git a/lib/src/nstack_builder.dart b/lib/src/nstack_builder.dart index c6ad2aa..778b3aa 100644 --- a/lib/src/nstack_builder.dart +++ b/lib/src/nstack_builder.dart @@ -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 { @@ -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) { diff --git a/pubspec.yaml b/pubspec.yaml index 1961960..98be912 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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: