feat(library): automatically fetch remote data if not available

This commit is contained in:
DecDuck
2024-11-26 20:11:03 +11:00
parent 99c8b39a11
commit 2dedfbbd5c
4 changed files with 50 additions and 6 deletions

View File

@ -13,7 +13,7 @@
>
<div class="flex items-center min-w-0 gap-x-2">
<img
class="h-5 w-5 flex-none object-cover rounded-sm bg-zinc-900"
class="h-5 w-auto flex-none object-cover rounded-sm bg-zinc-900"
:src="icons[navIdx]"
alt=""
/>

View File

@ -35,6 +35,7 @@ pub enum DatabaseGameStatus {
#[serde(rename_all = "camelCase")]
pub struct DatabaseGames {
pub install_dirs: Vec<String>,
// Guaranteed to exist if the game also exists in the app state map
pub games_statuses: HashMap<String, DatabaseGameStatus>,
}

View File

@ -212,7 +212,7 @@ impl GameDownloadAgent {
}
pub fn run(&self) -> Result<(), ()> {
const DOWNLOAD_MAX_THREADS: usize = 4;
const DOWNLOAD_MAX_THREADS: usize = 1;
let pool = ThreadPoolBuilder::new()
.num_threads(DOWNLOAD_MAX_THREADS)

View File

@ -1,3 +1,4 @@
use std::fmt::format;
use std::sync::Mutex;
use log::info;
@ -8,6 +9,7 @@ use tauri::{AppHandle, Manager};
use crate::db;
use crate::db::DatabaseGameStatus;
use crate::db::DatabaseImpls;
use crate::downloads::download_manager::GameDownloadStatus;
use crate::remote::RemoteAccessError;
use crate::{auth::generate_authorization_header, AppState, DB};
@ -77,9 +79,9 @@ pub fn fetch_library(app: AppHandle) -> Result<String, String> {
fn fetch_game_logic(id: String, app: tauri::AppHandle) -> Result<String, RemoteAccessError> {
let state = app.state::<Mutex<AppState>>();
let handle = state.lock().unwrap();
let mut state_handle = state.lock().unwrap();
let game = handle.games.get(&id);
let game = state_handle.games.get(&id);
if let Some(game) = game {
let db_handle = DB.borrow_data().unwrap();
@ -95,9 +97,50 @@ fn fetch_game_logic(id: String, app: tauri::AppHandle) -> Result<String, RemoteA
return Ok(json!(data).to_string());
}
// TODO request games that aren't found from remote server
Err(RemoteAccessError::GameNotFound)
let base_url = DB.fetch_base_url();
let endpoint = base_url.join(&format!("/api/v1/game/{}", id))?;
let header = generate_authorization_header();
let client = reqwest::blocking::Client::new();
let response = client
.get(endpoint.to_string())
.header("Authorization", header)
.send()?;
if response.status() == 404 {
return Err(RemoteAccessError::GameNotFound);
}
if response.status() != 200 {
return Err(RemoteAccessError::InvalidCodeError(
response.status().into(),
));
}
let game = response.json::<Game>()?;
state_handle.games.insert(id.clone(), game.clone());
let mut db_handle = DB.borrow_data_mut().unwrap();
if !db_handle.games.games_statuses.contains_key(&id) {
db_handle
.games
.games_statuses
.insert(id, DatabaseGameStatus::Remote);
}
let data = FetchGameStruct {
game: game.clone(),
status: db_handle
.games
.games_statuses
.get(&game.id)
.unwrap()
.clone(),
};
return Ok(json!(data).to_string());
}
#[tauri::command]