chore: refactor UI to use Downloads struct
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use crate::models::{FileDisplayData, FileDownloadState};
|
||||
use crate::models::FileDisplayData;
|
||||
use ratatui::{layout::Rect, widgets::TableState};
|
||||
use soulseek_rs::DownloadStatus;
|
||||
use soulseek_rs::{types::Download, DownloadStatus};
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::{mpsc::Receiver, Arc};
|
||||
use std::time::Instant;
|
||||
@@ -22,7 +22,7 @@ pub struct SearchEntry {
|
||||
}
|
||||
|
||||
pub struct DownloadEntry {
|
||||
pub state: FileDownloadState,
|
||||
pub download: Download,
|
||||
pub receiver: Option<Receiver<DownloadStatus>>,
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ pub struct AppState {
|
||||
pub downloads: Vec<DownloadEntry>,
|
||||
pub downloads_table_state: TableState,
|
||||
pub downloads_receiver_channel:
|
||||
Option<Receiver<(usize, Receiver<DownloadStatus>)>>,
|
||||
Option<Receiver<(Download, Receiver<DownloadStatus>)>>,
|
||||
pub active_downloads_count: usize,
|
||||
|
||||
// UI State
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
use soulseek_rs::DownloadStatus;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FileDownloadState {
|
||||
pub filename: String,
|
||||
pub username: String,
|
||||
pub total_bytes: u64,
|
||||
pub bytes_downloaded: u64,
|
||||
pub speed_bytes_per_sec: f64,
|
||||
pub status: DownloadStatus,
|
||||
}
|
||||
|
||||
impl FileDownloadState {
|
||||
pub fn new(filename: String, username: String, total_bytes: u64) -> Self {
|
||||
Self {
|
||||
filename,
|
||||
username,
|
||||
total_bytes,
|
||||
bytes_downloaded: 0,
|
||||
speed_bytes_per_sec: 0.0,
|
||||
status: DownloadStatus::Queued,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_status(&mut self, status: DownloadStatus) {
|
||||
match &status {
|
||||
DownloadStatus::InProgress {
|
||||
bytes_downloaded,
|
||||
total_bytes,
|
||||
speed_bytes_per_sec,
|
||||
} => {
|
||||
self.bytes_downloaded = *bytes_downloaded;
|
||||
self.total_bytes = *total_bytes;
|
||||
self.speed_bytes_per_sec = *speed_bytes_per_sec;
|
||||
}
|
||||
DownloadStatus::Completed => {
|
||||
self.bytes_downloaded = self.total_bytes;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
self.status = status;
|
||||
}
|
||||
|
||||
pub fn is_finished(&self) -> bool {
|
||||
matches!(
|
||||
self.status,
|
||||
DownloadStatus::Completed
|
||||
| DownloadStatus::Failed
|
||||
| DownloadStatus::TimedOut
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,7 @@
|
||||
mod app_state;
|
||||
mod download_state;
|
||||
mod file_display_data;
|
||||
|
||||
pub use app_state::{
|
||||
AppState, DownloadEntry, FocusedPane, SearchEntry, SearchStatus,
|
||||
};
|
||||
pub use download_state::FileDownloadState;
|
||||
pub use file_display_data::FileDisplayData;
|
||||
|
||||
Reference in New Issue
Block a user