From ee68d80d0a876dd9216b8248d8668e2bdfe28a3b Mon Sep 17 00:00:00 2001
From: GyDi <zzzgydi@gmail.com>
Date: Tue, 15 Nov 2022 01:33:50 +0800
Subject: [PATCH] refactor: wip

---
 src-tauri/src/config/config.rs | 20 ++++++++
 src-tauri/src/config/draft.rs  |  2 +-
 src-tauri/src/config/mod.rs    |  4 ++
 src-tauri/src/config/verge.rs  | 91 +++++++++++++++++++---------------
 4 files changed, 77 insertions(+), 40 deletions(-)
 create mode 100644 src-tauri/src/config/config.rs

diff --git a/src-tauri/src/config/config.rs b/src-tauri/src/config/config.rs
new file mode 100644
index 0000000..324a62f
--- /dev/null
+++ b/src-tauri/src/config/config.rs
@@ -0,0 +1,20 @@
+use super::{Draft, IVerge};
+use once_cell::sync::OnceCell;
+
+pub struct Config {
+    verge_config: Draft<IVerge>,
+}
+
+impl Config {
+    pub fn global() -> &'static Config {
+        static CONFIG: OnceCell<Config> = OnceCell::new();
+
+        CONFIG.get_or_init(|| Config {
+            verge_config: Draft::from(IVerge::new()),
+        })
+    }
+
+    pub fn verge() -> Draft<IVerge> {
+        Self::global().verge_config.clone()
+    }
+}
diff --git a/src-tauri/src/config/draft.rs b/src-tauri/src/config/draft.rs
index 4e96462..01f7bec 100644
--- a/src-tauri/src/config/draft.rs
+++ b/src-tauri/src/config/draft.rs
@@ -3,7 +3,7 @@ use parking_lot::{MappedMutexGuard, Mutex, MutexGuard};
 use serde_yaml::Mapping;
 use std::sync::Arc;
 
-#[derive(Debug)]
+#[derive(Debug, Clone)]
 pub struct Draft<T: Clone + ToOwned> {
     inner: Arc<Mutex<(T, Option<T>)>>,
 }
diff --git a/src-tauri/src/config/mod.rs b/src-tauri/src/config/mod.rs
index 9aa880f..554a4d4 100644
--- a/src-tauri/src/config/mod.rs
+++ b/src-tauri/src/config/mod.rs
@@ -1,9 +1,13 @@
 mod clash;
+mod config;
+mod draft;
 mod prfitem;
 mod profiles;
 mod verge;
 
 pub use self::clash::*;
+pub use self::config::*;
+pub use self::draft::*;
 pub use self::prfitem::*;
 pub use self::profiles::*;
 pub use self::verge::*;
diff --git a/src-tauri/src/config/verge.rs b/src-tauri/src/config/verge.rs
index 47593b7..9a2bc45 100644
--- a/src-tauri/src/config/verge.rs
+++ b/src-tauri/src/config/verge.rs
@@ -23,51 +23,15 @@ impl VergeN {
 
     /// Save IVerge App Config
     pub fn save_file(&self) -> Result<()> {
-        let config = self.config.lock();
-
-        config::save_yaml(
-            dirs::verge_path(),
-            &*config,
-            Some("# The Config for Clash IVerge App\n\n"),
-        )
+        self.config.lock().save_file()
     }
 
     /// patch verge config
     /// only save to file
     pub fn patch_config(&self, patch: IVerge) -> Result<()> {
-        let mut config = self.config.lock();
-
-        macro_rules! patch {
-            ($key: tt) => {
-                if patch.$key.is_some() {
-                    config.$key = patch.$key;
-                }
-            };
+        {
+            self.config.lock().patch_config(patch);
         }
-
-        patch!(language);
-        patch!(theme_mode);
-        patch!(theme_blur);
-        patch!(traffic_graph);
-
-        patch!(enable_tun_mode);
-        patch!(enable_service_mode);
-        patch!(enable_auto_launch);
-        patch!(enable_silent_start);
-        patch!(enable_system_proxy);
-        patch!(enable_proxy_guard);
-        patch!(system_proxy_bypass);
-        patch!(proxy_guard_duration);
-
-        patch!(theme_setting);
-        patch!(web_ui_list);
-        patch!(clash_core);
-        patch!(hotkeys);
-
-        patch!(auto_close_connection);
-        patch!(default_latency_test);
-
-        drop(config);
         self.save_file()
     }
 
@@ -165,3 +129,52 @@ pub struct IVergeTheme {
     pub font_family: Option<String>,
     pub css_injection: Option<String>,
 }
+
+impl IVerge {
+    pub fn new() -> Self {
+        config::read_yaml::<IVerge>(dirs::verge_path())
+    }
+
+    /// Save IVerge App Config
+    pub fn save_file(&self) -> Result<()> {
+        config::save_yaml(
+            dirs::verge_path(),
+            &self,
+            Some("# The Config for Clash IVerge App\n\n"),
+        )
+    }
+
+    /// patch verge config
+    /// only save to file
+    pub fn patch_config(&mut self, patch: IVerge) {
+        macro_rules! patch {
+            ($key: tt) => {
+                if patch.$key.is_some() {
+                    self.$key = patch.$key;
+                }
+            };
+        }
+
+        patch!(language);
+        patch!(theme_mode);
+        patch!(theme_blur);
+        patch!(traffic_graph);
+
+        patch!(enable_tun_mode);
+        patch!(enable_service_mode);
+        patch!(enable_auto_launch);
+        patch!(enable_silent_start);
+        patch!(enable_system_proxy);
+        patch!(enable_proxy_guard);
+        patch!(system_proxy_bypass);
+        patch!(proxy_guard_duration);
+
+        patch!(theme_setting);
+        patch!(web_ui_list);
+        patch!(clash_core);
+        patch!(hotkeys);
+
+        patch!(auto_close_connection);
+        patch!(default_latency_test);
+    }
+}
-- 
GitLab