Skip to content

Commit a44abff

Browse files
committed
Add a basic mod details view
1 parent 504c252 commit a44abff

File tree

5 files changed

+278
-20
lines changed

5 files changed

+278
-20
lines changed

Cargo.lock

Lines changed: 19 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ egui_commonmark = "0.7.4"
1616
egui_dnd = { git = "https://github.com/lucasmerlin/egui_dnd.git" }
1717
futures = "0.3.28"
1818
hex = "0.4.3"
19-
image = { version = "0.24.7", default-features = false, features = ["png"] }
19+
image = { version = "0.24.7", default-features = false, features = ["png", "jpeg"] }
2020
indexmap = { version = "2.0.0", features = ["serde"] }
2121
inventory = "0.3.11"
2222
lazy_static = "1.4.0"

src/gui/message.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pub enum Message {
4646
LintMods(LintMods),
4747
SelfUpdate(SelfUpdate),
4848
FetchSelfUpdateProgress(FetchSelfUpdateProgress),
49+
FetchModDetails(FetchModDetails),
4950
}
5051

5152
impl Message {
@@ -59,6 +60,7 @@ impl Message {
5960
Self::LintMods(msg) => msg.receive(app),
6061
Self::SelfUpdate(msg) => msg.receive(app),
6162
Self::FetchSelfUpdateProgress(msg) => msg.receive(app),
63+
Self::FetchModDetails(msg) => msg.receive(app),
6264
}
6365
}
6466
}
@@ -712,3 +714,94 @@ async fn self_update_async(
712714

713715
Ok(original_exe_path)
714716
}
717+
718+
#[derive(Debug)]
719+
pub struct FetchModDetails {
720+
rid: RequestID,
721+
result: Result<ModDetails>,
722+
}
723+
724+
#[derive(Debug)]
725+
pub struct ModDetails {
726+
pub r#mod: modio::mods::Mod,
727+
pub versions: Vec<modio::files::File>,
728+
pub thumbnail: Vec<u8>,
729+
}
730+
731+
impl FetchModDetails {
732+
pub fn send(
733+
rc: &mut RequestCounter,
734+
ctx: &egui::Context,
735+
tx: Sender<Message>,
736+
oauth_token: &str,
737+
modio_id: u32,
738+
) -> MessageHandle<()> {
739+
let rid = rc.next();
740+
let ctx = ctx.clone();
741+
let oauth_token = oauth_token.to_string();
742+
743+
MessageHandle {
744+
rid,
745+
handle: tokio::task::spawn(async move {
746+
let result = fetch_modio_mod_details(oauth_token, modio_id).await;
747+
tx.send(Message::FetchModDetails(FetchModDetails { rid, result }))
748+
.await
749+
.unwrap();
750+
ctx.request_repaint();
751+
}),
752+
state: (),
753+
}
754+
}
755+
756+
fn receive(self, app: &mut App) {
757+
if Some(self.rid) == app.fetch_mod_details_rid.as_ref().map(|r| r.rid) {
758+
match self.result {
759+
Ok(mod_details) => {
760+
info!("fetch mod details successful");
761+
app.mod_details = Some(mod_details);
762+
app.last_action_status =
763+
LastActionStatus::Success("fetch mod details complete".to_string());
764+
}
765+
Err(e) => {
766+
error!("fetch mod details failed");
767+
error!("{:#?}", e);
768+
app.mod_details = None;
769+
app.fetch_mod_details_rid = None;
770+
app.last_action_status =
771+
LastActionStatus::Failure("fetch mod details failed".to_string());
772+
}
773+
}
774+
app.integrate_rid = None;
775+
}
776+
}
777+
}
778+
779+
async fn fetch_modio_mod_details(oauth_token: String, modio_id: u32) -> Result<ModDetails> {
780+
use crate::providers::modio::{LoggingMiddleware, MODIO_DRG_ID};
781+
use modio::{filter::prelude::*, Credentials, Modio};
782+
783+
let credentials = Credentials::with_token("", oauth_token);
784+
let client = reqwest_middleware::ClientBuilder::new(reqwest::Client::new())
785+
.with::<LoggingMiddleware>(Default::default())
786+
.build();
787+
let modio = Modio::new(credentials, client.clone())?;
788+
let mod_ref = modio.mod_(MODIO_DRG_ID, modio_id);
789+
let r#mod = mod_ref.clone().get().await?;
790+
791+
let filter = with_limit(10).order_by(modio::user::filters::files::Version::desc());
792+
let versions = mod_ref.clone().files().search(filter).first_page().await?;
793+
794+
let thumbnail = client
795+
.get(r#mod.logo.thumb_320x180.clone())
796+
.send()
797+
.await?
798+
.bytes()
799+
.await?
800+
.to_vec();
801+
802+
Ok(ModDetails {
803+
r#mod,
804+
versions,
805+
thumbnail,
806+
})
807+
}

0 commit comments

Comments
 (0)