feat: implemented basic update notification functionality

This commit is contained in:
Gabriele Musco 2024-08-21 23:53:25 +02:00
parent 6f142d9ee5
commit 6694655690
2 changed files with 37 additions and 1 deletions

View file

@ -117,6 +117,8 @@ pub enum Msg {
StartProber,
OnProberExit(bool),
NoOp,
#[cfg(feature = "appimage")]
CheckForUpdates,
}
impl App {
@ -740,6 +742,26 @@ impl AsyncComponent for App {
}
}
}
#[cfg(feature = "appimage")]
Msg::CheckForUpdates => match crate::updater::check_updates_available().await {
Ok(true) => {
let notif =
gtk::gio::Notification::new(&format!("{APP_NAME}: update available"));
notif.set_body(Some(&format!(
"A new version of {APP_NAME} is available. Download it now."
)));
notif.set_default_action("app.updateapp");
notif.set_icon(&gtk::gio::ThemedIcon::from_names(&[
crate::constants::APP_ID,
]));
self.application.send_notification(
Some("org.gabmus.envision.update_available_notif"),
&notif,
);
}
Err(e) => eprintln!("Warning: failed to check for updates: {e}"),
_ => {}
},
}
}
@ -944,6 +966,17 @@ impl AsyncComponent for App {
root.insert_action_group(AppActionGroup::NAME, Some(&actions.into_action_group()));
#[cfg(feature = "appimage")]
{
let update_action = gtk::gio::SimpleAction::new("updateapp", None);
update_action.connect_activate(move |_, _| {
open_with_default_handler(
"https://gitlab.com/gabmus/envision/-/pipelines?ref=main&status=success",
);
});
model.application.add_action(&update_action);
}
{
let app = &model.application;
app.set_accelerators_for_action::<QuitAction>(&["<Control>q"]);
@ -970,6 +1003,9 @@ impl AsyncComponent for App {
model.split_view = Some(widgets.split_view.clone());
#[cfg(feature = "appimage")]
sender.input(Msg::CheckForUpdates);
AsyncComponentParts { model, widgets }
}
}

View file

@ -28,7 +28,7 @@ pub async fn check_updates_available() -> anyhow::Result<bool> {
let text = res.text().await?;
let commit_list: GitLabCommitList = serde_json::from_str(&text)?;
if commit_list
.get(0)
.first()
.is_some_and(|c| c.id.starts_with(shorthash))
{
Ok(false)