Skip to content

Commit

Permalink
Add a basic mod details view
Browse files Browse the repository at this point in the history
  • Loading branch information
jieyouxu committed Aug 21, 2023
1 parent 504c252 commit a44abff
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 20 deletions.
31 changes: 19 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ egui_commonmark = "0.7.4"
egui_dnd = { git = "https://github.com/lucasmerlin/egui_dnd.git" }
futures = "0.3.28"
hex = "0.4.3"
image = { version = "0.24.7", default-features = false, features = ["png"] }
image = { version = "0.24.7", default-features = false, features = ["png", "jpeg"] }
indexmap = { version = "2.0.0", features = ["serde"] }
inventory = "0.3.11"
lazy_static = "1.4.0"
Expand Down
93 changes: 93 additions & 0 deletions src/gui/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub enum Message {
LintMods(LintMods),
SelfUpdate(SelfUpdate),
FetchSelfUpdateProgress(FetchSelfUpdateProgress),
FetchModDetails(FetchModDetails),
}

impl Message {
Expand All @@ -59,6 +60,7 @@ impl Message {
Self::LintMods(msg) => msg.receive(app),
Self::SelfUpdate(msg) => msg.receive(app),
Self::FetchSelfUpdateProgress(msg) => msg.receive(app),
Self::FetchModDetails(msg) => msg.receive(app),
}
}
}
Expand Down Expand Up @@ -712,3 +714,94 @@ async fn self_update_async(

Ok(original_exe_path)
}

#[derive(Debug)]
pub struct FetchModDetails {
rid: RequestID,
result: Result<ModDetails>,
}

#[derive(Debug)]
pub struct ModDetails {
pub r#mod: modio::mods::Mod,
pub versions: Vec<modio::files::File>,
pub thumbnail: Vec<u8>,
}

impl FetchModDetails {
pub fn send(
rc: &mut RequestCounter,
ctx: &egui::Context,
tx: Sender<Message>,
oauth_token: &str,
modio_id: u32,
) -> MessageHandle<()> {
let rid = rc.next();
let ctx = ctx.clone();
let oauth_token = oauth_token.to_string();

MessageHandle {
rid,
handle: tokio::task::spawn(async move {
let result = fetch_modio_mod_details(oauth_token, modio_id).await;
tx.send(Message::FetchModDetails(FetchModDetails { rid, result }))
.await
.unwrap();
ctx.request_repaint();
}),
state: (),
}
}

fn receive(self, app: &mut App) {
if Some(self.rid) == app.fetch_mod_details_rid.as_ref().map(|r| r.rid) {
match self.result {
Ok(mod_details) => {
info!("fetch mod details successful");
app.mod_details = Some(mod_details);
app.last_action_status =
LastActionStatus::Success("fetch mod details complete".to_string());
}
Err(e) => {
error!("fetch mod details failed");
error!("{:#?}", e);
app.mod_details = None;
app.fetch_mod_details_rid = None;
app.last_action_status =
LastActionStatus::Failure("fetch mod details failed".to_string());
}
}
app.integrate_rid = None;
}
}
}

async fn fetch_modio_mod_details(oauth_token: String, modio_id: u32) -> Result<ModDetails> {
use crate::providers::modio::{LoggingMiddleware, MODIO_DRG_ID};
use modio::{filter::prelude::*, Credentials, Modio};

let credentials = Credentials::with_token("", oauth_token);
let client = reqwest_middleware::ClientBuilder::new(reqwest::Client::new())
.with::<LoggingMiddleware>(Default::default())
.build();
let modio = Modio::new(credentials, client.clone())?;
let mod_ref = modio.mod_(MODIO_DRG_ID, modio_id);
let r#mod = mod_ref.clone().get().await?;

let filter = with_limit(10).order_by(modio::user::filters::files::Version::desc());
let versions = mod_ref.clone().files().search(filter).first_page().await?;

let thumbnail = client
.get(r#mod.logo.thumb_320x180.clone())
.send()
.await?
.bytes()
.await?
.to_vec();

Ok(ModDetails {
r#mod,
versions,
thumbnail,
})
}
Loading

0 comments on commit a44abff

Please sign in to comment.