Skip to content

Commit a88308d

Browse files
committed
Check errors in add_subscription_dialog
1 parent d2af271 commit a88308d

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

ntfy-daemon/src/models.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn emoji_map() -> &'static HashMap<String, String> {
1414
})
1515
}
1616

17-
fn validate_topic(topic: &str) -> Result<&str, Error> {
17+
pub fn validate_topic(topic: &str) -> Result<&str, Error> {
1818
let re = Regex::new(r"^\w+$").unwrap();
1919
if re.is_match(topic) {
2020
Ok(topic)

src/widgets/add_subscription_dialog.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ use glib::once_cell::sync::Lazy;
66
use glib::subclass::Signal;
77
use gtk::gio;
88
use gtk::glib;
9+
use ntfy_daemon::models;
910

1011
mod imp {
1112
pub use super::*;
1213
#[derive(Debug, Default)]
1314
pub struct AddSubscriptionDialog {
1415
pub topic_entry: RefCell<adw::EntryRow>,
1516
pub server_entry: RefCell<adw::EntryRow>,
17+
pub sub_btn: RefCell<gtk::Button>,
1618
}
1719

1820
#[glib::object_subclass]
@@ -119,7 +121,7 @@ impl AddSubscriptionDialog {
119121
}
120122
}
121123
},
122-
append = &gtk::Button {
124+
append: sub_btn = &gtk::Button {
123125
set_label: "Subscribe",
124126
add_css_class: "suggested-action",
125127
add_css_class: "pill",
@@ -134,8 +136,20 @@ impl AddSubscriptionDialog {
134136
},
135137
}
136138

139+
let (tx, rx) = glib::MainContext::channel(Default::default());
140+
let txc = tx.clone();
141+
topic_entry.delegate().unwrap().connect_changed(move |_| {
142+
txc.send(()).unwrap();
143+
});
144+
let rx = crate::async_utils::debounce_channel(std::time::Duration::from_millis(500), rx);
145+
let objc = obj.clone();
146+
rx.attach(None, move |_| {
147+
objc.check_errors();
148+
glib::ControlFlow::Continue
149+
});
137150
imp.topic_entry.replace(topic_entry);
138151
imp.server_entry.replace(server_entry);
152+
imp.sub_btn.replace(sub_btn);
139153

140154
obj.set_content(Some(&toolbar_view));
141155
}
@@ -145,6 +159,18 @@ impl AddSubscriptionDialog {
145159
pub fn server(&self) -> String {
146160
self.imp().server_entry.borrow().text().to_string()
147161
}
162+
fn check_errors(&self) {
163+
let imp = self.imp();
164+
let topic_entry = imp.topic_entry.borrow().clone();
165+
let sub_btn = imp.sub_btn.borrow().clone();
166+
if let Err(_) = models::validate_topic(&topic_entry.delegate().unwrap().text()) {
167+
topic_entry.add_css_class("error");
168+
sub_btn.set_sensitive(false);
169+
} else {
170+
topic_entry.remove_css_class("error");
171+
sub_btn.set_sensitive(true);
172+
}
173+
}
148174
fn emit_subscribe_request(&self) {
149175
self.emit_by_name::<()>("subscribe-request", &[]);
150176
}

0 commit comments

Comments
 (0)