From a88308d67a9586f5a9cd8afea3a79909dc45eb7c Mon Sep 17 00:00:00 2001 From: ranfdev Date: Tue, 24 Oct 2023 12:35:57 +0200 Subject: [PATCH] Check errors in add_subscription_dialog --- ntfy-daemon/src/models.rs | 2 +- src/widgets/add_subscription_dialog.rs | 28 +++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/ntfy-daemon/src/models.rs b/ntfy-daemon/src/models.rs index 337d790..aaa6425 100644 --- a/ntfy-daemon/src/models.rs +++ b/ntfy-daemon/src/models.rs @@ -14,7 +14,7 @@ fn emoji_map() -> &'static HashMap { }) } -fn validate_topic(topic: &str) -> Result<&str, Error> { +pub fn validate_topic(topic: &str) -> Result<&str, Error> { let re = Regex::new(r"^\w+$").unwrap(); if re.is_match(topic) { Ok(topic) diff --git a/src/widgets/add_subscription_dialog.rs b/src/widgets/add_subscription_dialog.rs index 5e4dfe6..375bf8e 100644 --- a/src/widgets/add_subscription_dialog.rs +++ b/src/widgets/add_subscription_dialog.rs @@ -6,6 +6,7 @@ use glib::once_cell::sync::Lazy; use glib::subclass::Signal; use gtk::gio; use gtk::glib; +use ntfy_daemon::models; mod imp { pub use super::*; @@ -13,6 +14,7 @@ mod imp { pub struct AddSubscriptionDialog { pub topic_entry: RefCell, pub server_entry: RefCell, + pub sub_btn: RefCell, } #[glib::object_subclass] @@ -119,7 +121,7 @@ impl AddSubscriptionDialog { } } }, - append = >k::Button { + append: sub_btn = >k::Button { set_label: "Subscribe", add_css_class: "suggested-action", add_css_class: "pill", @@ -134,8 +136,20 @@ impl AddSubscriptionDialog { }, } + let (tx, rx) = glib::MainContext::channel(Default::default()); + let txc = tx.clone(); + topic_entry.delegate().unwrap().connect_changed(move |_| { + txc.send(()).unwrap(); + }); + let rx = crate::async_utils::debounce_channel(std::time::Duration::from_millis(500), rx); + let objc = obj.clone(); + rx.attach(None, move |_| { + objc.check_errors(); + glib::ControlFlow::Continue + }); imp.topic_entry.replace(topic_entry); imp.server_entry.replace(server_entry); + imp.sub_btn.replace(sub_btn); obj.set_content(Some(&toolbar_view)); } @@ -145,6 +159,18 @@ impl AddSubscriptionDialog { pub fn server(&self) -> String { self.imp().server_entry.borrow().text().to_string() } + fn check_errors(&self) { + let imp = self.imp(); + let topic_entry = imp.topic_entry.borrow().clone(); + let sub_btn = imp.sub_btn.borrow().clone(); + if let Err(_) = models::validate_topic(&topic_entry.delegate().unwrap().text()) { + topic_entry.add_css_class("error"); + sub_btn.set_sensitive(false); + } else { + topic_entry.remove_css_class("error"); + sub_btn.set_sensitive(true); + } + } fn emit_subscribe_request(&self) { self.emit_by_name::<()>("subscribe-request", &[]); }