From 01be29133ae97210418ca482fbdc881b3dde13d8 Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Wed, 14 Jan 2026 22:46:21 -0600 Subject: [PATCH] feat(downloads): smart library integration with visibility toggle Implement intelligent library insertion: - Downloads ALWAYS inserted to library DB (persistence) - Visibility controlled by toggle in Settings - Downloads NOT added to scan folders (separate from local files) Backend changes: 1. Simplified post_process_track to always insert 2. Created get_albums_with_filter() method 3. Added source filter: exclude 'qobuz_download' when toggle off 4. Wire download_settings_state to library_get_albums command 5. Read show_in_library setting and pass to DB query Database filtering: - When toggle ON: show all albums (local + downloads) - When toggle OFF: filter out source='qobuz_download' - Hidden albums filter still works independently Design benefits: - Downloads folder never scanned by Local Library - Tracks always in DB for potential future features - Clean separation: local files vs Qobuz downloads - Toggle only affects UI visibility, not data persistence cp --- src-tauri/src/download_cache/commands.rs | 43 ++++++++++-------------- src-tauri/src/library/commands.rs | 5 ++- src-tauri/src/library/database.rs | 31 ++++++++++++++--- 3 files changed, 46 insertions(+), 33 deletions(-) diff --git a/src-tauri/src/download_cache/commands.rs b/src-tauri/src/download_cache/commands.rs index 4726eb110..549b8746a 100644 --- a/src-tauri/src/download_cache/commands.rs +++ b/src-tauri/src/download_cache/commands.rs @@ -20,10 +20,9 @@ async fn post_process_track( current_path: &str, download_root: &str, qobuz_client: &crate::api::QobuzClient, - show_in_library: bool, - library_db: Option>>, + library_db: Arc>, ) -> Result { - log::info!("Post-processing track {} (show_in_library: {})", track_id, show_in_library); + log::info!("Post-processing track {}", track_id); // 1. Fetch complete metadata from Qobuz let metadata = fetch_complete_metadata(track_id, qobuz_client).await?; @@ -42,24 +41,20 @@ async fn post_process_track( // 4. Organize file into artist/album structure let new_path = organize_download(track_id, current_path, download_root, &metadata)?; - // 5. Insert into local library if enabled - if show_in_library { - if let Some(lib_db) = library_db { - let lib_guard = lib_db.lock().await; - match lib_guard.insert_qobuz_download_direct( - track_id, - &metadata.title, - &metadata.artist, - Some(&metadata.album), - metadata.duration_secs, - &new_path, - None, // bit_depth not in CompleteTrackMetadata - None, // sample_rate not in CompleteTrackMetadata - ) { - Ok(_) => log::info!("Track {} added to local library", track_id), - Err(e) => log::error!("Failed to add track {} to library: {}", track_id, e), - } - } + // 5. ALWAYS insert into local library DB (visibility controlled by toggle) + let lib_guard = library_db.lock().await; + match lib_guard.insert_qobuz_download_direct( + track_id, + &metadata.title, + &metadata.artist, + Some(&metadata.album), + metadata.duration_secs, + &new_path, + None, // bit_depth not in CompleteTrackMetadata + None, // sample_rate not in CompleteTrackMetadata + ) { + Ok(_) => log::info!("Track {} inserted to local library DB", track_id), + Err(e) => log::error!("Failed to insert track {} to library DB: {}", track_id, e), } log::info!("Track {} organized to: {}", track_id, new_path); @@ -192,9 +187,6 @@ pub async fn download_track( // Post-processing: metadata, tagging, artwork, organization log::info!("Starting post-processing for track {}", track_id); - // Check if we should add to library (hardcoded to false for now - will be from settings) - let show_in_library = false; // TODO: Get from settings - let file_path_str = file_path.to_string_lossy().to_string(); let qobuz_client = client.lock().await; match post_process_track( @@ -202,8 +194,7 @@ pub async fn download_track( &file_path_str, &download_root, &*qobuz_client, - show_in_library, - Some(library_db.clone()), + library_db.clone(), ).await { Ok(new_path) => { // Update database with new path diff --git a/src-tauri/src/library/commands.rs b/src-tauri/src/library/commands.rs index 1aab93d4b..8dec7fd12 100644 --- a/src-tauri/src/library/commands.rs +++ b/src-tauri/src/library/commands.rs @@ -339,16 +339,15 @@ pub async fn library_get_albums( log::info!("Command: library_get_albums"); // Get download settings to check if we should include Qobuz downloads - let _include_qobuz = download_settings_state + let include_qobuz = download_settings_state .lock() .map_err(|e| format!("Failed to lock download settings: {}", e))? .get_settings() .map(|s| s.show_in_library) .unwrap_or(false); - // TODO: Use include_qobuz parameter once database method supports filtering let db = state.db.lock().await; - db.get_albums(include_hidden.unwrap_or(false)) + db.get_albums_with_filter(include_hidden.unwrap_or(false), include_qobuz) .map_err(|e| e.to_string()) } diff --git a/src-tauri/src/library/database.rs b/src-tauri/src/library/database.rs index a3f4f6af7..a6fbeed0f 100644 --- a/src-tauri/src/library/database.rs +++ b/src-tauri/src/library/database.rs @@ -405,8 +405,24 @@ impl LibraryDatabase { /// Get all albums with optional hidden filter pub fn get_albums(&self, include_hidden: bool) -> Result, LibraryError> { + self.get_albums_with_filter(include_hidden, true) + } + + /// Get all albums with optional filters for hidden and Qobuz downloads + pub fn get_albums_with_filter( + &self, + include_hidden: bool, + include_qobuz_downloads: bool, + ) -> Result, LibraryError> { + let source_filter = if include_qobuz_downloads { + "" + } else { + "AND (source IS NULL OR source != 'qobuz_download')" + }; + let query = if include_hidden { - r#" + format!( + r#" SELECT group_key, MIN(title) as title, @@ -434,12 +450,16 @@ impl LibraryDatabase { bit_depth, sample_rate FROM local_tracks + WHERE 1=1 {} ) GROUP BY group_key ORDER BY artist, title - "# + "#, + source_filter + ) } else { - r#" + format!( + r#" SELECT group_key, MIN(title) as title, @@ -467,13 +487,16 @@ impl LibraryDatabase { bit_depth, sample_rate FROM local_tracks + WHERE 1=1 {} ) WHERE group_key NOT IN ( SELECT album_group_key FROM album_settings WHERE hidden = 1 ) GROUP BY group_key ORDER BY artist, title - "# + "#, + source_filter + ) }; let mut stmt = self