feat(cache): Added forceOffline in settings and caching games & library

This commit is contained in:
quexeky
2025-01-31 13:01:41 +11:00
parent f33ca95bdf
commit 2a1a7326d0
13 changed files with 132 additions and 130 deletions

View File

@ -3,7 +3,7 @@ use std::sync::Mutex;
use tauri::{AppHandle, Manager};
use crate::{
database::db::GameVersion, error::{library_error::LibraryError, remote_access_error::RemoteAccessError}, games::library::{get_current_meta, uninstall_game_logic}, offline, AppState
database::db::GameVersion, error::{library_error::LibraryError, remote_access_error::RemoteAccessError}, games::library::{fetch_game_logic_offline, fetch_library_logic_offline, get_current_meta, uninstall_game_logic}, offline, AppState
};
use super::{
@ -15,17 +15,16 @@ use super::{
};
#[tauri::command]
pub fn fetch_library(app: AppHandle) -> Result<Vec<Game>, RemoteAccessError> {
let state = app.state::<Mutex<AppState>>();
offline!(state, fetch_library_logic, fetch_library_logic, app)
pub fn fetch_library(state: tauri::State<'_, Mutex<AppState>>) -> Result<Vec<Game>, RemoteAccessError> {
offline!(state, fetch_library_logic, fetch_library_logic_offline, state)
}
#[tauri::command]
pub fn fetch_game(
game_id: String,
app: tauri::AppHandle,
state: tauri::State<'_, Mutex<AppState>>
) -> Result<FetchGameStruct, RemoteAccessError> {
fetch_game_logic(game_id, app)
offline!(state, fetch_game_logic, fetch_game_logic_offline, game_id, state)
}
#[tauri::command]

View File

@ -362,7 +362,7 @@ impl Downloadable for GameDownloadAgent {
*self.status.lock().unwrap() = DownloadStatus::Queued;
}
fn on_error(&self, app_handle: &tauri::AppHandle, error: ApplicationDownloadError) {
fn on_error(&self, app_handle: &tauri::AppHandle, error: &ApplicationDownloadError) {
*self.status.lock().unwrap() = DownloadStatus::Error;
app_handle
.emit("download_error", error.to_string())

View File

@ -14,10 +14,11 @@ use crate::download_manager::downloadable_metadata::DownloadableMetadata;
use crate::error::remote_access_error::RemoteAccessError;
use crate::games::state::{GameStatusManager, GameStatusWithTransient};
use crate::remote::auth::generate_authorization_header;
use crate::remote::cache::{cache_object, get_cached_object};
use crate::remote::requests::make_request;
use crate::AppState;
#[derive(serde::Serialize)]
#[derive(Serialize, Deserialize)]
pub struct FetchGameStruct {
game: Game,
status: GameStatusWithTransient,
@ -66,10 +67,11 @@ pub struct StatsUpdateEvent {
pub time: usize,
}
pub fn fetch_library_logic(app: AppHandle) -> Result<Vec<Game>, RemoteAccessError> {
pub fn fetch_library_logic(state: tauri::State<'_, Mutex<AppState>>) -> Result<Vec<Game>, RemoteAccessError> {
let header = generate_authorization_header();
let client = reqwest::blocking::Client::new();
println!("Making library request");
let response = make_request(&client, &["/api/v1/client/user/library"], &[], |f| {
f.header("Authorization", header)
})?
@ -80,10 +82,10 @@ pub fn fetch_library_logic(app: AppHandle) -> Result<Vec<Game>, RemoteAccessErro
warn!("{:?}", err);
return Err(RemoteAccessError::InvalidResponse(err));
}
println!("Getting Games");
let games: Vec<Game> = response.json()?;
let state = app.state::<Mutex<AppState>>();
let mut handle = state.lock().unwrap();
let mut db_handle = borrow_db_mut_checked();
@ -97,17 +99,30 @@ pub fn fetch_library_logic(app: AppHandle) -> Result<Vec<Game>, RemoteAccessErro
.insert(game.id.clone(), GameDownloadStatus::Remote {});
}
}
drop(handle);
drop(db_handle);
println!("Caching");
cache_object("library", &games)?;
println!("Finished caching");
Ok(games)
}
pub fn fetch_library_logic_offline(_state: tauri::State<'_, Mutex<AppState>>) -> Result<Vec<Game>, RemoteAccessError> {
let mut games: Vec<Game> = get_cached_object("library")?;
let db_handle = borrow_db_checked();
games.retain(|game| {
db_handle.applications.installed_game_version.contains_key(&game.id)
});
Ok(games)
}
pub fn fetch_game_logic(
id: String,
app: tauri::AppHandle,
state: tauri::State<'_, Mutex<AppState>>
) -> Result<FetchGameStruct, RemoteAccessError> {
let state = app.state::<Mutex<AppState>>();
let mut state_handle = state.lock().unwrap();
let game = state_handle.games.get(&id);
@ -155,9 +170,18 @@ pub fn fetch_game_logic(
status,
};
cache_object(id, &data)?;
Ok(data)
}
pub fn fetch_game_logic_offline(
id: String,
_state: tauri::State<'_, Mutex<AppState>>
) -> Result<FetchGameStruct, RemoteAccessError> {
get_cached_object(id)
}
pub fn fetch_game_verion_options_logic(
game_id: String,
state: tauri::State<'_, Mutex<AppState>>,