chore: refactor UI to use Downloads struct

This commit is contained in:
Michel de Graaf
2025-11-18 14:33:33 +01:00
parent 79da19ed0f
commit 4c6d7c6850
8 changed files with 116 additions and 189 deletions
+4 -4
View File
@@ -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
-52
View File
@@ -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
)
}
}
-2
View File
@@ -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;