Skip to content

Commit

Permalink
add download window
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDavenport committed Apr 24, 2024
1 parent 11d77a4 commit 3afa862
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 4 deletions.
44 changes: 44 additions & 0 deletions gamercade_app/src/modes/arcade_mode/download_window.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use eframe::egui::Window;
use tokio::task::block_in_place;

use crate::{app::AppDrawContext, task_manager::DownloadStatus};

#[derive(Default)]
pub struct DownloadWindow {
pub open: bool,
}

impl DownloadWindow {
pub fn draw(&mut self, context: &mut AppDrawContext) {
Window::new("Downloads")
.open(&mut self.open)
.collapsible(false)
.resizable(false)
.show(context.ui.ctx(), |ui| {
ui.spinner();
block_in_place(|| {
let lock = context.task_manager.http.state.blocking_lock();

lock.rom_downloads.iter().for_each(|(key, value)| {
match value.download_status {
DownloadStatus::Starting => {
ui.label(format!("{key}: Starting..."));
}
DownloadStatus::InProgress {
bytes_downloaded,
total_bytes,
} => {
ui.label(format!(
"{key}: {}",
bytes_downloaded as f32 / total_bytes as f32
));
}
DownloadStatus::Done(_) => {
ui.label(format!("{key}: Done"));
}
}
});
});
});
}
}
1 change: 1 addition & 0 deletions gamercade_app/src/modes/arcade_mode/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::app::AppDrawContext;

mod creator_dashboard;
mod download_window;
mod login;
mod manage_game;
mod new_game;
Expand Down
15 changes: 14 additions & 1 deletion gamercade_app/src/modes/arcade_mode/online.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ use gamercade_interface::platform::FrontPageResponse;

use crate::app::AppDrawContext;

use super::{ArcadeActiveView, CreatorDashboardView};
use super::{download_window::DownloadWindow, ArcadeActiveView, CreatorDashboardView};

#[derive(Default)]
pub struct OnlineView {
active_mode: OnlineViewMode,

pub arcade: ArcadeView,
pub dashboard: CreatorDashboardView,
pub download_window: DownloadWindow,
}

#[derive(Default)]
Expand All @@ -30,6 +31,7 @@ enum ArcadeTab {
impl ArcadeView {
fn draw(&mut self, context: &mut AppDrawContext) {
let ui = &mut context.ui;

if let Some(front_page) = &self.front_page {
ui.horizontal(|ui| {
ui.selectable_value(&mut self.tab, ArcadeTab::Popular, "Popular Games");
Expand Down Expand Up @@ -102,13 +104,24 @@ impl OnlineView {
pub fn draw(&mut self, ctx: &mut AppDrawContext) -> Option<ArcadeActiveView> {
ctx.ui.label("Online View");

self.download_window.draw(ctx);

ctx.ui.horizontal(|ui| {
ui.selectable_value(&mut self.active_mode, OnlineViewMode::Arcade, "Arcade");
ui.selectable_value(
&mut self.active_mode,
OnlineViewMode::CreatorDashboard,
"Creator Dashboard",
);

ui.separator();

if ui
.selectable_label(self.download_window.open, "Downloads")
.clicked()
{
self.download_window.open = !self.download_window.open;
};
});

match self.active_mode {
Expand Down
19 changes: 16 additions & 3 deletions gamercade_app/src/task_manager/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ pub struct HttpManagerState {
}

pub struct ActiveDownload {
id: i64,
download_status: DownloadStatus,
pub id: i64,
pub download_status: DownloadStatus,
}

pub enum DownloadStatus {
Starting,
InProgress {
bytes_downloaded: usize,
total_bytes: usize,
Expand Down Expand Up @@ -69,7 +70,19 @@ impl TaskRequest<HttpManagerState> for HttpRequest {
) {
match self {
HttpRequest::DownloadRom(request) => {
download_file(sender.clone(), state.clone(), request)
let mut lock = state.lock().await;
if !lock.rom_downloads.contains_key(&request.data.game_id) {
lock.rom_downloads.insert(
request.data.game_id,
ActiveDownload {
id: request.data.game_id,
download_status: DownloadStatus::Starting,
},
);

drop(lock);
download_file(sender.clone(), state.clone(), request);
}
}
HttpRequest::UploadRom(request) => {
let WithSession {
Expand Down

0 comments on commit 3afa862

Please sign in to comment.