Skip to content

Commit 03e9767

Browse files
authored
refactor(#46): Singleton NStack instance to expose business logic features (#49)
* refactor(#46): Write NStack & its Config first * refactor(#46): Improve comments * refactor(#46): Add missing commas & restructure a bit * refactor(#46): Regenerate `nstack.dart` * refactor(#46): NStack -> NStackSdk, introduce NStackLocalization * refactor(#46): Update the generator * refactor(#46): Update example project * nit * nit
1 parent 85774e3 commit 03e9767

File tree

7 files changed

+724
-478
lines changed

7 files changed

+724
-478
lines changed

example/lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class MainScreen extends StatelessWidget {
2828

2929
@override
3030
Widget build(BuildContext context) {
31-
final activeLanguage = NStackScope.of(context).nstack.activeLanguage;
31+
final activeLanguage = context.nstack.localization.activeLanguage;
3232

3333
return Scaffold(
3434
appBar: AppBar(

example/lib/nstack.dart

Lines changed: 154 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,113 @@
1-
/// Generated by NStack, do not modify this file.
1+
/*
2+
* ❌ GENERATED BY NSTACK, DO NOT MODIFY THIS FILE BY HAND!
3+
*
4+
* To update this file, run
5+
*
6+
* Flutter projects:
7+
* flutter pub run build_runner build
8+
*
9+
* Dart projects:
10+
* pub run build_runner build
11+
*
12+
* 💡 FEATURES
13+
*
14+
* To access the NStack features:
15+
* - Use the global `NStack` object;
16+
* - Use the BuildContext extension: `context.nstack`.
17+
*
18+
* 🔤 LOCALIZATION
19+
*
20+
* To access localization in your UI you can use an extension for BuildContexts:
21+
* `context.localization.yourSection.yourKey`.
22+
*/
223

324
import 'package:flutter/foundation.dart';
425
import 'package:flutter/widgets.dart';
526
import 'package:nstack/models/app_open_platform.dart';
627
import 'package:nstack/models/language.dart';
728
import 'package:nstack/models/localize_index.dart';
829
import 'package:nstack/models/nstack_config.dart';
9-
import 'package:nstack/nstack.dart';
30+
import 'package:nstack/sdk/nstack_sdk.dart';
31+
import 'package:nstack/sdk/localization/nstack_localization.dart';
1032
import 'package:nstack/partial/section_key_delegate.dart';
1133

1234
export 'package:nstack/models/app_open_platform.dart';
1335

14-
// Update this file by running:
15-
// - `flutter pub run build_runner build`, if your package depends on Flutter
16-
// - `pub run build_runner build` otherwise
36+
/*
37+
*
38+
* NStack Config
39+
*
40+
*/
41+
42+
final NStack = NStackSdk<Localization>(
43+
config: _config,
44+
localization: _nstackLocalization,
45+
isDebug: kDebugMode,
46+
);
47+
48+
final _nstackLocalization = NStackLocalization<Localization>(
49+
config: _config,
50+
translations: const Localization(),
51+
availableLanguages: _languages,
52+
bundledTranslations: _bundledTranslations,
53+
pickedLanguageLocale: '',
54+
isDebug: kDebugMode,
55+
);
56+
57+
const _config = NStackConfig(
58+
projectId: 'h6wJremI2TGFM88gbLkdyljWQuwf2hxhxvCH',
59+
apiKey: 'zp2S18H32b67eYAbRQh94tVw76ZzaKKXlHjd',
60+
);
61+
62+
/*
63+
*
64+
* Languages & Bundled translations
65+
*
66+
*/
67+
68+
final _languages = [
69+
LocalizeIndex(
70+
id: 1216,
71+
url: null,
72+
lastUpdatedAt: null,
73+
shouldUpdate: false,
74+
language: const Language(
75+
id: 56,
76+
name: 'English',
77+
locale: 'en-EN',
78+
direction: 'LRM',
79+
isDefault: true,
80+
isBestFit: true,
81+
),
82+
),
83+
LocalizeIndex(
84+
id: 1270,
85+
url: null,
86+
lastUpdatedAt: null,
87+
shouldUpdate: false,
88+
language: const Language(
89+
id: 7,
90+
name: 'German (Austria)',
91+
locale: 'de-AT',
92+
direction: 'LRM',
93+
isDefault: false,
94+
isBestFit: false,
95+
),
96+
),
97+
];
98+
99+
const _bundledTranslations = {
100+
'en-EN':
101+
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"}}}''',
102+
'de-AT':
103+
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"}}}''',
104+
};
105+
106+
/*
107+
*
108+
* Localization & Localization Section Keys
109+
*
110+
*/
17111

18112
class Localization {
19113
final defaultSection = const _DefaultSection();
@@ -41,64 +135,21 @@ class _Test extends SectionKeyDelegate {
41135
get('testMultipleLines', "testing\nmultiple\nlines\nupdated");
42136
}
43137

44-
const _config = NStackConfig(
45-
projectId: 'h6wJremI2TGFM88gbLkdyljWQuwf2hxhxvCH',
46-
apiKey: 'zp2S18H32b67eYAbRQh94tVw76ZzaKKXlHjd');
47-
48-
final _languages = [
49-
LocalizeIndex(
50-
id: 1216,
51-
url: null,
52-
lastUpdatedAt: null,
53-
shouldUpdate: false,
54-
language: Language(
55-
id: 56,
56-
name: 'English',
57-
locale: 'en-EN',
58-
direction: 'LRM',
59-
isDefault: true,
60-
isBestFit: true)),
61-
LocalizeIndex(
62-
id: 1270,
63-
url: null,
64-
lastUpdatedAt: null,
65-
shouldUpdate: false,
66-
language: Language(
67-
id: 7,
68-
name: 'German (Austria)',
69-
locale: 'de-AT',
70-
direction: 'LRM',
71-
isDefault: false,
72-
isBestFit: false)),
73-
];
74-
75-
const _bundledTranslations = {
76-
'en-EN':
77-
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"}}}''',
78-
'de-AT':
79-
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"}}}''',
80-
};
81-
82-
final _nstack = NStack<Localization>(
83-
config: _config,
84-
localization: const Localization(),
85-
availableLanguages: _languages,
86-
bundledTranslations: _bundledTranslations,
87-
pickedLanguageLocale: '',
88-
debug: kDebugMode);
89-
138+
/*
139+
*
140+
* NStack Flutter Widgets
141+
*
142+
*/
90143
class NStackScope extends InheritedWidget {
91-
final NStack<Localization> nstack;
92144
final NStackState state;
93145
final String checksum;
94146

95-
NStackScope(
96-
{Key? key,
97-
required Widget child,
98-
required this.state,
99-
required this.nstack,
100-
required this.checksum})
101-
: super(key: key, child: child);
147+
const NStackScope({
148+
Key? key,
149+
required Widget child,
150+
required this.state,
151+
required this.checksum,
152+
}) : super(key: key, child: child);
102153

103154
static NStackState of(BuildContext context) =>
104155
context.dependOnInheritedWidgetOfExactType<NStackScope>()!.state;
@@ -113,29 +164,32 @@ class NStackWidget extends StatefulWidget {
113164
final AppOpenPlatform? platformOverride;
114165
final VoidCallback? onComplete;
115166

116-
const NStackWidget(
117-
{Key? key, required Widget child, this.platformOverride, this.onComplete})
118-
: child = child,
119-
super(key: key);
167+
const NStackWidget({
168+
Key? key,
169+
required this.child,
170+
this.platformOverride,
171+
this.onComplete,
172+
}) : super(key: key);
120173

121174
@override
122175
NStackState createState() => NStackState();
123176
}
124177

125178
class NStackState extends State<NStackWidget> {
126-
final NStack<Localization> nstack = _nstack;
179+
final NStackSdk<Localization> _nstack = NStack;
127180
bool _initializedNStack = false;
128181

129182
late Future<bool> _nstackInitFuture;
130183

131184
@override
132185
void initState() {
133186
super.initState();
187+
134188
_nstackInitFuture = _nstack.init();
135189
}
136190

137-
changeLanguage(Locale locale) async {
138-
await _nstack
191+
Future<void> changeLanguage(Locale locale) {
192+
return _nstack.localization
139193
.changeLocalization(locale)
140194
.whenComplete(() => setState(() {}));
141195
}
@@ -151,28 +205,46 @@ class NStackState extends State<NStackWidget> {
151205
}
152206

153207
return FutureBuilder(
154-
future: _nstackInitFuture,
155-
builder: (context, snapshot) {
156-
if (snapshot.connectionState == ConnectionState.done) {
157-
return NStackScope(
158-
child: widget.child,
159-
state: this,
160-
nstack: this.nstack,
161-
checksum: nstack.checksum,
162-
);
163-
} else {
164-
return SizedBox();
165-
}
166-
});
208+
future: _nstackInitFuture,
209+
builder: (context, snapshot) {
210+
if (snapshot.connectionState == ConnectionState.done) {
211+
return NStackScope(
212+
state: this,
213+
checksum: _nstack.localization.checksum,
214+
child: widget.child,
215+
);
216+
} else {
217+
return const SizedBox();
218+
}
219+
},
220+
);
167221
}
168222
}
169223

170-
/// Allows to access the Nstack Localization using the BuildContext
224+
/*
225+
*
226+
* NStack Flutter Extensions
227+
*
228+
*/
229+
230+
/// Allows to access the NStack features using the BuildContext
171231
extension NStackWidgetExtension on BuildContext {
172-
Localization get localization => NStackScope.of(this).nstack.localization;
232+
/// NStack SDK of this project.
233+
NStackSdk<Localization> get nstack => NStackScope.of(this)._nstack;
234+
235+
/// Provides the localization for this NStack project.
236+
///
237+
/// Use `NStackScope.of(context).state.changeLanguage` to update language of the app.
238+
Localization get localization => nstack.localization.translations;
173239
}
174240

175-
/// Allows to access the Nstack Localization from StatefulWidget's State
241+
/// Allows to access the NStack features from StatefulWidget's State
176242
extension NStackStateExtension<T extends StatefulWidget> on State<T> {
243+
/// NStack SDK of this project.
244+
NStackSdk<Localization> get nstack => context.nstack;
245+
246+
/// Provides the localization for this NStack project.
247+
///
248+
/// Use `NStackScope.of(context).state.changeLanguage` to update language of the app.
177249
Localization get localization => context.localization;
178250
}

0 commit comments

Comments
 (0)