mirror of
https://github.com/Drop-OSS/drop-app.git
synced 2025-11-13 16:22:43 +10:00
feat(library): automatically fetch remote data if not available
This commit is contained in:
@ -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=""
|
||||
/>
|
||||
|
||||
@ -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>,
|
||||
}
|
||||
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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]
|
||||
|
||||
Reference in New Issue
Block a user