Skip to content

Commit

Permalink
Tagline: support "min_launches" attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
g123k committed Jan 19, 2025
1 parent 277eea2 commit 43b2015
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
30 changes: 27 additions & 3 deletions packages/smooth_app/lib/data_models/news_feed/newsfeed_json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ class _TagLineJSON {
final _TagLineJSONNewsList news;
final _TaglineJSONFeed taglineFeed;

Future<AppNews> toTagLine(String locale, String appVersion) async {
Future<AppNews> toTagLine(
String locale,
String appVersion,
int appLaunches,
) async {
final Map<String, AppNewsItem> tagLineNews = _getAppNewsItem(
locale,
appVersion,
appLaunches,
);

final _TagLineJSONFeedLocale localizedFeed = taglineFeed.loadNews(locale);
Expand All @@ -33,23 +38,35 @@ class _TagLineJSON {
);
}

Map<String, AppNewsItem> _getAppNewsItem(String locale, String appVersion) {
Map<String, AppNewsItem> _getAppNewsItem(
String locale,
String appVersion,
int appLaunches,
) {
final Map<String, AppNewsItem> tagLineNews = news.map(
(String key, _TagLineItemNewsItem value) => MapEntry<String, AppNewsItem>(
key,
value.toTagLineItem(locale),
),
);
final Iterable<AppNewsItem> values =
List<AppNewsItem>.of(tagLineNews.values);

final int? appVersionNumber = _extractVersionNumber(appVersion);

for (final AppNewsItem item in tagLineNews.values) {
for (final AppNewsItem item in values) {
if (item.minLaunches != null && appLaunches < item.minLaunches!) {
tagLineNews.remove(item.id);
continue;
}

if (item.minAppVersion != null) {
final int? minVersionNumber = _extractVersionNumber(item.minAppVersion);

if (minVersionNumber != null && appVersionNumber != null) {
if (appVersionNumber < minVersionNumber) {
tagLineNews.remove(item.id);
continue;
}
}
}
Expand All @@ -59,6 +76,7 @@ class _TagLineJSON {
if (maxVersionNumber != null && appVersionNumber != null) {
if (appVersionNumber > maxVersionNumber) {
tagLineNews.remove(item.id);
continue;
}
}
}
Expand Down Expand Up @@ -100,6 +118,7 @@ class _TagLineItemNewsItem {
required this.id,
required this.url,
required _TagLineItemNewsTranslations translations,
this.minLaunches,
this.startDate,
this.endDate,
this.minVersion,
Expand All @@ -124,6 +143,7 @@ class _TagLineItemNewsItem {
);
}
}),
minLaunches = json['min_launches'] is int ? json['min_launches'] : null,
startDate = DateTime.tryParse(json['start_date']),
endDate = DateTime.tryParse(json['end_date']),
minVersion = json['min_version'],
Expand All @@ -134,6 +154,7 @@ class _TagLineItemNewsItem {

final String id;
final String url;
final int? minLaunches;
final _TagLineItemNewsTranslations _translations;
final DateTime? startDate;
final DateTime? endDate;
Expand Down Expand Up @@ -169,6 +190,7 @@ class _TagLineItemNewsItem {
message: translation.message!,
url: translation.url ?? url,
buttonLabel: translation.buttonLabel,
minLaunches: minLaunches,
startDate: startDate,
endDate: endDate,
minAppVersion: minVersion,
Expand All @@ -183,6 +205,7 @@ class _TagLineItemNewsItem {
_TagLineItemNewsItem copyWith({
String? url,
_TagLineItemNewsTranslations? translations,
int? minLaunches,
DateTime? startDate,
DateTime? endDate,
String? minVersion,
Expand All @@ -194,6 +217,7 @@ class _TagLineItemNewsItem {
// Still the same
url: url ?? this.url,
translations: translations ?? _translations,
minLaunches: minLaunches ?? this.minLaunches,
startDate: startDate ?? this.startDate,
endDate: endDate ?? this.endDate,
minVersion: minVersion ?? this.minVersion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class AppNewsItem {
required this.message,
required this.url,
this.buttonLabel,
this.minLaunches,
this.startDate,
this.endDate,
this.minAppVersion,
Expand All @@ -50,6 +51,7 @@ class AppNewsItem {
final String message;
final String url;
final String? buttonLabel;
final int? minLaunches;
final DateTime? startDate;
final DateTime? endDate;
final String? minAppVersion;
Expand All @@ -59,7 +61,7 @@ class AppNewsItem {

@override
String toString() {
return 'AppNewsItem{id: $id, title: $title, message: $message, url: $url, buttonLabel: $buttonLabel, startDate: $startDate, endDate: $endDate, minAppVersion: $minAppVersion, maxAppVersion: $maxAppVersion, image: $image, style: $style}';
return 'AppNewsItem{id: $id, title: $title, message: $message, url: $url, buttonLabel: $buttonLabel, minLaunches: $minLaunches, startDate: $startDate, endDate: $endDate, minAppVersion: $minAppVersion, maxAppVersion: $maxAppVersion, image: $image, style: $style}';
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@ class AppNewsProvider extends ChangeNotifier {
}

final PackageInfo app = await PackageInfo.fromPlatform();
final int appLaunches = _preferences.appLaunches;

final AppNews? appNews = await Isolate.run(
() => _parseJSONAndGetLocalizedContent(
jsonString!,
locale,
app.version,
appLaunches,
),
);
if (appNews == null) {
Expand All @@ -97,12 +99,13 @@ class AppNewsProvider extends ChangeNotifier {
String json,
String locale,
String appVersion,
int appLaunches,
) async {
try {
final _TagLineJSON tagLineJSON = _TagLineJSON.fromJson(
jsonDecode(json) as Map<dynamic, dynamic>,
);
return tagLineJSON.toTagLine(locale, appVersion);
return tagLineJSON.toTagLine(locale, appVersion, appLaunches);
} catch (_) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class UserPreferences extends ChangeNotifier {
: _sharedPreferences = sharedPreferences {
onCrashReportingChanged = ValueNotifier<bool>(crashReports);
onAnalyticsChanged = ValueNotifier<bool>(userTracking);
_incrementAppLaunches();
}

/// Singleton
Expand Down Expand Up @@ -68,6 +69,7 @@ class UserPreferences extends ChangeNotifier {
/// The current version of preferences
static const String _TAG_VERSION = 'prefs_version';
static const int _PREFS_CURRENT_VERSION = 3;
static const String _TAG_APP_LAUNCHES = 'appLaunches';
static const String _TAG_PREFIX_IMPORTANCE = 'IMPORTANCE_AS_STRING';
static const String _TAG_CURRENT_THEME_MODE = 'currentThemeMode';
static const String _TAG_CURRENT_COLOR_SCHEME = 'currentColorScheme';
Expand Down Expand Up @@ -150,6 +152,13 @@ class UserPreferences extends ChangeNotifier {
);
}

int get appLaunches => _sharedPreferences.getInt(_TAG_APP_LAUNCHES) ?? 0;

Future<void> _incrementAppLaunches() async {
await _sharedPreferences.setInt(_TAG_APP_LAUNCHES, appLaunches + 1);
// No need to call notifyListeners here
}

String _getImportanceTag(final String variable) =>
_TAG_PREFIX_IMPORTANCE + variable;

Expand Down

0 comments on commit 43b2015

Please sign in to comment.