refactor: remove unnecessary emit calls in switch_proxy_node and enhance profile switch notifications

This commit is contained in:
Tunglies 2025-11-06 00:30:43 +08:00
parent 6eaf999240
commit 21de5cbb0b
No known key found for this signature in database
GPG Key ID: B9B01B389469B3E8
2 changed files with 75 additions and 47 deletions

View File

@ -60,7 +60,6 @@ pub async fn switch_proxy_node(group_name: &str, proxy_name: &str) {
group_name, group_name,
proxy_name proxy_name
); );
let _ = handle::Handle::app_handle().emit("verge://force-refresh-proxies", ());
let _ = tray::Tray::global().update_menu().await; let _ = tray::Tray::global().update_menu().await;
} }
Err(err) => { Err(err) => {
@ -72,7 +71,6 @@ pub async fn switch_proxy_node(group_name: &str, proxy_name: &str) {
proxy_name, proxy_name,
err err
); );
let _ = handle::Handle::app_handle().emit("verge://force-refresh-proxies", ());
} }
} }
} }

View File

@ -4,9 +4,9 @@ use smartstring::alias::String;
use tauri::Url; use tauri::Url;
use crate::{ use crate::{
config::{PrfItem, profiles}, config::{Config, PrfItem, profiles},
core::handle, core::handle,
logging, logging_error, logging,
utils::logging::Type, utils::logging::Type,
}; };
@ -29,12 +29,13 @@ pub(super) async fn resolve_scheme(param: &str) -> Result<()> {
} }
}; };
let (url_param, name) =
if link_parsed.scheme() == "clash" || link_parsed.scheme() == "clash-verge" { if link_parsed.scheme() == "clash" || link_parsed.scheme() == "clash-verge" {
let name_owned: Option<String> = link_parsed let name_owned: Option<String> = link_parsed
.query_pairs() .query_pairs()
.find(|(key, _)| key == "name") .find(|(key, _)| key == "name")
.map(|(_, value)| value.into_owned().into()); .map(|(_, value)| value.into_owned().into());
let name = name_owned.as_ref(); let name = name_owned.to_owned();
let url_param = if let Some(query) = link_parsed.query() { let url_param = if let Some(query) = link_parsed.query() {
let prefix = "url="; let prefix = "url=";
@ -47,39 +48,68 @@ pub(super) async fn resolve_scheme(param: &str) -> Result<()> {
} else { } else {
None None
}; };
(url_param, name)
} else {
(None, None)
};
match url_param { let url = if let Some(ref url) = url_param {
Some(ref url) => { url
logging!(info, Type::Config, "decoded subscription url: {url}"); } else {
match PrfItem::from_url(url.as_ref(), name, None, None).await { logging!(
Ok(mut item) => { error,
let uid = match item.uid.clone() { Type::Config,
Some(uid) => uid, "missing url parameter in deep link: {}",
None => { param_str
logging!(error, Type::Config, "Profile item missing UID");
handle::Handle::notice_message(
"import_sub_url::error",
"Profile item missing UID".to_string(),
); );
return Ok(()); return Ok(());
};
let mut item = match PrfItem::from_url(url, name.as_ref(), None, None).await {
Ok(item) => item,
Err(e) => {
logging!(
error,
Type::Config,
"failed to parse profile from url: {:?}",
e
);
// TODO 通知系统疑似损坏,前端无法显示通知事件
handle::Handle::notice_message("import_sub_url::error", e.to_string());
return Ok(());
} }
}; };
let result = profiles::profiles_append_item_safe(&mut item).await;
logging_error!( let uid = item.uid.clone().unwrap_or_default();
Type::Config, // TODO 通过 deep link 导入后需要正确调用前端刷新订阅页面,以及通知结果
"failed to import subscription url: {:?}", match profiles::profiles_append_item_safe(&mut item).await {
result Ok(_) => {
Config::profiles().await.apply();
let _ = Config::profiles().await.data_arc().save_file().await;
// TODO 通知系统疑似损坏,前端无法显示通知事件
handle::Handle::notice_message(
"import_sub_url::ok",
item.uid.clone().unwrap_or_default(),
); );
handle::Handle::notice_message("import_sub_url::ok", uid); // TODO fuck me this shit is fucking broken as fucked
handle::Handle::notify_profile_changed(uid);
} }
Err(e) => { Err(e) => {
logging!(
error,
Type::Config,
"failed to import subscription url: {:?}",
e
);
Config::profiles().await.discard();
// TODO 通知系统疑似损坏,前端无法显示通知事件
handle::Handle::notice_message("import_sub_url::error", e.to_string()); handle::Handle::notice_message("import_sub_url::error", e.to_string());
} return Ok(());
}
}
None => bail!("failed to get profile url"),
} }
} }
handle::Handle::refresh_verge();
handle::Handle::refresh_clash();
Ok(()) Ok(())
} }