From f59f0e1b87c1cd40484c80f95dd2cda83f1f2293 Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Sat, 14 Feb 2026 14:54:20 -0600 Subject: [PATCH] feat(qbz-nix): Phase 6.2 - CoreBridge and V2 commands Add bridge layer connecting QbzCore to Tauri and example V2 commands. New files: - core_bridge.rs: CoreBridge wraps QbzCore for Tauri integration - commands_v2.rs: Example commands using new architecture V2 commands implemented: - v2_is_logged_in, v2_login, v2_logout (auth) - v2_get_queue_state, v2_set_repeat_mode, v2_toggle_shuffle, v2_clear_queue These coexist with old commands during migration. Frontend can gradually switch to V2 commands as they're tested. --- src-tauri/src/commands_v2.rs | 82 +++++++++++++++++++++++++++ src-tauri/src/core_bridge.rs | 107 +++++++++++++++++++++++++++++++++++ src-tauri/src/lib.rs | 4 +- 3 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 src-tauri/src/commands_v2.rs create mode 100644 src-tauri/src/core_bridge.rs diff --git a/src-tauri/src/commands_v2.rs b/src-tauri/src/commands_v2.rs new file mode 100644 index 000000000..9097e197d --- /dev/null +++ b/src-tauri/src/commands_v2.rs @@ -0,0 +1,82 @@ +//! V2 Commands - Using the new multi-crate architecture +//! +//! These commands use QbzCore via CoreBridge instead of the old AppState. +//! They coexist with the old commands during migration. + +use tauri::State; + +use qbz_models::{QueueState, RepeatMode, UserSession}; + +use crate::core_bridge::CoreBridgeState; + +// ==================== Auth Commands (V2) ==================== + +/// Check if user is logged in (V2 - uses QbzCore) +#[tauri::command] +pub async fn v2_is_logged_in( + bridge: State<'_, CoreBridgeState>, +) -> Result { + let bridge = bridge.get().await; + Ok(bridge.is_logged_in().await) +} + +/// Login with email and password (V2 - uses QbzCore) +#[tauri::command] +pub async fn v2_login( + email: String, + password: String, + bridge: State<'_, CoreBridgeState>, +) -> Result { + let bridge = bridge.get().await; + bridge.login(&email, &password).await +} + +/// Logout current user (V2 - uses QbzCore) +#[tauri::command] +pub async fn v2_logout( + bridge: State<'_, CoreBridgeState>, +) -> Result<(), String> { + let bridge = bridge.get().await; + bridge.logout().await +} + +// ==================== Queue Commands (V2) ==================== + +/// Get current queue state (V2 - uses QbzCore) +#[tauri::command] +pub async fn v2_get_queue_state( + bridge: State<'_, CoreBridgeState>, +) -> Result { + let bridge = bridge.get().await; + Ok(bridge.get_queue_state().await) +} + +/// Set repeat mode (V2 - uses QbzCore) +#[tauri::command] +pub async fn v2_set_repeat_mode( + mode: RepeatMode, + bridge: State<'_, CoreBridgeState>, +) -> Result<(), String> { + let bridge = bridge.get().await; + bridge.set_repeat_mode(mode).await; + Ok(()) +} + +/// Toggle shuffle (V2 - uses QbzCore) +#[tauri::command] +pub async fn v2_toggle_shuffle( + bridge: State<'_, CoreBridgeState>, +) -> Result { + let bridge = bridge.get().await; + Ok(bridge.toggle_shuffle().await) +} + +/// Clear the queue (V2 - uses QbzCore) +#[tauri::command] +pub async fn v2_clear_queue( + bridge: State<'_, CoreBridgeState>, +) -> Result<(), String> { + let bridge = bridge.get().await; + bridge.clear_queue().await; + Ok(()) +} diff --git a/src-tauri/src/core_bridge.rs b/src-tauri/src/core_bridge.rs new file mode 100644 index 000000000..ba22430e4 --- /dev/null +++ b/src-tauri/src/core_bridge.rs @@ -0,0 +1,107 @@ +//! Core Bridge +//! +//! Bridges the new QbzCore architecture with the existing Tauri app. +//! This allows gradual migration - new code uses QbzCore, old code +//! continues to work until migrated. + +use std::sync::Arc; +use tokio::sync::RwLock; + +use qbz_core::QbzCore; +use qbz_models::{QueueState, RepeatMode, UserSession}; + +use crate::tauri_adapter::TauriAdapter; + +/// Bridge to the new QbzCore architecture +/// +/// This struct provides access to QbzCore functionality while +/// the old AppState continues to handle unmigrated features. +pub struct CoreBridge { + core: Arc>, +} + +impl CoreBridge { + /// Create a new CoreBridge with the given TauriAdapter + pub async fn new(adapter: TauriAdapter) -> Result { + let core = QbzCore::new(adapter); + core.init().await.map_err(|e| e.to_string())?; + + Ok(Self { + core: Arc::new(core), + }) + } + + /// Get a reference to the underlying QbzCore + pub fn core(&self) -> &Arc> { + &self.core + } + + // ==================== Auth Commands ==================== + + /// Check if user is logged in + pub async fn is_logged_in(&self) -> bool { + self.core.has_session().await + } + + /// Login with email and password + pub async fn login(&self, email: &str, password: &str) -> Result { + self.core.login(email, password).await.map_err(|e| e.to_string()) + } + + /// Logout current user + pub async fn logout(&self) -> Result<(), String> { + self.core.logout().await.map_err(|e| e.to_string()) + } + + // ==================== Queue Commands ==================== + + /// Get current queue state + pub async fn get_queue_state(&self) -> QueueState { + self.core.get_queue_state().await + } + + /// Set repeat mode + pub async fn set_repeat_mode(&self, mode: RepeatMode) { + self.core.set_repeat_mode(mode).await + } + + /// Toggle shuffle + pub async fn toggle_shuffle(&self) -> bool { + self.core.toggle_shuffle().await + } + + /// Clear the queue + pub async fn clear_queue(&self) { + self.core.clear_queue().await + } +} + +/// State wrapper for Tauri's managed state +pub struct CoreBridgeState(pub Arc>>); + +impl CoreBridgeState { + pub fn new() -> Self { + Self(Arc::new(RwLock::new(None))) + } + + /// Initialize the core bridge with the app handle + pub async fn init(&self, adapter: TauriAdapter) -> Result<(), String> { + let bridge = CoreBridge::new(adapter).await?; + *self.0.write().await = Some(bridge); + Ok(()) + } + + /// Get the bridge (panics if not initialized) + pub async fn get(&self) -> impl std::ops::Deref + '_ { + tokio::sync::RwLockReadGuard::map( + self.0.read().await, + |opt| opt.as_ref().expect("CoreBridge not initialized") + ) + } +} + +impl Default for CoreBridgeState { + fn default() -> Self { + Self::new() + } +} diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 67fb54005..9fba211fe 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -2,8 +2,10 @@ //! //! This application uses the Qobuz API but is not certified by Qobuz. -// New multi-crate architecture adapter +// New multi-crate architecture pub mod tauri_adapter; +pub mod core_bridge; +pub mod commands_v2; pub mod api; pub mod api_cache;