Skip to content

Commit

Permalink
Use custom server from selected subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
ranfdev committed Nov 9, 2023
1 parent 5c51832 commit 98811de
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
3 changes: 2 additions & 1 deletion ntfy-daemon/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use serde::{Deserialize, Serialize};

use crate::Error;

pub const DEFAULT_SERVER: &str = "https://ntfy.sh";
static EMOJI_MAP: OnceLock<HashMap<String, String>> = OnceLock::new();

fn emoji_map() -> &'static HashMap<String, String> {
Expand Down Expand Up @@ -188,7 +189,7 @@ pub struct SubscriptionBuilder {
impl SubscriptionBuilder {
pub fn new(topic: String) -> Self {
Self {
server: "https://ntfy.sh".to_string(),
server: DEFAULT_SERVER.to_string(),
topic,
muted: false,
archived: false,
Expand Down
23 changes: 14 additions & 9 deletions src/widgets/add_subscription_dialog.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cell::OnceCell;
use std::cell::RefCell;

use adw::prelude::*;
Expand All @@ -20,6 +21,7 @@ mod imp {
#[derive(Debug, Default)]
pub struct AddSubscriptionDialog {
pub widgets: RefCell<Widgets>,
pub init_custom_server: OnceCell<String>,
}

#[glib::object_subclass]
Expand Down Expand Up @@ -47,12 +49,6 @@ mod imp {
Lazy::new(|| vec![Signal::builder("subscribe-request").build()]);
SIGNALS.as_ref()
}

fn constructed(&self) {
self.parent_constructed();
let obj = self.obj().clone();
obj.build_ui();
}
}
impl WidgetImpl for AddSubscriptionDialog {}
impl WindowImpl for AddSubscriptionDialog {}
Expand All @@ -66,8 +62,15 @@ glib::wrapper! {
}

impl AddSubscriptionDialog {
pub fn new() -> Self {
glib::Object::builder().build()
pub fn new(custom_server: Option<String>) -> Self {
let this: Self = glib::Object::builder().build();
if let Some(s) = custom_server {
if s != ntfy_daemon::models::DEFAULT_SERVER {
this.imp().init_custom_server.set(s).unwrap();
}
}
this.build_ui();
this
}
fn build_ui(&self) {
let imp = self.imp();
Expand Down Expand Up @@ -118,10 +121,12 @@ impl AddSubscriptionDialog {
},
append: server_expander = &adw::ExpanderRow {
set_title: "Custom server...",
set_enable_expansion: false,
set_enable_expansion: imp.init_custom_server.get().is_some(),
set_expanded: imp.init_custom_server.get().is_some(),
set_show_enable_switch: true,
add_row: server_entry = &adw::EntryRow {
set_title: "Server",
set_text: imp.init_custom_server.get().map(|x| x.as_str()).unwrap_or(""),
}
}
},
Expand Down
5 changes: 3 additions & 2 deletions src/widgets/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,12 @@ mod imp {
impl NotifyWindow {
#[template_callback]
fn show_add_topic(&self, _btn: &gtk::Button) {
let dialog = AddSubscriptionDialog::new();
let this = self.obj().clone();
let dialog =
AddSubscriptionDialog::new(this.selected_subscription().map(|x| x.server()));
dialog.set_transient_for(Some(&self.obj().clone()));
dialog.present();

let this = self.obj().clone();
let dc = dialog.clone();
dialog.connect_local("subscribe-request", true, move |_| {
let sub = match dc.subscription() {
Expand Down

0 comments on commit 98811de

Please sign in to comment.