Skip to content

Commit

Permalink
feat: put profile works
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzgydi committed Dec 16, 2021
1 parent 83c0bde commit f736951
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions src-tauri/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ use crate::{
emit::ClashInfoPayload,
state::{ClashInfoState, ProfileLock},
},
utils::{app_home_dir, clash, fetch::fetch_profile},
utils::{
app_home_dir,
clash::{self, put_clash_profile},
fetch::fetch_profile,
},
};
use std::fs::File;
use std::io::Write;
Expand Down Expand Up @@ -135,27 +139,35 @@ pub fn set_profiles(

#[tauri::command]
/// change to target profile
pub fn put_profiles(current: usize, lock: State<'_, ProfileLock>) -> Result<(), String> {
pub async fn put_profiles(
current: usize,
lock: State<'_, ProfileLock>,
clash_info: State<'_, ClashInfoState>,
) -> Result<(), String> {
match lock.0.lock() {
Ok(_) => {}
Err(_) => return Err(format!("can not get file locked")),
};

let mut profiles = read_profiles();
let clash_info = match clash_info.0.lock() {
Ok(arc) => arc.clone(),
_ => return Err(format!("can not get clash info")),
};

let mut profiles = read_profiles();
let items_len = match &profiles.items {
Some(p) => p.len(),
None => 0,
};

if current >= items_len {
Err(format!(
return Err(format!(
"failed to change profile to the index `{}`",
current
))
} else {
profiles.current = Some(current as u32);
save_profiles(&profiles);
Ok(())
));
}

profiles.current = Some(current as u32);
save_profiles(&profiles);
put_clash_profile(&clash_info).await
}

0 comments on commit f736951

Please sign in to comment.