From e757a1474c941e0e1883b57abc996f1dc5a7098e Mon Sep 17 00:00:00 2001 From: uku Date: Mon, 12 May 2025 17:13:10 +0200 Subject: [PATCH] fix: lock upload button when uploading --- src/main.rs | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5ee6cd0..f6ae7ee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,12 +43,14 @@ impl Config { enum Message { OpenFilePicker, SetFolder(ZiplineFolder), + LockAndStart, StartTheProcess, Nothing, } struct Tyrolienne { config: Config, + locked: bool, video_path: Option, folder: Option, dialog: Controller, @@ -118,10 +120,11 @@ impl AsyncComponent for Tyrolienne { }, gtk::Button { - set_label: "Send", #[watch] - set_sensitive: model.video_path.is_some(), - connect_clicked => Message::StartTheProcess, + set_label: if model.locked { "Uploading..." } else { "Send" }, + #[watch] + set_sensitive: !model.locked && model.video_path.is_some(), + connect_clicked => Message::LockAndStart, } } } @@ -135,6 +138,7 @@ impl AsyncComponent for Tyrolienne { ) -> AsyncComponentParts { let model = Tyrolienne { config, + locked: false, video_path: None, folder: None, dialog: Dialog::builder() @@ -177,7 +181,7 @@ impl AsyncComponent for Tyrolienne { async fn update( &mut self, message: Self::Input, - _sender: relm4::AsyncComponentSender, + sender: relm4::AsyncComponentSender, _root: &Self::Root, ) { match message { @@ -185,13 +189,6 @@ impl AsyncComponent for Tyrolienne { Message::SetFolder(folder) => { self.folder = (folder.id != GtkZiplineFolder::NONE_ID).then_some(folder) } - Message::StartTheProcess => match the_process(self).await { - Ok(url) => tracing::info!("{url}"), - Err(e) => self.dialog.emit(DialogInput::Show { - heading: "An error occurred".into(), - body: e.to_string(), - }), - }, Message::OpenFilePicker => { let file = rfd::AsyncFileDialog::new() .add_filter("Video file", &["mp4", "mkv", "webm"]) @@ -202,6 +199,22 @@ 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 { + heading: "An error occurred".into(), + body: e.to_string(), + }), + } + self.locked = false; + } } } } @@ -258,12 +271,11 @@ async fn the_process(app: &Tyrolienne) -> Result { tracing::info!("uploading video..."); } - let res = zipline::upload_file( - &app.config, - app.folder.as_ref(), - app.video_path.as_ref().unwrap(), - ) - .await?; + let Some(ref video_path) = app.video_path else { + bail!("No video given!"); + }; + + let res = zipline::upload_file(&app.config, app.folder.as_ref(), video_path).await?; let zp_file = &res.files[0]; tracing::info!("recalculating thumbnails...");