refactor: rename profiles & command state
This commit is contained in:
parent
47155a4a29
commit
8d7ab9d05e
@ -1,5 +1,5 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
core::{ClashInfo, ProfileItem, ProfilesConfig, VergeConfig},
|
core::{ClashInfo, ProfileItem, Profiles, VergeConfig},
|
||||||
states::{ClashState, ProfilesState, VergeState},
|
states::{ClashState, ProfilesState, VergeState},
|
||||||
utils::{dirs::app_home_dir, fetch::fetch_profile, sysopt::SysProxyConfig},
|
utils::{dirs::app_home_dir, fetch::fetch_profile, sysopt::SysProxyConfig},
|
||||||
};
|
};
|
||||||
@ -10,8 +10,8 @@ use tauri::State;
|
|||||||
/// get all profiles from `profiles.yaml`
|
/// get all profiles from `profiles.yaml`
|
||||||
/// do not acquire the lock of ProfileLock
|
/// do not acquire the lock of ProfileLock
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn get_profiles(profiles: State<'_, ProfilesState>) -> Result<ProfilesConfig, String> {
|
pub fn get_profiles(profiles_state: State<'_, ProfilesState>) -> Result<Profiles, String> {
|
||||||
match profiles.0.lock() {
|
match profiles_state.0.lock() {
|
||||||
Ok(profiles) => Ok(profiles.clone()),
|
Ok(profiles) => Ok(profiles.clone()),
|
||||||
Err(_) => Err("failed to get profiles lock".into()),
|
Err(_) => Err("failed to get profiles lock".into()),
|
||||||
}
|
}
|
||||||
@ -19,8 +19,8 @@ pub fn get_profiles(profiles: State<'_, ProfilesState>) -> Result<ProfilesConfig
|
|||||||
|
|
||||||
/// synchronize data irregularly
|
/// synchronize data irregularly
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn sync_profiles(profiles: State<'_, ProfilesState>) -> Result<(), String> {
|
pub fn sync_profiles(profiles_state: State<'_, ProfilesState>) -> Result<(), String> {
|
||||||
match profiles.0.lock() {
|
match profiles_state.0.lock() {
|
||||||
Ok(mut profiles) => profiles.sync_file(),
|
Ok(mut profiles) => profiles.sync_file(),
|
||||||
Err(_) => Err("failed to get profiles lock".into()),
|
Err(_) => Err("failed to get profiles lock".into()),
|
||||||
}
|
}
|
||||||
@ -32,11 +32,11 @@ pub fn sync_profiles(profiles: State<'_, ProfilesState>) -> Result<(), String> {
|
|||||||
pub async fn import_profile(
|
pub async fn import_profile(
|
||||||
url: String,
|
url: String,
|
||||||
with_proxy: bool,
|
with_proxy: bool,
|
||||||
profiles: State<'_, ProfilesState>,
|
profiles_state: State<'_, ProfilesState>,
|
||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
match fetch_profile(&url, with_proxy).await {
|
match fetch_profile(&url, with_proxy).await {
|
||||||
Some(result) => {
|
Some(result) => {
|
||||||
let mut profiles = profiles.0.lock().unwrap();
|
let mut profiles = profiles_state.0.lock().unwrap();
|
||||||
profiles.import_from_url(url, result)
|
profiles.import_from_url(url, result)
|
||||||
}
|
}
|
||||||
None => Err(format!("failed to fetch profile from `{}`", url)),
|
None => Err(format!("failed to fetch profile from `{}`", url)),
|
||||||
@ -48,11 +48,11 @@ pub async fn import_profile(
|
|||||||
pub async fn update_profile(
|
pub async fn update_profile(
|
||||||
index: usize,
|
index: usize,
|
||||||
with_proxy: bool,
|
with_proxy: bool,
|
||||||
clash: State<'_, ClashState>,
|
clash_state: State<'_, ClashState>,
|
||||||
profiles: State<'_, ProfilesState>,
|
profiles_state: State<'_, ProfilesState>,
|
||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
// maybe we can get the url from the web app directly
|
// maybe we can get the url from the web app directly
|
||||||
let url = match profiles.0.lock() {
|
let url = match profiles_state.0.lock() {
|
||||||
Ok(mut profile) => {
|
Ok(mut profile) => {
|
||||||
let items = profile.items.take().unwrap_or(vec![]);
|
let items = profile.items.take().unwrap_or(vec![]);
|
||||||
if index >= items.len() {
|
if index >= items.len() {
|
||||||
@ -69,14 +69,14 @@ pub async fn update_profile(
|
|||||||
};
|
};
|
||||||
|
|
||||||
match fetch_profile(&url, with_proxy).await {
|
match fetch_profile(&url, with_proxy).await {
|
||||||
Some(result) => match profiles.0.lock() {
|
Some(result) => match profiles_state.0.lock() {
|
||||||
Ok(mut profiles) => {
|
Ok(mut profiles) => {
|
||||||
profiles.update_item(index, result)?;
|
profiles.update_item(index, result)?;
|
||||||
|
|
||||||
// reactivate the profile
|
// reactivate the profile
|
||||||
let current = profiles.current.clone().unwrap_or(0);
|
let current = profiles.current.clone().unwrap_or(0);
|
||||||
if current == index {
|
if current == index {
|
||||||
let clash = clash.0.lock().unwrap();
|
let clash = clash_state.0.lock().unwrap();
|
||||||
profiles.activate(&clash)
|
profiles.activate(&clash)
|
||||||
} else {
|
} else {
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -92,14 +92,14 @@ pub async fn update_profile(
|
|||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn select_profile(
|
pub fn select_profile(
|
||||||
index: usize,
|
index: usize,
|
||||||
clash: State<'_, ClashState>,
|
clash_state: State<'_, ClashState>,
|
||||||
profiles: State<'_, ProfilesState>,
|
profiles_state: State<'_, ProfilesState>,
|
||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
let mut profiles = profiles.0.lock().unwrap();
|
let mut profiles = profiles_state.0.lock().unwrap();
|
||||||
|
|
||||||
match profiles.put_current(index) {
|
match profiles.put_current(index) {
|
||||||
Ok(()) => {
|
Ok(()) => {
|
||||||
let clash = clash.0.lock().unwrap();
|
let clash = clash_state.0.lock().unwrap();
|
||||||
profiles.activate(&clash)
|
profiles.activate(&clash)
|
||||||
}
|
}
|
||||||
Err(err) => Err(err),
|
Err(err) => Err(err),
|
||||||
@ -131,9 +131,9 @@ pub fn delete_profile(
|
|||||||
pub fn patch_profile(
|
pub fn patch_profile(
|
||||||
index: usize,
|
index: usize,
|
||||||
profile: ProfileItem,
|
profile: ProfileItem,
|
||||||
profiles: State<'_, ProfilesState>,
|
profiles_state: State<'_, ProfilesState>,
|
||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
match profiles.0.lock() {
|
match profiles_state.0.lock() {
|
||||||
Ok(mut profiles) => profiles.patch_item(index, profile),
|
Ok(mut profiles) => profiles.patch_item(index, profile),
|
||||||
Err(_) => Err("can not get profiles lock".into()),
|
Err(_) => Err("can not get profiles lock".into()),
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use super::{ProfilesConfig, Verge};
|
use super::{Profiles, Verge};
|
||||||
use crate::utils::{config, dirs};
|
use crate::utils::{config, dirs};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_yaml::{Mapping, Value};
|
use serde_yaml::{Mapping, Value};
|
||||||
@ -139,7 +139,7 @@ impl Clash {
|
|||||||
|
|
||||||
/// restart clash sidecar
|
/// restart clash sidecar
|
||||||
/// should reactivate profile after restart
|
/// should reactivate profile after restart
|
||||||
pub fn restart_sidecar(&mut self, profiles: &mut ProfilesConfig) -> Result<(), String> {
|
pub fn restart_sidecar(&mut self, profiles: &mut Profiles) -> Result<(), String> {
|
||||||
self.update_config();
|
self.update_config();
|
||||||
self.drop_sidecar()?;
|
self.drop_sidecar()?;
|
||||||
self.run_sidecar()?;
|
self.run_sidecar()?;
|
||||||
@ -171,7 +171,7 @@ impl Clash {
|
|||||||
&mut self,
|
&mut self,
|
||||||
patch: Mapping,
|
patch: Mapping,
|
||||||
verge: &mut Verge,
|
verge: &mut Verge,
|
||||||
profiles: &mut ProfilesConfig,
|
profiles: &mut Profiles,
|
||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
for (key, value) in patch.iter() {
|
for (key, value) in patch.iter() {
|
||||||
let value = value.clone();
|
let value = value.clone();
|
||||||
|
@ -11,7 +11,7 @@ use std::time::{SystemTime, UNIX_EPOCH};
|
|||||||
|
|
||||||
/// Define the `profiles.yaml` schema
|
/// Define the `profiles.yaml` schema
|
||||||
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
|
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
|
||||||
pub struct ProfilesConfig {
|
pub struct Profiles {
|
||||||
/// current profile's name
|
/// current profile's name
|
||||||
pub current: Option<usize>,
|
pub current: Option<usize>,
|
||||||
|
|
||||||
@ -63,10 +63,10 @@ pub struct ProfileResponse {
|
|||||||
static PROFILE_YAML: &str = "profiles.yaml";
|
static PROFILE_YAML: &str = "profiles.yaml";
|
||||||
static PROFILE_TEMP: &str = "clash-verge-runtime.yaml";
|
static PROFILE_TEMP: &str = "clash-verge-runtime.yaml";
|
||||||
|
|
||||||
impl ProfilesConfig {
|
impl Profiles {
|
||||||
/// read the config from the file
|
/// read the config from the file
|
||||||
pub fn read_file() -> Self {
|
pub fn read_file() -> Self {
|
||||||
config::read_yaml::<ProfilesConfig>(dirs::app_home_dir().join(PROFILE_YAML))
|
config::read_yaml::<Profiles>(dirs::app_home_dir().join(PROFILE_YAML))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// save the config to the file
|
/// save the config to the file
|
||||||
@ -99,7 +99,7 @@ impl ProfilesConfig {
|
|||||||
File::create(path).unwrap().write(file_data).unwrap();
|
File::create(path).unwrap().write(file_data).unwrap();
|
||||||
|
|
||||||
// update `profiles.yaml`
|
// update `profiles.yaml`
|
||||||
let data = ProfilesConfig::read_file();
|
let data = Profiles::read_file();
|
||||||
let mut items = data.items.unwrap_or(vec![]);
|
let mut items = data.items.unwrap_or(vec![]);
|
||||||
|
|
||||||
let now = SystemTime::now()
|
let now = SystemTime::now()
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
use crate::core::{Clash, ProfilesConfig, Verge};
|
use crate::core::{Clash, Profiles, Verge};
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct ProfilesState(pub Arc<Mutex<ProfilesConfig>>);
|
pub struct ProfilesState(pub Arc<Mutex<Profiles>>);
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct ClashState(pub Arc<Mutex<Clash>>);
|
pub struct ClashState(pub Arc<Mutex<Clash>>);
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use super::{init, server};
|
use super::{init, server};
|
||||||
use crate::{core::ProfilesConfig, states};
|
use crate::{core::Profiles, states};
|
||||||
use tauri::{App, AppHandle, Manager};
|
use tauri::{App, AppHandle, Manager};
|
||||||
use tauri_plugin_shadows::Shadows;
|
use tauri_plugin_shadows::Shadows;
|
||||||
|
|
||||||
@ -38,7 +38,7 @@ pub fn resolve_setup(app: &App) {
|
|||||||
log::error!("{}", err);
|
log::error!("{}", err);
|
||||||
}
|
}
|
||||||
|
|
||||||
*profiles = ProfilesConfig::read_file();
|
*profiles = Profiles::read_file();
|
||||||
if let Err(err) = profiles.activate(&clash) {
|
if let Err(err) = profiles.activate(&clash) {
|
||||||
log::error!("{}", err);
|
log::error!("{}", err);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user