mirror of
https://github.com/Drop-OSS/drop-app.git
synced 2026-06-22 04:11:37 +10:00
Update autostart.rs to include DB
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
use log::info;
|
use log::info;
|
||||||
use tauri::AppHandle;
|
use tauri::AppHandle;
|
||||||
use tauri_plugin_autostart::ManagerExt;
|
use tauri_plugin_autostart::ManagerExt;
|
||||||
|
use crate::DB;
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn toggle_autostart(app: AppHandle, enabled: bool) -> Result<(), String> {
|
pub async fn toggle_autostart(app: AppHandle, enabled: bool) -> Result<(), String> {
|
||||||
@@ -12,11 +13,57 @@ pub async fn toggle_autostart(app: AppHandle, enabled: bool) -> Result<(), Strin
|
|||||||
manager.disable().map_err(|e| e.to_string())?;
|
manager.disable().map_err(|e| e.to_string())?;
|
||||||
info!("Disabled autostart");
|
info!("Disabled autostart");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Store the state in DB
|
||||||
|
let mut db_handle = DB.borrow_data_mut().map_err(|e| e.to_string())?;
|
||||||
|
db_handle.settings.autostart = enabled;
|
||||||
|
drop(db_handle);
|
||||||
|
DB.save().map_err(|e| e.to_string())?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn get_autostart_enabled(app: AppHandle) -> Result<bool, String> {
|
pub async fn get_autostart_enabled(app: AppHandle) -> Result<bool, String> {
|
||||||
|
// First check DB state
|
||||||
|
let db_handle = DB.borrow_data().map_err(|e| e.to_string())?;
|
||||||
|
let db_state = db_handle.settings.autostart;
|
||||||
|
drop(db_handle);
|
||||||
|
|
||||||
|
// Get actual system state
|
||||||
let manager = app.autolaunch();
|
let manager = app.autolaunch();
|
||||||
manager.is_enabled().map_err(|e| e.to_string())
|
let system_state = manager.is_enabled().map_err(|e| e.to_string())?;
|
||||||
}
|
|
||||||
|
// If they don't match, sync to DB state
|
||||||
|
if db_state != system_state {
|
||||||
|
if db_state {
|
||||||
|
manager.enable().map_err(|e| e.to_string())?;
|
||||||
|
} else {
|
||||||
|
manager.disable().map_err(|e| e.to_string())?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(db_state)
|
||||||
|
}
|
||||||
|
|
||||||
|
// New function to sync state on startup
|
||||||
|
pub fn sync_autostart_on_startup(app: &AppHandle) -> Result<(), String> {
|
||||||
|
let db_handle = DB.borrow_data().map_err(|e| e.to_string())?;
|
||||||
|
let should_be_enabled = db_handle.settings.autostart;
|
||||||
|
drop(db_handle);
|
||||||
|
|
||||||
|
let manager = app.autolaunch();
|
||||||
|
let current_state = manager.is_enabled().map_err(|e| e.to_string())?;
|
||||||
|
|
||||||
|
if current_state != should_be_enabled {
|
||||||
|
if should_be_enabled {
|
||||||
|
manager.enable().map_err(|e| e.to_string())?;
|
||||||
|
info!("Synced autostart: enabled");
|
||||||
|
} else {
|
||||||
|
manager.disable().map_err(|e| e.to_string())?;
|
||||||
|
info!("Synced autostart: disabled");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user