feat(phase4.2): add favorites preferences backend
- Create favorites_preferences.rs with FavoritesPreferences struct - Add database table (custom_icon_path, custom_icon_preset, tab_order) - Implement State wrapper and Tauri commands - Register state and commands in lib.rs - Default preset: heart, default order: tracks/albums/artists/playlists cc
This commit is contained in:
1 parent
d39180bd6c
commit
a6aa6aa936
3 files changed
+208
No files matched your search
@@ -0,0 +1,194 @@
|
||||
use rusqlite::{params, Connection, Result};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct FavoritesPreferences {
|
||||
pub custom_icon_path: Option<String>,
|
||||
pub custom_icon_preset: Option<String>,
|
||||
pub tab_order: Vec<String>,
|
||||
}
|
||||
|
||||
impl Default for FavoritesPreferences {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
custom_icon_path: None,
|
||||
custom_icon_preset: Some("heart".to_string()),
|
||||
tab_order: vec![
|
||||
"tracks".to_string(),
|
||||
"albums".to_string(),
|
||||
"artists".to_string(),
|
||||
"playlists".to_string(),
|
||||
],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct FavoritesPreferencesStore {
|
||||
conn: Connection,
|
||||
}
|
||||
|
||||
impl FavoritesPreferencesStore {
|
||||
pub fn new() -> Result<Self, String> {
|
||||
let data_dir = dirs::data_dir()
|
||||
.ok_or("Could not determine data directory")?
|
||||
.join("qbz");
|
||||
|
||||
std::fs::create_dir_all(&data_dir)
|
||||
.map_err(|e| format!("Failed to create data directory: {}", e))?;
|
||||
|
||||
let db_path = data_dir.join("favorites_preferences.db");
|
||||
let conn = Connection::open(&db_path)
|
||||
.map_err(|e| format!("Failed to open favorites preferences database: {}", e))?;
|
||||
|
||||
conn.execute(
|
||||
"CREATE TABLE IF NOT EXISTS favorites_preferences (
|
||||
id INTEGER PRIMARY KEY CHECK (id = 1),
|
||||
custom_icon_path TEXT,
|
||||
custom_icon_preset TEXT,
|
||||
tab_order TEXT NOT NULL
|
||||
)",
|
||||
[],
|
||||
)
|
||||
.map_err(|e| format!("Failed to create favorites preferences table: {}", e))?;
|
||||
|
||||
Ok(Self { conn })
|
||||
}
|
||||
|
||||
pub fn get_preferences(&self) -> Result<FavoritesPreferences, String> {
|
||||
let mut stmt = self.conn.prepare("SELECT custom_icon_path, custom_icon_preset, tab_order FROM favorites_preferences WHERE id = 1")
|
||||
.map_err(|e| format!("Failed to prepare select: {}", e))?;
|
||||
|
||||
let result = stmt.query_row([], |row| {
|
||||
let custom_icon_path: Option<String> = row.get(0)?;
|
||||
let custom_icon_preset: Option<String> = row.get(1)?;
|
||||
let tab_order_str: String = row.get(2)?;
|
||||
|
||||
let tab_order: Vec<String> = serde_json::from_str(&tab_order_str).unwrap_or_else(|_| {
|
||||
vec![
|
||||
"tracks".to_string(),
|
||||
"albums".to_string(),
|
||||
"artists".to_string(),
|
||||
"playlists".to_string(),
|
||||
]
|
||||
});
|
||||
|
||||
Ok(FavoritesPreferences {
|
||||
custom_icon_path,
|
||||
custom_icon_preset,
|
||||
tab_order,
|
||||
})
|
||||
});
|
||||
|
||||
match result {
|
||||
Ok(prefs) => Ok(prefs),
|
||||
Err(rusqlite::Error::QueryReturnedNoRows) => Ok(FavoritesPreferences::default()),
|
||||
Err(e) => Err(format!("Failed to query preferences: {}", e)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save_preferences(&self, prefs: &FavoritesPreferences) -> Result<(), String> {
|
||||
let tab_order_str = serde_json::to_string(&prefs.tab_order)
|
||||
.map_err(|e| format!("Failed to serialize tab_order: {}", e))?;
|
||||
|
||||
self.conn.execute(
|
||||
"INSERT OR REPLACE INTO favorites_preferences (id, custom_icon_path, custom_icon_preset, tab_order)
|
||||
VALUES (1, ?1, ?2, ?3)",
|
||||
params![prefs.custom_icon_path, prefs.custom_icon_preset, tab_order_str],
|
||||
)
|
||||
.map_err(|e| format!("Failed to save preferences: {}", e))?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct FavoritesPreferencesState {
|
||||
pub store: Arc<Mutex<FavoritesPreferencesStore>>,
|
||||
}
|
||||
|
||||
impl FavoritesPreferencesState {
|
||||
pub fn new() -> Result<Self, String> {
|
||||
let store = FavoritesPreferencesStore::new()?;
|
||||
Ok(Self {
|
||||
store: Arc::new(Mutex::new(store)),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn get_favorites_preferences(
|
||||
state: tauri::State<FavoritesPreferencesState>,
|
||||
) -> Result<FavoritesPreferences, String> {
|
||||
let store = state
|
||||
.store
|
||||
.lock()
|
||||
.map_err(|_| "Failed to lock favorites preferences store".to_string())?;
|
||||
store.get_preferences()
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn save_favorites_preferences(
|
||||
prefs: FavoritesPreferences,
|
||||
state: tauri::State<FavoritesPreferencesState>,
|
||||
) -> Result<(), String> {
|
||||
let store = state
|
||||
.store
|
||||
.lock()
|
||||
.map_err(|_| "Failed to lock favorites preferences store".to_string())?;
|
||||
store.save_preferences(&prefs)
|
||||
}
|
||||
|
||||
pub fn create_table(conn: &Connection) -> Result<()> {
|
||||
conn.execute(
|
||||
"CREATE TABLE IF NOT EXISTS favorites_preferences (
|
||||
id INTEGER PRIMARY KEY CHECK (id = 1),
|
||||
custom_icon_path TEXT,
|
||||
custom_icon_preset TEXT,
|
||||
tab_order TEXT NOT NULL
|
||||
)",
|
||||
[],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn load_preferences(conn: &Connection) -> Result<FavoritesPreferences> {
|
||||
let mut stmt = conn.prepare("SELECT custom_icon_path, custom_icon_preset, tab_order FROM favorites_preferences WHERE id = 1")?;
|
||||
|
||||
let result = stmt.query_row([], |row| {
|
||||
let custom_icon_path: Option<String> = row.get(0)?;
|
||||
let custom_icon_preset: Option<String> = row.get(1)?;
|
||||
let tab_order_str: String = row.get(2)?;
|
||||
|
||||
let tab_order: Vec<String> = serde_json::from_str(&tab_order_str).unwrap_or_else(|_| {
|
||||
vec![
|
||||
"tracks".to_string(),
|
||||
"albums".to_string(),
|
||||
"artists".to_string(),
|
||||
"playlists".to_string(),
|
||||
]
|
||||
});
|
||||
|
||||
Ok(FavoritesPreferences {
|
||||
custom_icon_path,
|
||||
custom_icon_preset,
|
||||
tab_order,
|
||||
})
|
||||
});
|
||||
|
||||
match result {
|
||||
Ok(prefs) => Ok(prefs),
|
||||
Err(rusqlite::Error::QueryReturnedNoRows) => Ok(FavoritesPreferences::default()),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save_preferences(conn: &Connection, prefs: &FavoritesPreferences) -> Result<()> {
|
||||
let tab_order_str = serde_json::to_string(&prefs.tab_order).unwrap();
|
||||
|
||||
conn.execute(
|
||||
"INSERT OR REPLACE INTO favorites_preferences (id, custom_icon_path, custom_icon_preset, tab_order)
|
||||
VALUES (1, ?1, ?2, ?3)",
|
||||
params![prefs.custom_icon_path, prefs.custom_icon_preset, tab_order_str],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
pub mod audio_settings;
|
||||
pub mod download_settings;
|
||||
pub mod playback_preferences;
|
||||
pub mod favorites_preferences;
|
||||
|
||||
pub use audio_settings::{
|
||||
AudioSettings,
|
||||
@@ -39,3 +40,10 @@ pub use playback_preferences::{
|
||||
get_playback_preferences,
|
||||
set_autoplay_mode,
|
||||
};
|
||||
|
||||
pub use favorites_preferences::{
|
||||
FavoritesPreferences,
|
||||
FavoritesPreferencesState,
|
||||
get_favorites_preferences,
|
||||
save_favorites_preferences,
|
||||
};
|
||||
@@ -175,6 +175,9 @@ pub fn run() {
|
||||
// Initialize playback preferences state
|
||||
let playback_prefs_state = config::playback_preferences::PlaybackPreferencesState::new()
|
||||
.expect("Failed to initialize playback preferences");
|
||||
// Initialize favorites preferences state
|
||||
let favorites_prefs_state = config::favorites_preferences::FavoritesPreferencesState::new()
|
||||
.expect("Failed to initialize favorites preferences");
|
||||
|
||||
// Read saved audio device and settings for player initialization
|
||||
let (saved_device, audio_settings) = audio_settings_state
|
||||
@@ -300,6 +303,7 @@ pub fn run() {
|
||||
.manage(download_settings_state)
|
||||
.manage(offline_state)
|
||||
.manage(playback_prefs_state)
|
||||
.manage(favorites_prefs_state)
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
// Auth commands
|
||||
commands::init_client,
|
||||
@@ -577,6 +581,8 @@ pub fn run() {
|
||||
config::playback_preferences::get_playback_preferences,
|
||||
config::playback_preferences::set_autoplay_mode,
|
||||
config::playback_preferences::set_show_context_icon,
|
||||
config::favorites_preferences::get_favorites_preferences,
|
||||
config::favorites_preferences::save_favorites_preferences,
|
||||
offline::commands::set_allow_immediate_scrobbling,
|
||||
offline::commands::set_allow_accumulated_scrobbling,
|
||||
offline::commands::set_show_network_folders_in_manual_offline,
|
||||
|
||||
Reference in new issue
Block a user