Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix downloaded file notification popup #121

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/data/downloads/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,6 @@ impl Download {
matches!(self.state, DownloadState::Completed)
}

pub fn is_errored(&self) -> bool {
matches!(self.state, DownloadState::Errored(_))
}

pub fn get_progress(&self) -> f64 {
match self.state {
DownloadState::Downloading(progress) => progress,
Expand Down
45 changes: 22 additions & 23 deletions src/data/downloads/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use moxin_protocol::{
};
use std::{collections::HashMap, rc::Rc, sync::mpsc::channel};

#[derive(Debug)]
pub enum DownloadPendingNotification {
DownloadedFile(File),
DownloadErrored(File),
Expand All @@ -18,6 +19,7 @@ pub struct Downloads {
pub downloaded_files: Vec<DownloadedFile>,
pub pending_downloads: Vec<PendingDownload>,
pub current_downloads: HashMap<FileID, Download>,
pub pending_notifications: Vec<DownloadPendingNotification>,
}

impl Downloads {
Expand All @@ -27,6 +29,7 @@ impl Downloads {
downloaded_files: Vec::new(),
pending_downloads: Vec::new(),
current_downloads: HashMap::new(),
pending_notifications: Vec::new(),
}
}

Expand Down Expand Up @@ -159,25 +162,7 @@ impl Downloads {
}

pub fn next_download_notification(&mut self) -> Option<DownloadPendingNotification> {
self.current_downloads
.iter_mut()
.filter_map(|(_, download)| {
if download.must_show_notification() {
if download.is_errored() {
return Some(DownloadPendingNotification::DownloadErrored(
download.file.clone(),
));
} else if download.is_complete() {
return Some(DownloadPendingNotification::DownloadedFile(
download.file.clone(),
));
} else {
return None;
}
}
None
})
.next()
self.pending_notifications.pop()
}

pub fn get_model_and_file_for_pending_download(&self, file_id: &str) -> Option<(Model, File)> {
Expand All @@ -197,22 +182,36 @@ impl Downloads {
let mut completed_download_ids = Vec::new();

for (id, download) in &mut self.current_downloads {
download.process_download_progress();

if let Some(pending) = self
.pending_downloads
.iter_mut()
.find(|d| d.file.id == id.to_string())
{
match download.state {
DownloadState::Downloading(_) => {
pending.status = PendingDownloadsStatus::Downloading
pending.status = PendingDownloadsStatus::Downloading;
}
DownloadState::Errored(_) => {
pending.status = PendingDownloadsStatus::Error;
if download.must_show_notification() {
self.pending_notifications.push(DownloadPendingNotification::DownloadErrored(
download.file.clone()
));
}
}
DownloadState::Completed => {
if download.must_show_notification() {
self.pending_notifications.push(DownloadPendingNotification::DownloadedFile(
download.file.clone()
));
}
}
DownloadState::Errored(_) => pending.status = PendingDownloadsStatus::Error,
DownloadState::Completed => (),
};
pending.progress = download.get_progress();
}

download.process_download_progress();
if download.is_complete() {
completed_download_ids.push(id.clone());
}
Expand Down