fix: lock upload button when uploading

This commit is contained in:
uku 2025-05-12 17:13:10 +02:00
parent 09b4829281
commit e757a1474c
Signed by: uku
SSH key fingerprint: SHA256:4P0aN6M8ajKukNi6aPOaX0LacanGYtlfjmN+m/sHY/o

View file

@ -43,12 +43,14 @@ impl Config {
enum Message {
OpenFilePicker,
SetFolder(ZiplineFolder),
LockAndStart,
StartTheProcess,
Nothing,
}
struct Tyrolienne {
config: Config,
locked: bool,
video_path: Option<PathBuf>,
folder: Option<ZiplineFolder>,
dialog: Controller<Dialog>,
@ -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<Self> {
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<Self>,
sender: relm4::AsyncComponentSender<Self>,
_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<String> {
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...");