Skip to content

Commit 5d22b88

Browse files
committed
refactor: Improvements to src-tauri
Signed-off-by: quexeky <[email protected]>
1 parent 62a2561 commit 5d22b88

File tree

16 files changed

+134
-184
lines changed

16 files changed

+134
-184
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/src/autostart.rs

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,8 @@
1-
use database::{borrow_db_checked, borrow_db_mut_checked};
1+
use database::borrow_db_checked;
22
use log::debug;
33
use tauri::AppHandle;
44
use tauri_plugin_autostart::ManagerExt;
55

6-
pub fn toggle_autostart_logic(app: AppHandle, enabled: bool) -> Result<(), String> {
7-
let manager = app.autolaunch();
8-
if enabled {
9-
manager.enable().map_err(|e| e.to_string())?;
10-
debug!("enabled autostart");
11-
} else {
12-
manager.disable().map_err(|e| e.to_string())?;
13-
debug!("eisabled autostart");
14-
}
15-
16-
// Store the state in DB
17-
let mut db_handle = borrow_db_mut_checked();
18-
db_handle.settings.autostart = enabled;
19-
drop(db_handle);
20-
21-
Ok(())
22-
}
23-
24-
pub fn get_autostart_enabled_logic(app: AppHandle) -> Result<bool, tauri_plugin_autostart::Error> {
25-
// First check DB state
26-
let db_handle = borrow_db_checked();
27-
let db_state = db_handle.settings.autostart;
28-
drop(db_handle);
29-
30-
// Get actual system state
31-
let manager = app.autolaunch();
32-
let system_state = manager.is_enabled()?;
33-
34-
// If they don't match, sync to DB state
35-
if db_state != system_state {
36-
if db_state {
37-
manager.enable()?;
38-
} else {
39-
manager.disable()?;
40-
}
41-
}
42-
43-
Ok(db_state)
44-
}
45-
466
// New function to sync state on startup
477
pub fn sync_autostart_on_startup(app: &AppHandle) -> Result<(), String> {
488
let db_handle = borrow_db_checked();

download_manager/src/frontend_updates.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct QueueUpdateEvent {
1717
pub queue: Vec<QueueUpdateEventQueueData>,
1818
}
1919

20-
#[derive(serde::Serialize, Clone)]
20+
#[derive(Serialize, Clone)]
2121
pub struct StatsUpdateEvent {
2222
pub speed: usize,
2323
pub time: usize,

download_manager/src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
#![feature(duration_millis_float)]
2+
#![feature(nonpoison_mutex)]
3+
#![feature(sync_nonpoison)]
4+
5+
use std::sync::{nonpoison::Mutex, LazyLock};
6+
7+
use crate::{download_manager_builder::DownloadManagerBuilder, download_manager_frontend::DownloadManager};
28

39
pub mod download_manager_builder;
410
pub mod download_manager_frontend;
511
pub mod downloadable;
612
pub mod util;
713
pub mod error;
8-
pub mod frontend_updates;
14+
pub mod frontend_updates;
15+
16+
pub static DOWNLOAD_MANAGER: LazyLock<Mutex<DownloadManager>> = LazyLock::new(|| todo!());

games/src/library.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ pub struct FetchGameStruct {
1818
version: Option<GameVersion>,
1919
}
2020

21+
impl FetchGameStruct {
22+
pub fn new(game: Game, status: GameStatusWithTransient, version: Option<GameVersion>) -> Self {
23+
Self { game, status, version }
24+
}
25+
}
26+
2127
#[derive(Serialize, Deserialize, Clone, Debug, Default, Encode, Decode)]
2228
#[serde(rename_all = "camelCase")]
2329
pub struct Game {
@@ -33,6 +39,11 @@ pub struct Game {
3339
m_image_library_object_ids: Vec<String>,
3440
m_image_carousel_object_ids: Vec<String>,
3541
}
42+
impl Game {
43+
pub fn id(&self) -> &String {
44+
&self.id
45+
}
46+
}
3647
#[derive(serde::Serialize, Clone)]
3748
pub struct GameUpdateEvent {
3849
pub game_id: String,

process/src/process_manager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl ProcessManager<'_> {
100100
}
101101
}
102102

103-
fn get_log_dir(&self, game_id: String) -> PathBuf {
103+
pub fn get_log_dir(&self, game_id: String) -> PathBuf {
104104
self.log_output_dir.join(game_id)
105105
}
106106

remote/src/cache.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@ use std::{
66
};
77

88
use bitcode::{Decode, DecodeOwned, Encode};
9-
use database::{borrow_db_checked, Database};
10-
use http::{header::{CONTENT_TYPE}, response::Builder as ResponseBuilder, Response};
9+
use database::{Database, borrow_db_checked};
10+
use http::{Response, header::CONTENT_TYPE, response::Builder as ResponseBuilder};
1111

1212
use crate::error::{CacheError, RemoteAccessError};
1313

1414
#[macro_export]
1515
macro_rules! offline {
1616
($var:expr, $func1:expr, $func2:expr, $( $arg:expr ),* ) => {
1717

18-
async move { if $crate::borrow_db_checked().settings.force_offline || $crate::lock!($var).status == $crate::AppStatus::Offline {
18+
async move {
19+
if ::database::borrow_db_checked().settings.force_offline
20+
|| ::utils::lock!($var).status == ::client::app_status::AppStatus::Offline {
1921
$func2( $( $arg ), *).await
2022
} else {
2123
$func1( $( $arg ), *).await
@@ -81,10 +83,7 @@ pub fn get_cached_object_db<D: DecodeOwned>(
8183
pub fn clear_cached_object(key: &str) -> Result<(), RemoteAccessError> {
8284
clear_cached_object_db(key, &borrow_db_checked())
8385
}
84-
pub fn clear_cached_object_db(
85-
key: &str,
86-
db: &Database,
87-
) -> Result<(), RemoteAccessError> {
86+
pub fn clear_cached_object_db(key: &str, db: &Database) -> Result<(), RemoteAccessError> {
8887
delete_sync(&db.cache_dir, key).map_err(RemoteAccessError::Cache)?;
8988
Ok(())
9089
}
@@ -103,9 +102,9 @@ impl ObjectCache {
103102
}
104103
}
105104

106-
impl TryFrom<Response<Vec<u8>>> for ObjectCache {
105+
impl TryFrom<Response<Vec<u8>>> for ObjectCache {
107106
type Error = CacheError;
108-
107+
109108
fn try_from(value: Response<Vec<u8>>) -> Result<Self, Self::Error> {
110109
Ok(ObjectCache {
111110
content_type: value
@@ -118,21 +117,24 @@ impl TryFrom<Response<Vec<u8>>> for ObjectCache {
118117
body: value.body().clone(),
119118
expiry: get_sys_time_in_secs() + 60 * 60 * 24,
120119
})
121-
122120
}
123121
}
124122
impl TryFrom<ObjectCache> for Response<Vec<u8>> {
125123
type Error = CacheError;
126124
fn try_from(value: ObjectCache) -> Result<Self, Self::Error> {
127125
let resp_builder = ResponseBuilder::new().header(CONTENT_TYPE, value.content_type);
128-
resp_builder.body(value.body).map_err(CacheError::ConstructionError)
126+
resp_builder
127+
.body(value.body)
128+
.map_err(CacheError::ConstructionError)
129129
}
130130
}
131131
impl TryFrom<&ObjectCache> for Response<Vec<u8>> {
132132
type Error = CacheError;
133133

134134
fn try_from(value: &ObjectCache) -> Result<Self, Self::Error> {
135135
let resp_builder = ResponseBuilder::new().header(CONTENT_TYPE, value.content_type.clone());
136-
resp_builder.body(value.body.clone()).map_err(CacheError::ConstructionError)
136+
resp_builder
137+
.body(value.body.clone())
138+
.map_err(CacheError::ConstructionError)
137139
}
138140
}

remote/src/utils.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ use serde::Deserialize;
1111

1212
#[derive(Deserialize)]
1313
#[serde(rename_all = "camelCase")]
14-
struct DropHealthcheck {
14+
pub struct DropHealthcheck {
1515
app_name: String,
1616
}
17-
17+
impl DropHealthcheck {
18+
pub fn app_name(&self) -> &String{
19+
&self.app_name
20+
}
21+
}
1822
static DROP_CERT_BUNDLE: LazyLock<Vec<Certificate>> = LazyLock::new(fetch_certificates);
1923
pub static DROP_CLIENT_SYNC: LazyLock<reqwest::blocking::Client> = LazyLock::new(get_client_sync);
2024
pub static DROP_CLIENT_ASYNC: LazyLock<reqwest::Client> = LazyLock::new(get_client_async);

src-tauri/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,12 @@ bytes = "1.10.1"
8080

8181

8282
# Workspaces
83-
client = { path = "../client" }
83+
client = { version = "0.1.0", path = "../client" }
8484
database = { path = "../database" }
8585
process = { path = "../process" }
86-
remote = { path = "../remote" }
86+
remote = { version = "0.1.0", path = "../remote" }
8787
utils = { path = "../utils" }
88+
games = { version = "0.1.0", path = "../games" }
8889

8990
[dependencies.dynfmt]
9091
version = "0.1.5"

src-tauri/src/client.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
use crate::{lock, AppState};
1+
use database::{borrow_db_checked, borrow_db_mut_checked};
2+
use log::{debug, error};
3+
use tauri::AppHandle;
4+
use tauri_plugin_autostart::ManagerExt;
5+
use utils::lock;
6+
7+
use crate::{AppState};
28

39
#[tauri::command]
410
pub fn fetch_state(
@@ -31,10 +37,39 @@ pub fn cleanup_and_exit(app: &AppHandle, state: &tauri::State<'_, std::sync::Mut
3137

3238
#[tauri::command]
3339
pub fn toggle_autostart(app: AppHandle, enabled: bool) -> Result<(), String> {
34-
toggle_autostart_logic(app, enabled)
40+
let manager = app.autolaunch();
41+
if enabled {
42+
manager.enable().map_err(|e| e.to_string())?;
43+
debug!("enabled autostart");
44+
} else {
45+
manager.disable().map_err(|e| e.to_string())?;
46+
debug!("eisabled autostart");
47+
}
48+
49+
// Store the state in DB
50+
let mut db_handle = borrow_db_mut_checked();
51+
db_handle.settings.autostart = enabled;
52+
Ok(())
3553
}
3654

3755
#[tauri::command]
3856
pub fn get_autostart_enabled(app: AppHandle) -> Result<bool, tauri_plugin_autostart::Error> {
39-
get_autostart_enabled_logic(app)
57+
let db_handle = borrow_db_checked();
58+
let db_state = db_handle.settings.autostart;
59+
drop(db_handle);
60+
61+
// Get actual system state
62+
let manager = app.autolaunch();
63+
let system_state = manager.is_enabled()?;
64+
65+
// If they don't match, sync to DB state
66+
if db_state != system_state {
67+
if db_state {
68+
manager.enable()?;
69+
} else {
70+
manager.disable()?;
71+
}
72+
}
73+
74+
Ok(db_state)
4075
}

0 commit comments

Comments
 (0)