diff --git a/flutter_client/lib/cache/audio_cache_manager.dart b/flutter_client/lib/cache/audio_cache_manager.dart index 9733eba9..34a4a061 100644 --- a/flutter_client/lib/cache/audio_cache_manager.dart +++ b/flutter_client/lib/cache/audio_cache_manager.dart @@ -96,13 +96,25 @@ class AudioCacheManager { .go(); } - /// Total bytes used by the cache. + /// Total bytes used by the cache. Walks the cache directory directly + /// instead of summing the index, because the streaming-as-you-play + /// path (audio_handler's LockCachingAudioSource) writes files + /// without registering an index row. The index sum would always + /// understate (often to zero) for users who only stream. Future usageBytes() async { - final result = await _db.customSelect( - 'SELECT COALESCE(SUM(size_bytes), 0) AS total FROM audio_cache_index', - readsFrom: {_db.audioCacheIndex}, - ).getSingle(); - return result.read('total'); + final dir = Directory(await _tracksDir()); + if (!await dir.exists()) return 0; + var total = 0; + await for (final entity in dir.list(followLinks: false)) { + if (entity is File) { + try { + total += await entity.length(); + } catch (_) { + // Race against concurrent writes / deletes — just skip. + } + } + } + return total; } /// Evicts files until usage ≤ targetBytes. Eviction order: