Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Min/max version for the tagline #5917

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 66 additions & 9 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,15 +15,59 @@ class _TagLineJSON {
final _TagLineJSONNewsList news;
final _TaglineJSONFeed taglineFeed;

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

final _TagLineJSONFeedLocale localizedFeed = taglineFeed.loadNews(locale);
final Iterable<AppNewsFeedItem> feed =
_getAppNewsFeedItems(localizedFeed, locale);

return AppNews(
news: AppNewsList(tagLineNews),
feed: AppNewsFeed(
feed.toList(growable: false),
),
);
}

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

final _TagLineJSONFeedLocale localizedFeed = taglineFeed.loadNews(locale);
final int? appVersionNumber = _extractVersionNumber(appVersion);

for (final AppNewsItem item in tagLineNews.values) {
if (item.minAppVersion != null) {
final int? minVersionNumber = _extractVersionNumber(item.minAppVersion);

if (minVersionNumber != null && appVersionNumber != null) {
if (appVersionNumber < minVersionNumber) {
tagLineNews.remove(item.id);
}
}
}
if (item.maxAppVersion != null) {
final int? maxVersionNumber = _extractVersionNumber(item.maxAppVersion);

if (maxVersionNumber != null && appVersionNumber != null) {
if (appVersionNumber > maxVersionNumber) {
tagLineNews.remove(item.id);
}
}
}
}
return tagLineNews;
}

Iterable<AppNewsFeedItem> _getAppNewsFeedItems(
_TagLineJSONFeedLocale localizedFeed, String locale) {
final Iterable<AppNewsFeedItem> feed = localizedFeed.news
.map((_TagLineJSONFeedLocaleItem item) {
if (news[item.id] == null) {
Expand All @@ -38,13 +82,14 @@ class _TagLineJSON {
item.startDate!.isBefore(DateTime.now())) &&
(item.endDate == null || item.endDate!.isAfter(DateTime.now())))
.whereNotNull();
return feed;
}

return AppNews(
news: AppNewsList(tagLineNews),
feed: AppNewsFeed(
feed.toList(growable: false),
),
);
int? _extractVersionNumber(String? version) {
if (version == null) {
return null;
}
return int.tryParse(version.replaceAll(r'.', '').trim());
}
}

Expand All @@ -57,6 +102,8 @@ class _TagLineItemNewsItem {
required _TagLineItemNewsTranslations translations,
this.startDate,
this.endDate,
this.minVersion,
this.maxVersion,
this.style,
}) : _translations = translations;

