fix: offline game status, user widget and use binary-encoding

This commit is contained in:
DecDuck
2025-04-04 11:07:10 +11:00
parent 77251a6524
commit 1fdf569278
3 changed files with 18 additions and 8 deletions

View File

@ -2,7 +2,7 @@ use std::fs::remove_dir_all;
use std::sync::Mutex;
use std::thread::spawn;
use log::{debug, error, warn};
use log::{debug, error, info, warn};
use serde::{Deserialize, Serialize};
use tauri::Emitter;
use tauri::{AppHandle, Manager};
@ -137,7 +137,7 @@ pub fn fetch_game_logic(
status,
};
cache_object(id, &data)?;
cache_object(id, game)?;
return Ok(data);
}
@ -175,7 +175,7 @@ pub fn fetch_game_logic(
status,
};
cache_object(id, &data)?;
cache_object(id, &game)?;
Ok(data)
}
@ -184,7 +184,9 @@ pub fn fetch_game_logic_offline(
id: String,
_state: tauri::State<'_, Mutex<AppState>>,
) -> Result<FetchGameStruct, RemoteAccessError> {
get_cached_object::<String, FetchGameStruct>(id)
let status = GameStatusManager::fetch_state(&id);
let game = get_cached_object::<String, Game>(id)?;
Ok(FetchGameStruct { game, status })
}
pub fn fetch_game_verion_options_logic(

View File

@ -17,7 +17,10 @@ use crate::{
AppState, AppStatus, User, DB,
};
use super::requests::make_request;
use super::{
cache::{cache_object, get_cached_object},
requests::make_request,
};
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
@ -198,9 +201,13 @@ pub fn setup() -> (AppStatus, Option<User>) {
if auth.is_some() {
let user_result = match fetch_user() {
Ok(data) => data,
Err(RemoteAccessError::FetchError(_)) => return (AppStatus::Offline, None),
Err(RemoteAccessError::FetchError(_)) => {
let user = get_cached_object::<String, User>("user".to_owned()).unwrap();
return (AppStatus::Offline, Some(user));
}
Err(_) => return (AppStatus::SignedInNeedsReauth, None),
};
cache_object("user", &user_result).unwrap();
return (AppStatus::SignedIn, Some(user_result));
}

View File

@ -2,6 +2,7 @@ use crate::{database::db::borrow_db_checked, error::remote_access_error::RemoteA
use cacache::Integrity;
use http::{header::CONTENT_TYPE, response::Builder as ResponseBuilder, Response};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use serde_binary::binary_stream::Endian;
use tauri::{UriSchemeContext, UriSchemeResponder};
use super::{auth::generate_authorization_header, requests::make_request};
@ -22,7 +23,7 @@ pub fn cache_object<'a, K: AsRef<str>, D: Serialize + DeserializeOwned>(
key: K,
data: &D,
) -> Result<Integrity, RemoteAccessError> {
let bytes = serde_json::to_vec(data).unwrap();
let bytes = serde_binary::to_vec(data, Endian::Little).unwrap();
cacache::write_sync(&borrow_db_checked().cache_dir, key, bytes)
.map_err(|e| RemoteAccessError::Cache(e))
}
@ -31,7 +32,7 @@ pub fn get_cached_object<'a, K: AsRef<str>, D: Serialize + DeserializeOwned>(
) -> Result<D, RemoteAccessError> {
let bytes = cacache::read_sync(&borrow_db_checked().cache_dir, key)
.map_err(|e| RemoteAccessError::Cache(e))?;
let data = serde_json::from_slice::<D>(&bytes).unwrap();
let data = serde_binary::from_slice::<D>(&bytes, Endian::Little).unwrap();
Ok(data)
}