fix: make request sending asynchronous too
This commit is contained in:
parent
2f96459f58
commit
fb227660bb
4 changed files with 100 additions and 70 deletions
93
src/main.rs
93
src/main.rs
|
@ -8,16 +8,18 @@ use color_eyre::eyre::{OptionExt, Result, bail};
|
|||
use gobject::GtkZiplineFolder;
|
||||
use relm::{Dialog, DialogInput};
|
||||
use relm4::{
|
||||
Component, ComponentController, ComponentParts, Controller, RelmApp,
|
||||
Component, ComponentController, Controller, RelmApp,
|
||||
adw::{self, prelude::*},
|
||||
gtk::{self, gio, glib::clone},
|
||||
prelude::{AsyncComponent, AsyncComponentParts},
|
||||
tokio,
|
||||
};
|
||||
use tracing::Level;
|
||||
use tracing_subscriber::EnvFilter;
|
||||
use urlencoding::Encoded;
|
||||
use zipline::ZiplineFolder;
|
||||
|
||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
|
||||
#[derive(Debug, Default, Clone, serde::Deserialize, serde::Serialize)]
|
||||
struct Config {
|
||||
zipline_url: String,
|
||||
zipline_token: String,
|
||||
|
@ -45,11 +47,6 @@ enum Message {
|
|||
Nothing,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum CommandMessage {
|
||||
SelectedFile(Option<PathBuf>),
|
||||
}
|
||||
|
||||
struct Widgets {
|
||||
file_picker_row: adw::ActionRow,
|
||||
send_button: gtk::Button,
|
||||
|
@ -71,11 +68,11 @@ impl Tyrolienne {
|
|||
}
|
||||
}
|
||||
|
||||
impl Component for Tyrolienne {
|
||||
impl AsyncComponent for Tyrolienne {
|
||||
type Input = Message;
|
||||
type Output = ();
|
||||
type CommandOutput = CommandMessage;
|
||||
type Init = (Config, Vec<ZiplineFolder>);
|
||||
type CommandOutput = ();
|
||||
type Init = Config;
|
||||
type Root = adw::ApplicationWindow;
|
||||
type Widgets = Widgets;
|
||||
|
||||
|
@ -86,11 +83,11 @@ impl Component for Tyrolienne {
|
|||
.build()
|
||||
}
|
||||
|
||||
fn init(
|
||||
(config, folders): Self::Init,
|
||||
async fn init(
|
||||
config: Self::Init,
|
||||
root: Self::Root,
|
||||
sender: relm4::ComponentSender<Self>,
|
||||
) -> relm4::ComponentParts<Self> {
|
||||
sender: relm4::AsyncComponentSender<Self>,
|
||||
) -> AsyncComponentParts<Self> {
|
||||
let model = Tyrolienne {
|
||||
config,
|
||||
video_path: None,
|
||||
|
@ -100,6 +97,19 @@ impl Component for Tyrolienne {
|
|||
.forward(sender.input_sender(), |_| Message::Nothing),
|
||||
};
|
||||
|
||||
// TODO consider using the "loading" (?) mechanism from relm4
|
||||
// https://relm4.org/book/stable/threads_and_async/async.html
|
||||
let folders = match zipline::get_folders(&model.config).await {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
model.dialog.emit(DialogInput::Show {
|
||||
heading: "Could not fetch folders".into(),
|
||||
body: e.to_string(),
|
||||
});
|
||||
Vec::new()
|
||||
}
|
||||
};
|
||||
|
||||
let file_picker_row = adw::ActionRow::builder()
|
||||
.activatable(true)
|
||||
.title("Video file")
|
||||
|
@ -179,13 +189,13 @@ impl Component for Tyrolienne {
|
|||
send_button,
|
||||
};
|
||||
|
||||
ComponentParts { model, widgets }
|
||||
AsyncComponentParts { model, widgets }
|
||||
}
|
||||
|
||||
fn update(
|
||||
async fn update(
|
||||
&mut self,
|
||||
message: Self::Input,
|
||||
sender: relm4::ComponentSender<Self>,
|
||||
_sender: relm4::AsyncComponentSender<Self>,
|
||||
_root: &Self::Root,
|
||||
) {
|
||||
match message {
|
||||
|
@ -193,38 +203,27 @@ impl Component for Tyrolienne {
|
|||
Message::SetFolder(folder) => {
|
||||
self.folder = (folder.id != GtkZiplineFolder::NONE_ID).then_some(folder)
|
||||
}
|
||||
Message::StartTheProcess => match the_process(self) {
|
||||
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 => sender.oneshot_command(async {
|
||||
CommandMessage::SelectedFile(
|
||||
rfd::AsyncFileDialog::new()
|
||||
.add_filter("Video file", &["mp4", "mkv", "webm"])
|
||||
.pick_file()
|
||||
.await
|
||||
.map(|h| h.path().to_owned()),
|
||||
)
|
||||
}),
|
||||
Message::OpenFilePicker => {
|
||||
let file = rfd::AsyncFileDialog::new()
|
||||
.add_filter("Video file", &["mp4", "mkv", "webm"])
|
||||
.pick_file()
|
||||
.await;
|
||||
|
||||
if let Some(file) = file {
|
||||
self.video_path = Some(file.path().to_owned());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn update_cmd(
|
||||
&mut self,
|
||||
message: Self::CommandOutput,
|
||||
_sender: relm4::ComponentSender<Self>,
|
||||
_root: &Self::Root,
|
||||
) {
|
||||
match message {
|
||||
CommandMessage::SelectedFile(Some(path)) => self.video_path = Some(path),
|
||||
CommandMessage::SelectedFile(None) => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn update_view(&self, widgets: &mut Self::Widgets, _sender: relm4::ComponentSender<Self>) {
|
||||
fn update_view(&self, widgets: &mut Self::Widgets, _sender: relm4::AsyncComponentSender<Self>) {
|
||||
widgets
|
||||
.file_picker_row
|
||||
.set_subtitle(&self.display_video_path());
|
||||
|
@ -242,10 +241,9 @@ fn main() -> Result<()> {
|
|||
|
||||
// TODO: show dialog in case these error
|
||||
let config = get_config()?;
|
||||
let folders = zipline::get_folders(&config)?;
|
||||
|
||||
let app = RelmApp::new("net.uku3lig.Tyrolienne");
|
||||
app.run::<Tyrolienne>((config, folders));
|
||||
app.run_async::<Tyrolienne>(config);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -279,7 +277,7 @@ fn get_config() -> Result<Config> {
|
|||
Ok(config)
|
||||
}
|
||||
|
||||
fn the_process(app: &Tyrolienne) -> Result<String> {
|
||||
async fn the_process(app: &Tyrolienne) -> Result<String> {
|
||||
if let Some(folder) = app.folder.as_ref() {
|
||||
tracing::info!("uploading to folder '{}'...", folder.name);
|
||||
} else {
|
||||
|
@ -290,18 +288,19 @@ fn the_process(app: &Tyrolienne) -> Result<String> {
|
|||
&app.config,
|
||||
app.folder.as_ref(),
|
||||
app.video_path.as_ref().unwrap(),
|
||||
)?;
|
||||
)
|
||||
.await?;
|
||||
let zp_file = &res.files[0];
|
||||
|
||||
tracing::info!("recalculating thumbnails...");
|
||||
|
||||
zipline::recalc_thumbnails(&app.config)?;
|
||||
zipline::recalc_thumbnails(&app.config).await?;
|
||||
|
||||
std::thread::sleep(Duration::from_secs(2));
|
||||
tokio::time::sleep(Duration::from_secs(2)).await;
|
||||
|
||||
tracing::info!("fetching thumbnail url...");
|
||||
|
||||
let res = zipline::get_file_details(&app.config, &zp_file.id)?;
|
||||
let res = zipline::get_file_details(&app.config, &zp_file.id).await?;
|
||||
let thumbnail_url = res
|
||||
.thumbnail_url(&app.config)
|
||||
.ok_or_eyre("could not get thumbnail url")?;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue