feat: add upload progress bar

This commit is contained in:
uku 2025-05-12 22:54:13 +02:00
parent ca96431208
commit 3b75134572
Signed by: uku
SSH key fingerprint: SHA256:4P0aN6M8ajKukNi6aPOaX0LacanGYtlfjmN+m/sHY/o
4 changed files with 125 additions and 27 deletions

View file

@ -8,7 +8,7 @@ use color_eyre::eyre::{OptionExt, Result, bail};
use gobject::GtkZiplineFolder;
use relm::{Dialog, DialogInput};
use relm4::{
Component, ComponentController, Controller, RelmApp,
Component, ComponentController, Controller, RelmApp, Sender,
adw::{self, prelude::*},
gtk::{self, gio},
prelude::{AsyncComponent, AsyncComponentParts},
@ -43,14 +43,29 @@ impl Config {
enum Message {
OpenFilePicker,
SetFolder(ZiplineFolder),
LockAndStart,
StartTheProcess,
Nothing,
}
#[derive(Debug)]
enum ProgressMessage {
SetTotal(usize),
Progress(usize),
Finish,
Error(String),
}
struct UploadInfo {
config: Config,
video_path: Option<PathBuf>,
folder: Option<ZiplineFolder>,
}
struct Tyrolienne {
config: Config,
locked: bool,
progress: usize,
total: usize,
video_path: Option<PathBuf>,
folder: Option<ZiplineFolder>,
dialog: Controller<Dialog>,
@ -63,13 +78,21 @@ impl Tyrolienne {
.map(|p| p.to_string_lossy())
.unwrap_or(Cow::Borrowed("None"))
}
fn clone_as_info(&self) -> UploadInfo {
UploadInfo {
config: self.config.clone(),
video_path: self.video_path.clone(),
folder: self.folder.clone(),
}
}
}
#[relm4::component(async)]
impl AsyncComponent for Tyrolienne {
type Input = Message;
type Output = ();
type CommandOutput = ();
type CommandOutput = ProgressMessage;
type Init = Config;
view! {
@ -119,12 +142,22 @@ impl AsyncComponent for Tyrolienne {
}
},
gtk::Button {
#[watch]
set_label: if model.locked { "Uploading..." } else { "Send" },
#[watch]
set_sensitive: !model.locked && model.video_path.is_some(),
connect_clicked => Message::LockAndStart,
gtk::Box {
set_orientation: gtk::Orientation::Vertical,
set_spacing: 10,
gtk::ProgressBar {
#[watch]
set_fraction: model.progress as f64 / model.total as f64,
},
gtk::Button {
#[watch]
set_label: if model.locked { "Uploading..." } else { "Send" },
#[watch]
set_sensitive: !model.locked && model.video_path.is_some(),
connect_clicked => Message::StartTheProcess,
}
}
}
}
@ -139,6 +172,8 @@ impl AsyncComponent for Tyrolienne {
let model = Tyrolienne {
config,
locked: false,
progress: 0,
total: 1,
video_path: None,
folder: None,
dialog: Dialog::builder()
@ -199,21 +234,48 @@ impl AsyncComponent for Tyrolienne {
self.video_path = Some(file.path().to_owned());
}
}
Message::LockAndStart => {
// a little bit convoluted, but i don't really know how to force a view update
// before starting the "long" process
self.locked = true;
sender.input(Message::StartTheProcess);
}
Message::StartTheProcess => {
match the_process(self).await {
Ok(url) => tracing::info!("{url}"),
Err(e) => self.dialog.emit(DialogInput::Show {
self.locked = true;
let info = self.clone_as_info();
sender.command(|out, shutdown| {
Box::pin(
shutdown
.register(async move {
match the_process(info, &out).await {
Ok(url) => {
tracing::info!("{url}");
out.emit(ProgressMessage::Finish);
}
Err(e) => out.emit(ProgressMessage::Error(e.to_string())),
}
})
.drop_on_shutdown(),
)
});
}
}
}
async fn update_cmd(
&mut self,
message: Self::CommandOutput,
_sender: relm4::AsyncComponentSender<Self>,
_root: &Self::Root,
) {
match message {
ProgressMessage::SetTotal(total) => self.total = total,
ProgressMessage::Progress(prog) => self.progress += prog,
ProgressMessage::Finish | ProgressMessage::Error(_) => {
self.locked = false;
self.progress = 0;
self.total = 1;
if let ProgressMessage::Error(e) = message {
self.dialog.emit(DialogInput::Show {
heading: "An error occurred".into(),
body: e.to_string(),
}),
});
}
self.locked = false;
}
}
}
@ -264,7 +326,7 @@ fn get_config() -> Result<Config> {
Ok(config)
}
async fn the_process(app: &Tyrolienne) -> Result<String> {
async fn the_process(app: UploadInfo, sender: &Sender<ProgressMessage>) -> Result<String> {
if let Some(folder) = app.folder.as_ref() {
tracing::info!("uploading to folder '{}'...", folder.name);
} else {
@ -275,7 +337,7 @@ async fn the_process(app: &Tyrolienne) -> Result<String> {
bail!("No video given!");
};
let res = zipline::upload_file(&app.config, app.folder.as_ref(), video_path).await?;
let res = zipline::upload_file(&app.config, sender, app.folder.as_ref(), video_path).await?;
let zp_file = &res.files[0];
tracing::info!("recalculating thumbnails...");