fix(config): refresh announcements and remote content

This commit is contained in:
zarzet
2026-07-25 15:43:25 +07:00
parent 555f0e8117
commit 19787ed461
3 changed files with 163 additions and 24 deletions
+8 -8
View File
@@ -9,7 +9,9 @@ import 'package:spotiflac_android/widgets/donate_icons.dart';
import 'package:spotiflac_android/widgets/settings_sliver_app_bar.dart';
class DonatePage extends StatefulWidget {
const DonatePage({super.key});
final AppRemoteConfigService? remoteConfigService;
const DonatePage({super.key, this.remoteConfigService});
@override
State<DonatePage> createState() => _DonatePageState();
@@ -26,11 +28,11 @@ class _DonatePageState extends State<DonatePage> {
if (_hasRequestedConfig) return;
_hasRequestedConfig = true;
_loadConfig(Localizations.localeOf(context).toLanguageTag());
unawaited(_loadConfig(Localizations.localeOf(context).toLanguageTag()));
}
Future<void> _loadConfig(String locale) async {
final service = AppRemoteConfigService();
final service = widget.remoteConfigService ?? AppRemoteConfigService();
final cached = await service.readCachedConfig();
if (!mounted) return;
@@ -38,11 +40,9 @@ class _DonatePageState extends State<DonatePage> {
_applyRemoteConfig(cached);
}
unawaited(_refreshConfigCache(locale));
}
Future<void> _refreshConfigCache(String locale) async {
await AppRemoteConfigService().fetchConfigSnapshot(locale: locale);
final refreshed = await service.fetchConfigSnapshot(locale: locale);
if (!mounted || refreshed == null) return;
_applyRemoteConfig(refreshed);
}
void _applyRemoteConfig(RemoteConfigSnapshot snapshot) {
+1 -16
View File
@@ -262,11 +262,8 @@ class AppRemoteConfigService {
static const _cachedConfigJsonKey = 'app_remote_config_cached_json';
static const _cachedConfigFetchedAtKey =
'app_remote_config_cached_fetched_at';
static const _lastFetchAttemptAtKey = 'app_remote_config_last_fetch_at';
static const _dismissedAnnouncementIdsKey =
'app_remote_config_dismissed_announcement_ids';
// Config changes are rare; don't re-hit the endpoint on every app open.
static const _fetchTtl = Duration(hours: 6);
final http.Client _client;
final String endpoint;
@@ -293,15 +290,6 @@ class AppRemoteConfigService {
Future<RemoteConfigSnapshot?> fetchConfigSnapshot({String? locale}) async {
try {
final prefs = await SharedPreferences.getInstance();
final lastFetch = DateTime.tryParse(
prefs.getString(_lastFetchAttemptAtKey) ?? '',
);
if (lastFetch != null &&
DateTime.now().difference(lastFetch) < _fetchTtl) {
return readCachedConfig();
}
final uri = Uri.parse(endpoint).replace(
queryParameters: {
'platform': Platform.isAndroid ? 'android' : Platform.operatingSystem,
@@ -323,10 +311,7 @@ class AppRemoteConfigService {
final snapshot = _parseSnapshot(response.body);
if (snapshot == null) return null;
await prefs.setString(
_lastFetchAttemptAtKey,
DateTime.now().toIso8601String(),
);
final prefs = await SharedPreferences.getInstance();
final cachedJson = prefs.getString(_cachedConfigJsonKey);
if (cachedJson != snapshot.rawJson) {
await prefs.setString(_cachedConfigJsonKey, snapshot.rawJson);