mirror of
https://github.com/zarzet/SpotiFLAC-Mobile.git
synced 2026-11-21 20:00:49 +01:00
Keep Explore payloads and timestamps, autoplay learning, and release bodies with their validators in application-support snapshots. Serialize migration, replacement, metadata updates and clear across store instances. Flush and rename before removing legacy keys, retain data on failure, and use a tombstone to prevent clear from reviving old preferences. Preserve full payload content and unrelated settings. Explore reads are asynchronous and report actual file length; delayed release revalidation updates current metadata without overwriting a newer body. The 261281-byte host preference fixture drops to 21 bytes after migration. Sixty subsequent blob saves make zero preference writes, with only three initial removals. Migration, corruption, failure/retry, ordering and all three consumer paths pass scoped host tests.
46 lines
1.2 KiB
Dart
46 lines
1.2 KiB
Dart
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:mornye/services/preference_blob_store.dart';
|
|
|
|
class ExploreCacheSnapshot {
|
|
const ExploreCacheSnapshot({
|
|
required this.payload,
|
|
required this.timestampMs,
|
|
required this.sizeBytes,
|
|
});
|
|
|
|
final String? payload;
|
|
final int? timestampMs;
|
|
final int sizeBytes;
|
|
}
|
|
|
|
class ExploreCacheStore {
|
|
const ExploreCacheStore(this._prefs);
|
|
|
|
static const _cacheKey = 'explore_home_feed_cache';
|
|
static const _cacheTsKey = 'explore_home_feed_ts';
|
|
static const _blobs = PreferenceBlobStore('explore_feed', {
|
|
_cacheKey: String,
|
|
_cacheTsKey: int,
|
|
});
|
|
|
|
final SharedPreferences _prefs;
|
|
|
|
Future<ExploreCacheSnapshot> read() async {
|
|
final snapshot = await _blobs.read(_prefs);
|
|
return ExploreCacheSnapshot(
|
|
payload: snapshot.values[_cacheKey] as String?,
|
|
timestampMs: snapshot.values[_cacheTsKey] as int?,
|
|
sizeBytes: snapshot.sizeBytes,
|
|
);
|
|
}
|
|
|
|
Future<void> write(String payload) async {
|
|
await _blobs.write(_prefs, {
|
|
_cacheKey: payload,
|
|
_cacheTsKey: DateTime.now().millisecondsSinceEpoch,
|
|
});
|
|
}
|
|
|
|
Future<void> clear() => _blobs.clear(_prefs);
|
|
}
|