perf: enhance profile reordering logic and adjust logging level

This commit is contained in:
Tunglies 2026-01-15 23:59:27 +08:00
parent 152259981d
commit a5bfe5f377
No known key found for this signature in database
GPG Key ID: B9B01B389469B3E8
2 changed files with 33 additions and 18 deletions

View File

@ -117,7 +117,7 @@ pub async fn import_profile(url: std::string::String, option: Option<PrfOption>)
pub async fn reorder_profile(active_id: String, over_id: String) -> CmdResult { 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!(debug, Type::Cmd, "重新排序配置文件");
Config::profiles().await.apply(); Config::profiles().await.apply();
Ok(()) Ok(())
} }

View File

@ -159,28 +159,43 @@ impl IProfiles {
} }
/// reorder items /// reorder items
pub async fn reorder(&mut self, active_id: &String, over_id: &String) -> Result<()> { pub async fn reorder(&mut self, active_id: &str, over_id: &str) -> Result<()> {
let mut items = self.items.take().unwrap_or_default(); if active_id == over_id {
let mut old_index = None; return Ok(());
let mut new_index = None;
for (i, _) in items.iter().enumerate() {
if items[i].uid.as_ref() == Some(active_id) {
old_index = Some(i);
}
if items[i].uid.as_ref() == Some(over_id) {
new_index = Some(i);
}
} }
let (old_idx, new_idx) = match (old_index, new_index) { let Some(items) = self.items.as_mut() else {
(Some(old), Some(new)) => (old, new), return Ok(());
_ => return Ok(()),
}; };
let item = items.remove(old_idx);
items.insert(new_idx, item); let mut old_idx = None;
self.items = Some(items); let mut new_idx = None;
self.save_file().await
for (i, item) in items.iter().enumerate() {
if let Some(uid) = item.uid.as_ref() {
if uid == active_id {
old_idx = Some(i);
}
if uid == over_id {
new_idx = Some(i);
}
}
if old_idx.is_some() && new_idx.is_some() {
break;
}
}
if let (Some(old), Some(new)) = (old_idx, new_idx) {
if old < new {
items[old..=new].rotate_left(1);
} else {
items[new..=old].rotate_right(1);
}
return self.save_file().await;
}
Ok(())
} }
/// update the item value /// update the item value