Skip to content

Commit dbeeb7c

Browse files
authored
Added App open models. Fixed map <-> string type casting error. Impro… (#10)
* Added App open models. Fixed map <-> string type casting error. Improved data type information. * Fixed NstackBuilder to use specific data types instead of dynamic types. Improved formatting. * More type safety. Replaced double quotes with single quotes. Improved formatting. * Refactoring: Improved section generation. Replaced normal constructors with const constructors. Added reserved keywords. Improved repositories. * Moved extensions to the end of the generated file. * Simplified even more the Sections classes getters.
1 parent 06540ce commit dbeeb7c

26 files changed

+928
-339
lines changed

example/lib/nstack.dart

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,67 @@
1-
// Generated by NStack, do not modify this file.
1+
/// Generated by NStack, do not modify this file.
2+
23
import 'package:flutter/widgets.dart';
34
import 'package:nstack/models/language.dart';
45
import 'package:nstack/models/nstack_config.dart';
5-
import 'package:nstack/models/section_key.dart';
66
import 'package:nstack/nstack.dart';
77
import 'package:nstack/partial/section_key_delegate.dart';
88

99
// Update this file by running:
10-
// - `flutter pub pub run build_runner build`, if your package depends on Flutter
10+
// - `flutter pub run build_runner build`, if your package depends on Flutter
1111
// - `pub run build_runner build` otherwise
12-
12+
1313
class Localization {
14-
_DefaultSection _defaultSection = _DefaultSection();
15-
_DefaultSection get defaultSection => _defaultSection;
16-
_Error _error = _Error();
17-
_Error get error => _error;
18-
_Test _test = _Test();
19-
_Test get test => _test;
14+
final defaultSection = const _DefaultSection();
15+
final error = const _Error();
16+
final test = const _Test();
17+
18+
const Localization();
2019
}
2120

2221
class _DefaultSection extends SectionKeyDelegate {
23-
String get hi => get(SectionKey("default", "hi", "Hej"));
24-
String get cancel => get(SectionKey("default", "cancel", "Annuller"));
25-
String get no => get(SectionKey("default", "no", "nej"));
26-
String get yes => get(SectionKey("default", "yes", "Ja"));
27-
String get edit => get(SectionKey("default", "edit", "Redigere"));
28-
String get next => get(SectionKey("default", "next", "Næste"));
29-
String get on => get(SectionKey("default", "on", "Tændt"));
30-
String get off => get(SectionKey("default", "off", "af"));
31-
String get ok => get(SectionKey("default", "ok", "Ok"));
22+
const _DefaultSection(): super('default');
23+
24+
String get hi => get('hi', 'Hej');
25+
String get cancel => get('cancel', 'Annuller');
26+
String get no => get('no', 'nej');
27+
String get yes => get('yes', 'Ja');
28+
String get edit => get('edit', 'Redigere');
29+
String get next => get('next', 'Næste');
30+
String get on => get('on', 'Tændt');
31+
String get off => get('off', 'af');
32+
String get ok => get('ok', 'Ok');
3233
}
3334

3435
class _Error extends SectionKeyDelegate {
35-
String get errorRandom => get(SectionKey("error", "errorRandom", "Helt tilfældig fejl"));
36-
String get errorTitle => get(SectionKey("error", "errorTitle", "Fejl"));
37-
String get authenticationError => get(SectionKey("error", "authenticationError", "Login er udløbet, login venligst ind igen."));
38-
String get connectionError => get(SectionKey("error", "connectionError", "Ingen eller dårlig forbindelse, prøv igen!"));
39-
String get unknownError => get(SectionKey("error", "unknownError", "Ukendt fejl, prøv igen."));
36+
const _Error(): super('error');
37+
38+
String get errorRandom => get('errorRandom', 'Helt tilfældig fejl');
39+
String get errorTitle => get('errorTitle', 'Fejl');
40+
String get authenticationError => get('authenticationError', 'Login er udløbet, login venligst ind igen.');
41+
String get connectionError => get('connectionError', 'Ingen eller dårlig forbindelse, prøv igen!');
42+
String get unknownError => get('unknownError', 'Ukendt fejl, prøv igen.');
4043
}
4144

4245
class _Test extends SectionKeyDelegate {
43-
String get title => get(SectionKey("test", "title", "NStack Demo"));
44-
String get message => get(SectionKey("test", "message", "Bacon ipsum dolor amet magna meatball jerky in, shank sunt do burgdoggen spare ribs. Lorem boudin eiusmod short ribs pastrami. Sausage bresaola do turkey, dolor qui tail ground round culpa boudin nulla minim sunt beef ribs ham. Cillum in pastrami adipisicing swine lorem, velit sunt meatloaf bresaola short loin fugiat tri-tip boudin."));
45-
String get subTitle => get(SectionKey("test", "subTitle", "Subtitle demo"));
46-
String get on => get(SectionKey("test", "on", "on"));
47-
String get off => get(SectionKey("test", "off", "off"));
46+
const _Test(): super('test');
47+
48+
String get title => get('title', 'NStack Demo');
49+
String get message => get('message', 'Bacon ipsum dolor amet magna meatball jerky in, shank sunt do burgdoggen spare ribs. Lorem boudin eiusmod short ribs pastrami. Sausage bresaola do turkey, dolor qui tail ground round culpa boudin nulla minim sunt beef ribs ham. Cillum in pastrami adipisicing swine lorem, velit sunt meatloaf bresaola short loin fugiat tri-tip boudin.');
50+
String get subTitle => get('subTitle', 'Subtitle demo');
51+
String get on => get('on', 'on');
52+
String get off => get('off', 'off');
4853
}
4954

50-
final _config = NStackConfig(projectId: "bOdrNuZd4syxuAz6gyCb3xwBCjA8U4h4IcQI", apiKey: "X0ENl5QpKI51tS9CzKSt1PGwfZeq2gBMTU58");
55+
const _config = NStackConfig(projectId: 'bOdrNuZd4syxuAz6gyCb3xwBCjA8U4h4IcQI', apiKey: 'X0ENl5QpKI51tS9CzKSt1PGwfZeq2gBMTU58');
5156

52-
final _languages = [
53-
Language(id: 6, locale: "da-DK", direction: "LRM", isDefault: true, isBestFit: false),
54-
Language(id: 11, locale: "en-GB", direction: "LRM", isDefault: false, isBestFit: true),
55-
Language(id: 15, locale: "es-MX", direction: "LRM", isDefault: false, isBestFit: false),
56-
Language(id: 20, locale: "fr-FR", direction: "LRM", isDefault: false, isBestFit: false),
57+
const _languages = [
58+
Language(id: 6, locale: 'da-DK', direction: 'LRM', isDefault: true, isBestFit: false),
59+
Language(id: 11, locale: 'en-GB', direction: 'LRM', isDefault: false, isBestFit: true),
60+
Language(id: 15, locale: 'es-MX', direction: 'LRM', isDefault: false, isBestFit: false),
61+
Language(id: 20, locale: 'fr-FR', direction: 'LRM', isDefault: false, isBestFit: false),
5762
];
5863

59-
final _bundledTranslations = {
64+
const _bundledTranslations = {
6065
'da-DK': '{"data":{"default":{"hi":"Hej","cancel":"Annuller","no":"nej","yes":"Ja","edit":"Redigere","next":"N\u00e6ste","on":"T\u00e6ndt","off":"af","ok":"Ok"},"error":{"errorRandom":"Helt tilf\u00e6ldig fejl","errorTitle":"Fejl","authenticationError":"Login er udl\u00f8bet, login venligst ind igen.","connectionError":"Ingen eller d\u00e5rlig forbindelse, pr\u00f8v igen!","unknownError":"Ukendt fejl, pr\u00f8v igen."},"test":{"title":"NStack Demo","message":"Bacon ipsum dolor amet magna meatball jerky in, shank sunt do burgdoggen spare ribs. Lorem boudin eiusmod short ribs pastrami. Sausage bresaola do turkey, dolor qui tail ground round culpa boudin nulla minim sunt beef ribs ham. Cillum in pastrami adipisicing swine lorem, velit sunt meatloaf bresaola short loin fugiat tri-tip boudin.","subTitle":"Subtitle demo","on":"on","off":"off"}},"meta":{"language":{"id":6,"name":"Danish","locale":"da-DK","direction":"LRM","is_default":false,"is_best_fit":false},"platform":{"id":30,"slug":"mobile"}}}',
6166
'en-GB': '{"data":{"default":{"hi":"Hi","cancel":"Cancel","no":"No","yes":"Yes","edit":"Edit","next":"Next","on":"On","off":"Off","ok":"Ok"},"error":{"errorRandom":"Totally random error","errorTitle":"Error","authenticationError":"Login expired, please login again.","connectionError":"No or bad connection, please try again.","unknownError":"Unknown error, please try again."},"test":{"title":"NStack Demo","message":"Bacon ipsum dolor amet magna meatball jerky in, shank sunt do burgdoggen spare ribs. Lorem boudin eiusmod short ribs pastrami. Sausage bresaola do turkey, dolor qui tail ground round culpa boudin nulla minim sunt beef ribs ham. Cillum in pastrami adipisicing swine lorem, velit sunt meatloaf bresaola short loin fugiat tri-tip boudin.","subTitle":"Subtitle demo","on":"on","off":"off"}},"meta":{"language":{"id":11,"name":"English (UK)","locale":"en-GB","direction":"LRM","is_default":false,"is_best_fit":false},"platform":{"id":30,"slug":"mobile"}}}',
6267
'es-MX': '{"data":{"default":{"hi":"Hola","cancel":"Cancelar","no":"no","yes":"Si","edit":"Editar","next":"Siguiente","on":"Apprendido","off":"Apagado","ok":"__ok"},"error":{"errorRandom":"__errorRandom","errorTitle":"__errorTitle","authenticationError":"__authenticationError","connectionError":"__connectionError","unknownError":"__unknownError"},"test":{"title":"__title","message":"__message","subTitle":"__subTitle","on":"__on","off":"__off"}},"meta":{"language":{"id":15,"name":"Spanish (Mexico)","locale":"es-MX","direction":"LRM","is_default":false,"is_best_fit":false},"platform":{"id":30,"slug":"mobile"}}}',
@@ -65,9 +70,9 @@ final _bundledTranslations = {
6570

6671
final _nstack = NStack<Localization>(
6772
config: _config,
68-
localization: Localization(),
73+
localization: const Localization(),
6974
availableLanguages: _languages,
70-
bundledTranslations: _bundledTranslations
75+
bundledTranslations: _bundledTranslations,
7176
);
7277

7378
class NStackWidget extends InheritedWidget {
@@ -85,10 +90,6 @@ class NStackWidget extends InheritedWidget {
8590
nstack != oldWidget.nstack;
8691
}
8792

88-
extension NStackWidgetExtension on BuildContext {
89-
Localization get localization => NStackWidget.of(this).localization;
90-
}
91-
9293
class NStackInitWidget extends StatefulWidget {
9394
final Widget child;
9495

@@ -119,3 +120,13 @@ class _NStackInitState extends State<NStackInitWidget> {
119120
}
120121
}
121122

123+
/// Allows to access the Nstack Localization using the BuildContext
124+
extension NStackWidgetExtension on BuildContext {
125+
Localization get localization => NStackWidget.of(this).localization;
126+
}
127+
128+
/// Allows to access the Nstack Localization from StatefulWidget's State
129+
extension NStackStateExtension<T extends StatefulWidget> on State<T> {
130+
Localization get localization => context.localization;
131+
}
132+

example/lib/nstack.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"version": 1,
23
"nstack_project_id": "bOdrNuZd4syxuAz6gyCb3xwBCjA8U4h4IcQI",
34
"nstack_api_key": "X0ENl5QpKI51tS9CzKSt1PGwfZeq2gBMTU58"
45
}

lib/models/app_open.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import 'package:meta/meta.dart';
2+
import 'package:nstack/models/app_open_data.dart';
3+
import 'package:nstack/models/app_open_meta.dart';
4+
5+
class AppOpen {
6+
final AppOpenData data;
7+
final AppOpenMeta meta;
8+
9+
AppOpen({
10+
@required this.data,
11+
@required this.meta,
12+
});
13+
14+
factory AppOpen.fromJson(Map json) {
15+
return AppOpen(
16+
data: AppOpenData.fromJson(json['data']),
17+
meta: AppOpenMeta.fromJson(json['meta']),
18+
);
19+
}
20+
}

lib/models/app_open_data.dart

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import 'package:meta/meta.dart';
2+
import 'package:nstack/models/app_update.dart';
3+
import 'package:nstack/models/localize_index.dart';
4+
import 'package:nstack/models/message.dart';
5+
import 'package:nstack/models/rate_reminder.dart';
6+
import 'package:nstack/models/terms.dart';
7+
import 'package:nstack/other/extensions.dart';
8+
9+
class AppOpenData {
10+
final int count;
11+
final AppUpdate update;
12+
final List<LocalizeIndex> localize;
13+
final String platform;
14+
final DateTime createdAt;
15+
final DateTime updatedAt;
16+
final Message message;
17+
final RateReminder rateReminder;
18+
final List<Terms> terms;
19+
20+
AppOpenData({
21+
@required this.count,
22+
@required this.update,
23+
@required this.localize,
24+
@required this.platform,
25+
@required this.createdAt,
26+
@required this.updatedAt,
27+
@required this.message,
28+
@required this.rateReminder,
29+
@required this.terms,
30+
});
31+
32+
factory AppOpenData.fromJson(Map json) {
33+
return AppOpenData(
34+
count: json['count'],
35+
update: AppUpdate.fromJson(json['update']),
36+
localize: (json['localize'] as List)?.let(
37+
(it) => it.map((e) => LocalizeIndex.fromJson(e)).toList(),
38+
),
39+
platform: json['platform'],
40+
createdAt: (json['created_at'] as String)?.let(
41+
(it) => DateTime.parse(it),
42+
),
43+
updatedAt: (json['last_updated'] as String)?.let(
44+
(it) => DateTime.parse(it),
45+
),
46+
message: (json['message'] as Map)?.let((it) => Message.fromJson(it)),
47+
rateReminder: json['rateReminder']?.let(
48+
(it) => RateReminder.fromJson(it),
49+
),
50+
terms: json['terms']?.let((it) => it),
51+
);
52+
}
53+
}

lib/models/app_open_meta.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import 'package:meta/meta.dart';
2+
3+
class AppOpenMeta {
4+
final String acceptLanguage;
5+
6+
AppOpenMeta({@required this.acceptLanguage});
7+
8+
factory AppOpenMeta.fromJson(Map json) {
9+
return AppOpenMeta(
10+
acceptLanguage: json['accept_Language'],
11+
);
12+
}
13+
}

lib/models/app_update.dart

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import 'package:meta/meta.dart';
2+
import 'package:nstack/models/app_update_state.dart';
3+
import 'package:nstack/models/update_info.dart';
4+
import 'package:nstack/other/extensions.dart';
5+
6+
class AppUpdate {
7+
final UpdateInfo newerVersion;
8+
final UpdateInfo newInVersion;
9+
10+
AppUpdate({
11+
@required this.newerVersion,
12+
@required this.newInVersion,
13+
});
14+
15+
factory AppUpdate.fromJson(Map json) {
16+
return AppUpdate(
17+
newerVersion: (json['newer_version'] as Map)?.let(
18+
(it) => UpdateInfo.fromJson(it),
19+
),
20+
newInVersion: (json['new_in_version'] as Map)?.let(
21+
(it) => UpdateInfo.fromJson(it),
22+
),
23+
);
24+
}
25+
26+
UpdateInfo get update {
27+
return newerVersion ?? newInVersion;
28+
}
29+
30+
AppUpdateState get state {
31+
if (update?.state == "yes") {
32+
return AppUpdateState.update;
33+
}
34+
if (update?.state == "force") {
35+
return AppUpdateState.force;
36+
}
37+
if (newInVersion != null) {
38+
return AppUpdateState.changelog;
39+
}
40+
return AppUpdateState.none;
41+
}
42+
}

lib/models/app_update_state.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
enum AppUpdateState {
2+
none,
3+
update,
4+
force,
5+
changelog,
6+
}

lib/models/language.dart

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
class Language {
2-
int id;
3-
String name;
4-
String locale;
5-
String direction;
6-
bool isDefault;
7-
bool isBestFit;
2+
final int id;
3+
final String name;
4+
final String locale;
5+
final String direction;
6+
final bool isDefault;
7+
final bool isBestFit;
88

9-
Language({this.id, this.name, this.locale, this.direction, this.isDefault, this.isBestFit});
9+
const Language({
10+
this.id,
11+
this.name,
12+
this.locale,
13+
this.direction,
14+
this.isDefault,
15+
this.isBestFit,
16+
});
1017

11-
Language.fromJson(Map<String, dynamic> json) {
12-
id = json['id'];
13-
name = json['name'];
14-
locale = json['locale'];
15-
direction = json['direction'];
16-
isDefault = json['is_default'];
17-
isBestFit = json['is_best_fit'];
18-
}
19-
}
18+
factory Language.fromJson(Map<String, dynamic> json) {
19+
return Language(
20+
id: json['id'],
21+
name: json['name'],
22+
locale: json['locale'],
23+
direction: json['direction'],
24+
isDefault: json['is_default'],
25+
isBestFit: json['is_best_fit'],
26+
);
27+
}
28+
}

lib/models/language_response.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class LocalizationData {
2+
final Map<String, dynamic> data;
3+
4+
LocalizationData(this.data);
5+
6+
factory LocalizationData.fromJson(Map json) {
7+
return LocalizationData(json['data']);
8+
}
9+
}

lib/models/localize_index.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import 'package:nstack/models/language.dart';
2+
import 'package:nstack/other/extensions.dart';
3+
4+
class LocalizeIndex {
5+
final int id;
6+
final String url;
7+
final DateTime lastUpdatedAt;
8+
final bool shouldUpdate;
9+
final Language language;
10+
11+
LocalizeIndex({
12+
this.id,
13+
this.url,
14+
this.lastUpdatedAt,
15+
this.shouldUpdate,
16+
this.language,
17+
});
18+
19+
factory LocalizeIndex.fromJson(Map json) {
20+
return LocalizeIndex(
21+
id: json['id'],
22+
url: json['url'],
23+
lastUpdatedAt:
24+
(json['last_updated_at'] as String)?.let((it) => DateTime.parse(it)),
25+
shouldUpdate: json['should_update'] ?? false,
26+
language: (json['language'] as Map)?.let((it) => Language.fromJson(it)),
27+
);
28+
}
29+
30+
@override
31+
String toString() {
32+
return 'LocalizeIndex(id: $id, url $url, lastUpdatedAt: $lastUpdatedAt, shouldUpdate: $shouldUpdate, language: $language)';
33+
}
34+
}

0 commit comments

Comments
 (0)