Skip to content

Commit 7231243

Browse files
committed
feat: expose onComplete in NStackOpenWidget and cleaner implementation of rebuild triggers
1 parent 7a72938 commit 7231243

File tree

4 files changed

+40
-25
lines changed

4 files changed

+40
-25
lines changed

example/lib/main.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ class MyApp extends StatelessWidget {
1717
class MainScreen extends StatelessWidget {
1818
@override
1919
Widget build(BuildContext context) {
20-
// App open!
21-
//NStackScope.of(context).nstack.appOpen(Locale("de-AT"));
22-
2320
return NStackAppOpen(
2421
child: Scaffold(
2522
appBar: AppBar(

example/lib/nstack.dart

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class _DefaultSection extends SectionKeyDelegate {
2929
class _Test extends SectionKeyDelegate {
3030
const _Test(): super('test');
3131

32-
String get testDollarSign => get('testDollarSign', "\$testing");
32+
String get testDollarSign => get('testDollarSign', "\$testing change back");
3333
String get testSingleQuotationMark => get('testSingleQuotationMark', "\'testing\'");
3434
String get testDoubleQuotationMark => get('testDoubleQuotationMark', "\"testing\"");
3535
String get testMultipleLines => get('testMultipleLines', "testing\nmultiple\nlines");
@@ -43,7 +43,7 @@ final _languages = [
4343
];
4444

4545
const _bundledTranslations = {
46-
'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"}}}''',
46+
'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"}}}''',
4747
'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"}}}''',
4848
};
4949

@@ -87,33 +87,44 @@ class NStackState extends State<NStackWidget> {
8787
final NStack<Localization> nstack = _nstack;
8888

8989
changeLanguage(Locale locale) async {
90-
await _nstack.changeLocalization(locale);
91-
setState(() {});
90+
await _nstack.changeLocalization(locale).whenComplete(() => setState(() {}));
9291
}
9392

93+
Future<void> appOpen(Locale locale) async {
94+
await _nstack.appOpen(locale).whenComplete(() => setState(() {}));
95+
}
96+
9497
@override
9598
Widget build(BuildContext context) {
9699
return NStackScope(child: widget.child, state: this, nstack: this.nstack, checksum: nstack.checksum,);
97100
}
98101
}
99102

100103
class NStackAppOpen extends StatefulWidget {
101-
const NStackAppOpen({Key? key, required this.child}) : super(key: key);
104+
const NStackAppOpen({
105+
Key? key,
106+
required this.child,
107+
this.onComplete,
108+
}) : super(key: key);
102109

103110
final Widget child;
111+
final VoidCallback? onComplete;
104112

105113
@override
106114
_NStackAppOpenState createState() => _NStackAppOpenState();
107115
}
108116

109117
class _NStackAppOpenState extends State<NStackAppOpen> {
110-
bool _initializedNStack = false;
118+
bool _initializedNStack = false;
119+
111120
@override
112121
Widget build(BuildContext context) {
113-
if(!_initializedNStack) {
114-
NStackScope.of(context).nstack.appOpen(Localizations.localeOf(context));
115-
_initializedNStack = true;
116-
}
122+
if (!_initializedNStack) {
123+
NStackScope.of(context)
124+
.appOpen(Localizations.localeOf(context))
125+
.whenComplete(() => widget.onComplete?.call());
126+
_initializedNStack = true;
127+
}
117128
return widget.child;
118129
}
119130
}

lib/nstack.dart

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ class NStack<T> {
3737

3838
String get checksum => LocalizationRepository().checksum;
3939

40-
Function? appOpenCompletedCallback;
41-
4240
var _appOpenCalled = false;
4341

4442
NStack(
@@ -250,12 +248,10 @@ class NStack<T> {
250248

251249
_log('NStack --> Updated localization.');
252250
_appOpenCalled = true;
253-
appOpenCompletedCallback?.call();
254251
return AppOpenResult.success;
255252
} catch (e, s) {
256253
_appOpenCalled = true;
257254
LocalizationRepository().switchBundledLocalization(locale.toLanguageTag());
258-
appOpenCompletedCallback?.call();
259255
_log('NStack --> App Open failed because of: ${e.toString()}');
260256
_log(s.toString());
261257
return AppOpenResult.failed;

lib/src/nstack_builder.dart

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -259,33 +259,44 @@ class NStackState extends State<NStackWidget> {
259259
final NStack<Localization> nstack = _nstack;
260260
261261
changeLanguage(Locale locale) async {
262-
await _nstack.changeLocalization(locale);
263-
setState(() {});
262+
await _nstack.changeLocalization(locale).whenComplete(() => setState(() {}));
264263
}
265264
265+
Future<void> appOpen(Locale locale) async {
266+
await _nstack.appOpen(locale).whenComplete(() => setState(() {}));
267+
}
268+
266269
@override
267270
Widget build(BuildContext context) {
268271
return NStackScope(child: widget.child, state: this, nstack: this.nstack, checksum: nstack.checksum,);
269272
}
270273
}
271274
272275
class NStackAppOpen extends StatefulWidget {
273-
const NStackAppOpen({Key? key, required this.child}) : super(key: key);
276+
const NStackAppOpen({
277+
Key? key,
278+
required this.child,
279+
this.onComplete,
280+
}) : super(key: key);
274281
275282
final Widget child;
283+
final VoidCallback? onComplete;
276284
277285
@override
278286
_NStackAppOpenState createState() => _NStackAppOpenState();
279287
}
280288
281289
class _NStackAppOpenState extends State<NStackAppOpen> {
282-
bool _initializedNStack = false;
290+
bool _initializedNStack = false;
291+
283292
@override
284293
Widget build(BuildContext context) {
285-
if(!_initializedNStack) {
286-
NStackScope.of(context).nstack.appOpen(Localizations.localeOf(context));
287-
_initializedNStack = true;
288-
}
294+
if (!_initializedNStack) {
295+
NStackScope.of(context)
296+
.appOpen(Localizations.localeOf(context))
297+
.whenComplete(() => widget.onComplete?.call());
298+
_initializedNStack = true;
299+
}
289300
return widget.child;
290301
}
291302
}

0 commit comments

Comments
 (0)