refactor(v2): inline offline cache fully-cached delegates

Inlines the bodies of `check_album_fully_cached` and
`check_albums_fully_cached_batch` from
crate::offline_cache::commands::* directly into
commands_v2/legacy_compat.rs, mapping the legacy String error type to
RuntimeError::Internal at the v2 boundary.

Note: v2_cache_notification_artwork is NOT a delegate — it lives in
commands_v2/helpers.rs and is a true helper used by other v2 commands.
The investigation list flagged it as a possible delegate, but
verification showed it owns its implementation.

Deletes the legacy #[tauri::command] definitions in
offline_cache/commands.rs. Other functions in the offline_cache::commands
module remain (still called from commands_v2/library.rs delegates,
which are step 8 scope).

No frontend invoke changes required.

Step 6 of the legacy cleanup plan, group D (offline cache).

cc
This commit is contained in:
vicrodh committed 2026-04-26 18:16:02 -06:00
1 parent 515f73b259
commit afa8298745
2 files changed
+61 -84

No files matched your search

+61 -6
View File
@@ -649,9 +649,30 @@ pub async fn v2_check_album_fully_cached(
albumId: String,
cache_state: State<'_, crate::offline_cache::OfflineCacheState>,
) -> Result<bool, RuntimeError> {
crate::offline_cache::commands::check_album_fully_cached(albumId, cache_state)
.await
.map_err(RuntimeError::Internal)
let guard__ = cache_state.db.lock().await;
let db = guard__
.as_ref()
.ok_or_else(|| RuntimeError::Internal("No active session - please log in".to_string()))?;
// Get all tracks for this album
let tracks = db.get_all_tracks().map_err(RuntimeError::Internal)?;
let album_tracks: Vec<_> = tracks
.into_iter()
.filter(|t| t.album_id.as_deref() == Some(&albumId))
.collect();
if album_tracks.is_empty() {
return Ok(false);
}
// Check if all tracks are ready
for track in album_tracks {
if track.status != crate::offline_cache::OfflineCacheStatus::Ready {
return Ok(false);
}
}
Ok(true)
}
#[tauri::command]
@@ -660,9 +681,43 @@ pub async fn v2_check_albums_fully_cached_batch(
albumIds: Vec<String>,
cache_state: State<'_, crate::offline_cache::OfflineCacheState>,
) -> Result<std::collections::HashMap<String, bool>, RuntimeError> {
crate::offline_cache::commands::check_albums_fully_cached_batch(albumIds, cache_state)
.await
.map_err(RuntimeError::Internal)
use std::collections::HashMap;
if albumIds.is_empty() {
return Ok(HashMap::new());
}
let guard__ = cache_state.db.lock().await;
let db = guard__
.as_ref()
.ok_or_else(|| RuntimeError::Internal("No active session - please log in".to_string()))?;
let tracks = db.get_all_tracks().map_err(RuntimeError::Internal)?;
// Group tracks by album_id
let mut album_tracks: HashMap<&str, (usize, usize)> = HashMap::new(); // (total, ready)
for track in &tracks {
if let Some(ref aid) = track.album_id {
let entry = album_tracks.entry(aid.as_str()).or_insert((0, 0));
entry.0 += 1;
if track.status == crate::offline_cache::OfflineCacheStatus::Ready {
entry.1 += 1;
}
}
}
let result: HashMap<String, bool> = albumIds
.into_iter()
.map(|id| {
let fully_cached = album_tracks
.get(id.as_str())
.map(|(total, ready)| *total > 0 && *total == *ready)
.unwrap_or(false);
(id, fully_cached)
})
.collect();
Ok(result)
}
/// Shared helper: spawn the download task for a single track.
-78
View File
@@ -788,84 +788,6 @@ pub async fn open_track_folder(
Ok(())
}
/// Check if an album is fully cached (all tracks ready)
#[tauri::command]
pub async fn check_album_fully_cached(
album_id: String,
cache_state: State<'_, OfflineCacheState>,
) -> Result<bool, String> {
let guard__ = cache_state.db.lock().await;
let db = guard__
.as_ref()
.ok_or("No active session - please log in")?;
// Get all tracks for this album
let tracks = db.get_all_tracks()?;
let album_tracks: Vec<_> = tracks
.into_iter()
.filter(|t| t.album_id.as_deref() == Some(&album_id))
.collect();
if album_tracks.is_empty() {
return Ok(false);
}
// Check if all tracks are ready
for track in album_tracks {
if track.status != crate::offline_cache::OfflineCacheStatus::Ready {
return Ok(false);
}
}
Ok(true)
}
/// Batch check which albums are fully cached (all tracks ready).
/// Single DB read instead of N individual calls.
#[tauri::command]
pub async fn check_albums_fully_cached_batch(
album_ids: Vec<String>,
cache_state: State<'_, OfflineCacheState>,
) -> Result<std::collections::HashMap<String, bool>, String> {
use std::collections::HashMap;
if album_ids.is_empty() {
return Ok(HashMap::new());
}
let guard__ = cache_state.db.lock().await;
let db = guard__
.as_ref()
.ok_or("No active session - please log in")?;
let tracks = db.get_all_tracks()?;
// Group tracks by album_id
let mut album_tracks: HashMap<&str, (usize, usize)> = HashMap::new(); // (total, ready)
for track in &tracks {
if let Some(ref aid) = track.album_id {
let entry = album_tracks.entry(aid.as_str()).or_insert((0, 0));
entry.0 += 1;
if track.status == crate::offline_cache::OfflineCacheStatus::Ready {
entry.1 += 1;
}
}
}
let result: HashMap<String, bool> = album_ids
.into_iter()
.map(|id| {
let fully_cached = album_tracks
.get(id.as_str())
.map(|(total, ready)| *total > 0 && *total == *ready)
.unwrap_or(false);
(id, fully_cached)
})
.collect();
Ok(result)
}
/// Evict tracks if cache exceeds limit (LRU policy)
async fn evict_if_needed(cache_state: &OfflineCacheState, limit_bytes: u64) -> Result<(), String> {
let guard__ = cache_state.db.lock().await;