Files
smooth-app/packages/smooth_app/lib/data_models/downloadable_string.dart
monsieurtanuki fd4c395383 feat: #41 - now storing preference references (for the next app start) (#1007)
The main goal is to avoid screen flickering at app start time, when the preference references are downloaded and notify a refresh (again and again everyday, with the same language).

New file:
* `downloadable_string.dart`: Downloadable String that may be stored (and compared to the previous value).

Impacted files:
* `main.dart`: refactored
* `product_preferences.dart`: created explicit methods `init` and `refresh`; now stores locally the downloaded strings, that will be reused for the next app start
* `user_preferences_page_test.dart`: refactored
2022-01-24 18:44:09 +01:00

42 lines
1.0 KiB
Dart

import 'package:http/http.dart';
import 'package:smooth_app/database/dao_string.dart';
/// Downloadable String that may be stored (and compared to the previous value).
class DownloadableString {
DownloadableString(
this.uri, {
this.dao,
});
final Uri uri;
final DaoString? dao;
String? _value;
/// The actual string value.
String? get value => _value;
/// Downloads data and stores it locally if possible.
///
/// Returns true if the downloaded string is different
/// from the previously stored one.
/// May throw an Exception.
Future<bool> download() async {
final Response response = await get(uri);
if (response.statusCode != 200) {
throw Exception('status is ${response.statusCode} for $uri');
}
_value = response.body;
if (dao != null) {
final String key = uri.toString();
final String? previousString = await dao!.get(key);
if (_value == previousString) {
return false;
}
await dao!.put(key, _value);
}
return true;
}
}