Move progress into controller to make it not block

This commit is contained in:
Jeremy Soller 2024-11-20 08:15:31 -07:00
parent c4a08ead84
commit b4b5e78abe
No known key found for this signature in database
GPG key ID: D02FD439211AF56F
4 changed files with 176 additions and 272 deletions

View file

@ -1,35 +1,23 @@
use cosmic::iced::futures::{channel::mpsc::Sender, executor, SinkExt};
use std::{fs, io, path::Path, sync::Arc};
use tokio::sync::Mutex;
use std::{fs, io, path::Path};
use super::Controller;
use crate::app::Message;
// Special reader just for operations, handling cancel and progress
pub struct OpReader {
file: fs::File,
metadata: fs::Metadata,
current: u64,
id: u64,
msg_tx: Arc<Mutex<Sender<Message>>>,
controller: Controller,
}
impl OpReader {
pub fn new<P: AsRef<Path>>(
path: P,
id: u64,
msg_tx: Arc<Mutex<Sender<Message>>>,
controller: Controller,
) -> io::Result<Self> {
pub fn new<P: AsRef<Path>>(path: P, controller: Controller) -> io::Result<Self> {
let file = fs::File::open(&path)?;
let metadata = file.metadata()?;
Ok(Self {
file,
metadata,
current: 0,
id,
msg_tx,
controller,
})
}
@ -45,14 +33,7 @@ impl io::Read for OpReader {
self.current += count as u64;
let progress = self.current as f32 / self.metadata.len() as f32;
executor::block_on(async {
let _ = self
.msg_tx
.lock()
.await
.send(Message::PendingProgress(self.id, 100.0 * progress))
.await;
});
self.controller.set_progress(progress);
Ok(count)
}