Skip to content

Commit

Permalink
Added App open models. Fixed map <-> string type casting error. Impro… (
Browse files Browse the repository at this point in the history
#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.
  • Loading branch information
vovahost authored Jun 6, 2020
1 parent 06540ce commit dbeeb7c
Show file tree
Hide file tree
Showing 26 changed files with 928 additions and 339 deletions.
95 changes: 53 additions & 42 deletions example/lib/nstack.dart
Original file line number Diff line number Diff line change
@@ -1,62 +1,67 @@
// Generated by NStack, do not modify this file.
/// Generated by NStack, do not modify this file.
import 'package:flutter/widgets.dart';
import 'package:nstack/models/language.dart';
import 'package:nstack/models/nstack_config.dart';
import 'package:nstack/models/section_key.dart';
import 'package:nstack/nstack.dart';
import 'package:nstack/partial/section_key_delegate.dart';

// Update this file by running:
// - `flutter pub pub run build_runner build`, if your package depends on Flutter
// - `flutter pub run build_runner build`, if your package depends on Flutter
// - `pub run build_runner build` otherwise

class Localization {
_DefaultSection _defaultSection = _DefaultSection();
_DefaultSection get defaultSection => _defaultSection;
_Error _error = _Error();
_Error get error => _error;
_Test _test = _Test();
_Test get test => _test;
final defaultSection = const _DefaultSection();
final error = const _Error();
final test = const _Test();

const Localization();
}

class _DefaultSection extends SectionKeyDelegate {
String get hi => get(SectionKey("default", "hi", "Hej"));
String get cancel => get(SectionKey("default", "cancel", "Annuller"));
String get no => get(SectionKey("default", "no", "nej"));
String get yes => get(SectionKey("default", "yes", "Ja"));
String get edit => get(SectionKey("default", "edit", "Redigere"));
String get next => get(SectionKey("default", "next", "Næste"));
String get on => get(SectionKey("default", "on", "Tændt"));
String get off => get(SectionKey("default", "off", "af"));
String get ok => get(SectionKey("default", "ok", "Ok"));
const _DefaultSection(): super('default');

String get hi => get('hi', 'Hej');
String get cancel => get('cancel', 'Annuller');
String get no => get('no', 'nej');
String get yes => get('yes', 'Ja');
String get edit => get('edit', 'Redigere');
String get next => get('next', 'Næste');
String get on => get('on', 'Tændt');
String get off => get('off', 'af');
String get ok => get('ok', 'Ok');
}

class _Error extends SectionKeyDelegate {
String get errorRandom => get(SectionKey("error", "errorRandom", "Helt tilfældig fejl"));
String get errorTitle => get(SectionKey("error", "errorTitle", "Fejl"));
String get authenticationError => get(SectionKey("error", "authenticationError", "Login er udløbet, login venligst ind igen."));
String get connectionError => get(SectionKey("error", "connectionError", "Ingen eller dårlig forbindelse, prøv igen!"));
String get unknownError => get(SectionKey("error", "unknownError", "Ukendt fejl, prøv igen."));
const _Error(): super('error');

String get errorRandom => get('errorRandom', 'Helt tilfældig fejl');
String get errorTitle => get('errorTitle', 'Fejl');
String get authenticationError => get('authenticationError', 'Login er udløbet, login venligst ind igen.');
String get connectionError => get('connectionError', 'Ingen eller dårlig forbindelse, prøv igen!');
String get unknownError => get('unknownError', 'Ukendt fejl, prøv igen.');
}

class _Test extends SectionKeyDelegate {
String get title => get(SectionKey("test", "title", "NStack Demo"));
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."));
String get subTitle => get(SectionKey("test", "subTitle", "Subtitle demo"));
String get on => get(SectionKey("test", "on", "on"));
String get off => get(SectionKey("test", "off", "off"));
const _Test(): super('test');

String get title => get('title', 'NStack Demo');
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.');
String get subTitle => get('subTitle', 'Subtitle demo');
String get on => get('on', 'on');
String get off => get('off', 'off');
}

final _config = NStackConfig(projectId: "bOdrNuZd4syxuAz6gyCb3xwBCjA8U4h4IcQI", apiKey: "X0ENl5QpKI51tS9CzKSt1PGwfZeq2gBMTU58");
const _config = NStackConfig(projectId: 'bOdrNuZd4syxuAz6gyCb3xwBCjA8U4h4IcQI', apiKey: 'X0ENl5QpKI51tS9CzKSt1PGwfZeq2gBMTU58');

final _languages = [
Language(id: 6, locale: "da-DK", direction: "LRM", isDefault: true, isBestFit: false),
Language(id: 11, locale: "en-GB", direction: "LRM", isDefault: false, isBestFit: true),
Language(id: 15, locale: "es-MX", direction: "LRM", isDefault: false, isBestFit: false),
Language(id: 20, locale: "fr-FR", direction: "LRM", isDefault: false, isBestFit: false),
const _languages = [
Language(id: 6, locale: 'da-DK', direction: 'LRM', isDefault: true, isBestFit: false),
Language(id: 11, locale: 'en-GB', direction: 'LRM', isDefault: false, isBestFit: true),
Language(id: 15, locale: 'es-MX', direction: 'LRM', isDefault: false, isBestFit: false),
Language(id: 20, locale: 'fr-FR', direction: 'LRM', isDefault: false, isBestFit: false),
];

final _bundledTranslations = {
const _bundledTranslations = {
'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"}}}',
'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"}}}',
'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"}}}',
Expand All @@ -65,9 +70,9 @@ final _bundledTranslations = {

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

class NStackWidget extends InheritedWidget {
Expand All @@ -85,10 +90,6 @@ class NStackWidget extends InheritedWidget {
nstack != oldWidget.nstack;
}

extension NStackWidgetExtension on BuildContext {
Localization get localization => NStackWidget.of(this).localization;
}

class NStackInitWidget extends StatefulWidget {
final Widget child;

Expand Down Expand Up @@ -119,3 +120,13 @@ class _NStackInitState extends State<NStackInitWidget> {
}
}

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

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

1 change: 1 addition & 0 deletions example/lib/nstack.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"version": 1,
"nstack_project_id": "bOdrNuZd4syxuAz6gyCb3xwBCjA8U4h4IcQI",
"nstack_api_key": "X0ENl5QpKI51tS9CzKSt1PGwfZeq2gBMTU58"
}
20 changes: 20 additions & 0 deletions lib/models/app_open.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:meta/meta.dart';
import 'package:nstack/models/app_open_data.dart';
import 'package:nstack/models/app_open_meta.dart';

class AppOpen {
final AppOpenData data;
final AppOpenMeta meta;

AppOpen({
@required this.data,
@required this.meta,
});

factory AppOpen.fromJson(Map json) {
return AppOpen(
data: AppOpenData.fromJson(json['data']),
meta: AppOpenMeta.fromJson(json['meta']),
);
}
}
53 changes: 53 additions & 0 deletions lib/models/app_open_data.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import 'package:meta/meta.dart';
import 'package:nstack/models/app_update.dart';
import 'package:nstack/models/localize_index.dart';
import 'package:nstack/models/message.dart';
import 'package:nstack/models/rate_reminder.dart';
import 'package:nstack/models/terms.dart';
import 'package:nstack/other/extensions.dart';

class AppOpenData {
final int count;
final AppUpdate update;
final List<LocalizeIndex> localize;
final String platform;
final DateTime createdAt;
final DateTime updatedAt;
final Message message;
final RateReminder rateReminder;
final List<Terms> terms;

AppOpenData({
@required this.count,
@required this.update,
@required this.localize,
@required this.platform,
@required this.createdAt,
@required this.updatedAt,
@required this.message,
@required this.rateReminder,
@required this.terms,
});

factory AppOpenData.fromJson(Map json) {
return AppOpenData(
count: json['count'],
update: AppUpdate.fromJson(json['update']),
localize: (json['localize'] as List)?.let(
(it) => it.map((e) => LocalizeIndex.fromJson(e)).toList(),
),
platform: json['platform'],
createdAt: (json['created_at'] as String)?.let(
(it) => DateTime.parse(it),
),
updatedAt: (json['last_updated'] as String)?.let(
(it) => DateTime.parse(it),
),
message: (json['message'] as Map)?.let((it) => Message.fromJson(it)),
rateReminder: json['rateReminder']?.let(
(it) => RateReminder.fromJson(it),
),
terms: json['terms']?.let((it) => it),
);
}
}
13 changes: 13 additions & 0 deletions lib/models/app_open_meta.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:meta/meta.dart';

class AppOpenMeta {
final String acceptLanguage;

AppOpenMeta({@required this.acceptLanguage});

factory AppOpenMeta.fromJson(Map json) {
return AppOpenMeta(
acceptLanguage: json['accept_Language'],
);
}
}
42 changes: 42 additions & 0 deletions lib/models/app_update.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:meta/meta.dart';
import 'package:nstack/models/app_update_state.dart';
import 'package:nstack/models/update_info.dart';
import 'package:nstack/other/extensions.dart';

class AppUpdate {
final UpdateInfo newerVersion;
final UpdateInfo newInVersion;

AppUpdate({
@required this.newerVersion,
@required this.newInVersion,
});

factory AppUpdate.fromJson(Map json) {
return AppUpdate(
newerVersion: (json['newer_version'] as Map)?.let(
(it) => UpdateInfo.fromJson(it),
),
newInVersion: (json['new_in_version'] as Map)?.let(
(it) => UpdateInfo.fromJson(it),
),
);
}

UpdateInfo get update {
return newerVersion ?? newInVersion;
}

AppUpdateState get state {
if (update?.state == "yes") {
return AppUpdateState.update;
}
if (update?.state == "force") {
return AppUpdateState.force;
}
if (newInVersion != null) {
return AppUpdateState.changelog;
}
return AppUpdateState.none;
}
}
6 changes: 6 additions & 0 deletions lib/models/app_update_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
enum AppUpdateState {
none,
update,
force,
changelog,
}
41 changes: 25 additions & 16 deletions lib/models/language.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
class Language {
int id;
String name;
String locale;
String direction;
bool isDefault;
bool isBestFit;
final int id;
final String name;
final String locale;
final String direction;
final bool isDefault;
final bool isBestFit;

Language({this.id, this.name, this.locale, this.direction, this.isDefault, this.isBestFit});
const Language({
this.id,
this.name,
this.locale,
this.direction,
this.isDefault,
this.isBestFit,
});

Language.fromJson(Map<String, dynamic> json) {
id = json['id'];
name = json['name'];
locale = json['locale'];
direction = json['direction'];
isDefault = json['is_default'];
isBestFit = json['is_best_fit'];
}
}
factory Language.fromJson(Map<String, dynamic> json) {
return Language(
id: json['id'],
name: json['name'],
locale: json['locale'],
direction: json['direction'],
isDefault: json['is_default'],
isBestFit: json['is_best_fit'],
);
}
}
9 changes: 9 additions & 0 deletions lib/models/language_response.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class LocalizationData {
final Map<String, dynamic> data;

LocalizationData(this.data);

factory LocalizationData.fromJson(Map json) {
return LocalizationData(json['data']);
}
}
34 changes: 34 additions & 0 deletions lib/models/localize_index.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'package:nstack/models/language.dart';
import 'package:nstack/other/extensions.dart';

class LocalizeIndex {
final int id;
final String url;
final DateTime lastUpdatedAt;
final bool shouldUpdate;
final Language language;

LocalizeIndex({
this.id,
this.url,
this.lastUpdatedAt,
this.shouldUpdate,
this.language,
});

factory LocalizeIndex.fromJson(Map json) {
return LocalizeIndex(
id: json['id'],
url: json['url'],
lastUpdatedAt:
(json['last_updated_at'] as String)?.let((it) => DateTime.parse(it)),
shouldUpdate: json['should_update'] ?? false,
language: (json['language'] as Map)?.let((it) => Language.fromJson(it)),
);
}

@override
String toString() {
return 'LocalizeIndex(id: $id, url $url, lastUpdatedAt: $lastUpdatedAt, shouldUpdate: $shouldUpdate, language: $language)';
}
}
Loading

0 comments on commit dbeeb7c

Please sign in to comment.