fix: delete profile item command
This commit is contained in:
parent
182bf49ad0
commit
a4c1573c45
@ -107,13 +107,23 @@ pub fn select_profile(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// todo: need to check
|
|
||||||
/// delete profile item
|
/// delete profile item
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn delete_profile(index: usize, profiles: State<'_, ProfilesState>) -> Result<(), String> {
|
pub fn delete_profile(
|
||||||
match profiles.0.lock() {
|
index: usize,
|
||||||
Ok(mut profiles) => profiles.delete_item(index),
|
clash_state: State<'_, ClashState>,
|
||||||
Err(_) => Err("can not get profiles lock".into()),
|
profiles_state: State<'_, ProfilesState>,
|
||||||
|
) -> Result<(), String> {
|
||||||
|
let mut profiles = profiles_state.0.lock().unwrap();
|
||||||
|
match profiles.delete_item(index) {
|
||||||
|
Ok(change) => match change {
|
||||||
|
true => {
|
||||||
|
let clash = clash_state.0.lock().unwrap();
|
||||||
|
profiles.activate(clash.info.clone())
|
||||||
|
}
|
||||||
|
false => Ok(()),
|
||||||
|
},
|
||||||
|
Err(err) => Err(err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -195,7 +195,7 @@ impl ProfilesConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// delete the item
|
/// delete the item
|
||||||
pub fn delete_item(&mut self, index: usize) -> Result<(), String> {
|
pub fn delete_item(&mut self, index: usize) -> Result<bool, String> {
|
||||||
let mut current = self.current.clone().unwrap_or(0);
|
let mut current = self.current.clone().unwrap_or(0);
|
||||||
let mut items = self.items.clone().unwrap_or(vec![]);
|
let mut items = self.items.clone().unwrap_or(vec![]);
|
||||||
|
|
||||||
@ -205,13 +205,22 @@ impl ProfilesConfig {
|
|||||||
|
|
||||||
items.remove(index);
|
items.remove(index);
|
||||||
|
|
||||||
|
let mut should_change = false;
|
||||||
|
|
||||||
if current == index {
|
if current == index {
|
||||||
current = 0;
|
current = 0;
|
||||||
|
should_change = true;
|
||||||
} else if current > index {
|
} else if current > index {
|
||||||
current = current - 1;
|
current = current - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.current = Some(current);
|
self.current = Some(current);
|
||||||
self.save_file()
|
self.items = Some(items);
|
||||||
|
|
||||||
|
match self.save_file() {
|
||||||
|
Ok(_) => Ok(should_change),
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// activate current profile
|
/// activate current profile
|
||||||
|
Loading…
x
Reference in New Issue
Block a user