Expand All @@ -79,6 +126,8 @@ class _TagLineItemNewsItem {
}),
startDate = DateTime.tryParse(json['start_date']),
endDate = DateTime.tryParse(json['end_date']),
minVersion = json['min_version'],
maxVersion = json['max_version'],
style = json['style'] == null
? null
: _TagLineNewsStyle.fromJson(json['style']);
Expand All @@ -88,6 +137,8 @@ class _TagLineItemNewsItem {
final _TagLineItemNewsTranslations _translations;
final DateTime? startDate;
final DateTime? endDate;
final String? minVersion;
final String? maxVersion;
final _TagLineNewsStyle? style;

_TagLineItemNewsTranslation loadTranslation(String locale) {
Expand Down Expand Up @@ -120,6 +171,8 @@ class _TagLineItemNewsItem {
buttonLabel: translation.buttonLabel,
startDate: startDate,
endDate: endDate,
minAppVersion: minVersion,
maxAppVersion: maxVersion,
style: style?.toTagLineStyle(),
image: translation.image?.overridesContent == true
? translation.image?.toTagLineImage()
Expand All @@ -132,6 +185,8 @@ class _TagLineItemNewsItem {
_TagLineItemNewsTranslations? translations,
DateTime? startDate,
DateTime? endDate,
String? minVersion,
String? maxVersion,
_TagLineNewsStyle? style,
}) {
return _TagLineItemNewsItem._(
Expand All @@ -141,6 +196,8 @@ class _TagLineItemNewsItem {
translations: translations ?? _translations,
startDate: startDate ?? this.startDate,
endDate: endDate ?? this.endDate,
minVersion: minVersion ?? this.minVersion,
maxVersion: maxVersion ?? this.maxVersion,
style: style ?? this.style,
);
}
Expand Down Expand Up @@ -312,7 +369,7 @@ class _TagLineNewsStyle {
);
}

AppNewsStyle toTagLineStyle() => AppNewsStyle.fromHexa(
AppNewsStyle toTagLineStyle() => AppNewsStyle.fromHex(
titleBackground: titleBackground,
titleTextColor: titleTextColor,
titleIndicatorColor: titleIndicatorColor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class AppNewsItem {
this.buttonLabel,
this.startDate,
this.endDate,
this.minAppVersion,
this.maxAppVersion,
this.image,
this.style,
});
Expand All @@ -50,12 +52,14 @@ class AppNewsItem {
final String? buttonLabel;
final DateTime? startDate;
final DateTime? endDate;
final String? minAppVersion;
final String? maxAppVersion;
final AppNewsImage? image;
final AppNewsStyle? style;

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

Expand All @@ -71,7 +75,7 @@ class AppNewsStyle {
this.contentBackgroundColor,
});

AppNewsStyle.fromHexa({
AppNewsStyle.fromHex({
String? titleBackground,
String? titleTextColor,
String? titleIndicatorColor,
Expand All @@ -98,11 +102,11 @@ class AppNewsStyle {
final Color? buttonTextColor;
final Color? contentBackgroundColor;

static Color? _parseColor(String? hexa) {
if (hexa == null || hexa.length != 7) {
static Color? _parseColor(String? hex) {
if (hex == null || hex.length != 7) {
return null;
}
return Color(int.parse(hexa.substring(1), radix: 16));
return Color(int.parse(hex.substring(1), radix: 16));
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'dart:isolate';
import 'package:collection/collection.dart';
import 'package:flutter/widgets.dart';
import 'package:http/http.dart' as http;
import 'package:package_info_plus/package_info_plus.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:smooth_app/data_models/news_feed/newsfeed_model.dart';
Expand Down Expand Up @@ -66,8 +67,15 @@ class AppNewsProvider extends ChangeNotifier {
return;
}

final PackageInfo app = await PackageInfo.fromPlatform();

final AppNews? appNews = await Isolate.run(
() => _parseJSONAndGetLocalizedContent(jsonString!, locale));
() => _parseJSONAndGetLocalizedContent(
jsonString!,
locale,
app.version,
),
);
if (appNews == null) {
_emit(const AppNewsStateError('Unable to parse the JSON news file'));
Logs.e('Unable to parse the JSON news file');
Expand All @@ -79,21 +87,23 @@ class AppNewsProvider extends ChangeNotifier {

void _emit(AppNewsState state) {
_state = state;
WidgetsBinding.instance.addPostFrameCallback((_) {
notifyListeners();
});
WidgetsBinding.instance
..addPostFrameCallback((_) => notifyListeners())
..ensureVisualUpdate();
}

AppNewsState get state => _state;

static Future<AppNews?> _parseJSONAndGetLocalizedContent(
String json,
String locale,
String appVersion,
) async {
try {
final _TagLineJSON tagLineJSON =
_TagLineJSON.fromJson(jsonDecode(json) as Map<dynamic, dynamic>);
return tagLineJSON.toTagLine(locale);
final _TagLineJSON tagLineJSON = _TagLineJSON.fromJson(
jsonDecode(json) as Map<dynamic, dynamic>,
);
return tagLineJSON.toTagLine(locale, appVersion);
} catch (_) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ class UserPreferencesDevMode extends AbstractUserPreferences {
return Consumer<AppNewsProvider>(
builder: (_, AppNewsProvider provider, __) {
return Text(switch (provider.state) {
AppNewsStateLoading() => 'Loading...',
AppNewsStateLoading() => 'Loading',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we extract this ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's in the Dev mode, but yes, I can translate it

AppNewsStateLoaded(lastUpdate: final DateTime date) =>
appLocalizations
.dev_preferences_news_provider_status_subtitle(
Expand Down
Loading