loadLatestNews method
Future<void>
loadLatestNews({ - bool forceUpdate = false,
})
Implementation
Future<void> loadLatestNews({bool forceUpdate = false}) async {
_emit(const AppNewsStateLoading());
final String locale = ProductQuery.getLocaleString();
if (locale.startsWith('-')) {
// ProductQuery not ready
return;
}
final File cacheFile = await _newsCacheFile;
String? jsonString;
// Try from the cache first
if (!forceUpdate && _isNewsCacheValid(cacheFile)) {
jsonString = cacheFile.readAsStringSync();
}
if (jsonString == null || jsonString.isEmpty == true) {
jsonString = await _fetchJSON();
}
if (jsonString?.isNotEmpty != true) {
_emit(const AppNewsStateError('JSON news file is empty'));
return;
}
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) {
_emit(const AppNewsStateError('Unable to parse the JSON news file'));
Logs.e('Unable to parse the JSON news file');
} else {
_emit(AppNewsStateLoaded(appNews, cacheFile.lastModifiedSync()));
Logs.i('News ${forceUpdate ? 're' : ''}loaded');
}
}