fix: update profile handling to apply and discard changes correctly

This commit is contained in:
Tunglies
2025-11-04 07:58:14 +08:00
parent ebd7f457d2
commit b70d45b66a

View File

@@ -27,8 +27,8 @@ static CURRENT_SWITCHING_PROFILE: AtomicBool = AtomicBool::new(false);
pub async fn get_profiles() -> CmdResult<SharedBox<IProfiles>> { pub async fn get_profiles() -> CmdResult<SharedBox<IProfiles>> {
logging!(debug, Type::Cmd, "获取配置文件列表"); logging!(debug, Type::Cmd, "获取配置文件列表");
let draft = Config::profiles().await; let draft = Config::profiles().await;
let latest = draft.latest_arc(); let data = draft.data_arc();
Ok(latest) Ok(data)
} }
/// 增强配置文件 /// 增强配置文件
@@ -100,9 +100,11 @@ pub async fn reorder_profile(active_id: String, over_id: String) -> CmdResult {
match profiles_reorder_safe(&active_id, &over_id).await { match profiles_reorder_safe(&active_id, &over_id).await {
Ok(_) => { Ok(_) => {
logging!(info, Type::Cmd, "重新排序配置文件"); logging!(info, Type::Cmd, "重新排序配置文件");
Config::profiles().await.apply();
Ok(()) Ok(())
} }
Err(err) => { Err(err) => {
Config::profiles().await.discard();
logging!(error, Type::Cmd, "重新排序配置文件失败: {}", err); logging!(error, Type::Cmd, "重新排序配置文件失败: {}", err);
Err(format!("重新排序配置文件失败: {}", err).into()) Err(format!("重新排序配置文件失败: {}", err).into())
} }
@@ -120,12 +122,16 @@ pub async fn create_profile(item: PrfItem, file_data: Option<String>) -> CmdResu
logging!(info, Type::Cmd, "[创建订阅] 发送配置变更通知: {}", uid); logging!(info, Type::Cmd, "[创建订阅] 发送配置变更通知: {}", uid);
handle::Handle::notify_profile_changed(uid.clone()); handle::Handle::notify_profile_changed(uid.clone());
} }
Config::profiles().await.apply();
Ok(()) Ok(())
} }
Err(err) => match err.to_string().as_str() { Err(err) => {
"the file already exists" => Err("the file already exists".into()), Config::profiles().await.discard();
_ => Err(format!("add profile error: {err}").into()), match err.to_string().as_str() {
}, "the file already exists" => Err("the file already exists".into()),
_ => Err(format!("add profile error: {err}").into()),
}
}
} }
} }
@@ -133,8 +139,12 @@ pub async fn create_profile(item: PrfItem, file_data: Option<String>) -> CmdResu
#[tauri::command] #[tauri::command]
pub async fn update_profile(index: String, option: Option<PrfOption>) -> CmdResult { pub async fn update_profile(index: String, option: Option<PrfOption>) -> CmdResult {
match feat::update_profile(&index, option.as_ref(), true, true).await { match feat::update_profile(&index, option.as_ref(), true, true).await {
Ok(_) => Ok(()), Ok(_) => {
let _: () = Config::profiles().await.apply();
Ok(())
}
Err(e) => { Err(e) => {
Config::profiles().await.discard();
logging!(error, Type::Cmd, "{}", e); logging!(error, Type::Cmd, "{}", e);
Err(e.to_string().into()) Err(e.to_string().into())
} }
@@ -144,12 +154,11 @@ pub async fn update_profile(index: String, option: Option<PrfOption>) -> CmdResu
/// 删除配置文件 /// 删除配置文件
#[tauri::command] #[tauri::command]
pub async fn delete_profile(index: String) -> CmdResult { pub async fn delete_profile(index: String) -> CmdResult {
println!("delete_profile: {}", index);
// 使用Send-safe helper函数 // 使用Send-safe helper函数
let should_update = profiles_delete_item_safe(&index).await.stringify_err()?; let should_update = profiles_delete_item_safe(&index).await.stringify_err()?;
profiles_save_file_safe().await.stringify_err()?; profiles_save_file_safe().await.stringify_err()?;
if should_update { if should_update {
Config::profiles().await.apply();
match CoreManager::global().update_config().await { match CoreManager::global().update_config().await {
Ok(_) => { Ok(_) => {
handle::Handle::refresh_clash(); handle::Handle::refresh_clash();