Skip to content

Commit

Permalink
WIP: Add notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
treiher committed Aug 9, 2023
1 parent 12c0ceb commit 8058742
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 17 deletions.
2 changes: 1 addition & 1 deletion frontend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ serde = "1.0"
serde_json = "1.0"
slice-group-by = "0.3"
wasm-bindgen = "=0.2.74"
web-sys = { version = "0.3", features = ["AudioContext", "AudioDestinationNode", "AudioNode", "AudioParam", "GainNode", "OscillatorNode", "ScrollBehavior", "ScrollIntoViewOptions", "ScrollLogicalPosition", "ScrollToOptions"] }
web-sys = { version = "0.3", features = ["AudioContext", "AudioDestinationNode", "AudioNode", "AudioParam", "GainNode", "Notification", "NotificationOptions", "NotificationPermission", "OscillatorNode", "ScrollBehavior", "ScrollIntoViewOptions", "ScrollLogicalPosition", "ScrollToOptions"] }
72 changes: 56 additions & 16 deletions frontend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ enum Msg {
ShowSettingsDialog,
CloseSettingsDialog,
BeepVolumeChanged(String),
EnableNotifications,
GoUp,
LogOut,

Expand Down Expand Up @@ -310,6 +311,23 @@ fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
orders.send_msg(Msg::Data(data::Msg::SetBeepVolume(value)));
}
}
Msg::EnableNotifications => {
orders.skip().perform_cmd(async {
if let Ok(promise) = web_sys::Notification::request_permission() {
if let Ok(result) = wasm_bindgen_futures::JsFuture::from(promise).await {
if let Some(string) = result.as_string() {
log!(string); // FIXME
let mut options = web_sys::NotificationOptions::new();
options.body("Body");
options.icon("Icon");
options.tag("Tag");
let notification =
web_sys::Notification::new_with_options("Test", &options);
}
}
}
});
}
Msg::GoUp => match &model.page {
Some(Page::Home(_) | Page::Login(_)) => {}
Some(Page::Admin(_)) => {
Expand Down Expand Up @@ -633,22 +651,44 @@ fn view_settings_dialog(data_model: &data::Model) -> Node<Msg> {
common::view_dialog(
"primary",
"Settings",
nodes![p![
h1![C!["subtitle"], "Beep volume"],
input![
C!["slider"],
C!["is-fullwidth"],
C!["is-info"],
attrs! {
At::Type => "range",
At::Value => data_model.settings.beep_volume,
At::Min => 0,
At::Max => 100,
At::Step => 10,
},
input_ev(Ev::Input, Msg::BeepVolumeChanged),
]
]],
nodes![
p![
h1![C!["subtitle"], "Beep volume"],
input![
C!["slider"],
C!["is-fullwidth"],
C!["is-info"],
attrs! {
At::Type => "range",
At::Value => data_model.settings.beep_volume,
At::Min => 0,
At::Max => 100,
At::Step => 10,
},
input_ev(Ev::Input, Msg::BeepVolumeChanged),
]
],
{
let permission = web_sys::Notification::permission();
p![
h1![C!["subtitle"], "Notifications"],
button![
C!["button"],
match permission {
web_sys::NotificationPermission::Granted => C!["is-primary"],
web_sys::NotificationPermission::Denied => C!["is-danger"],
_ => C![],
},
ev(Ev::Click, |_| Msg::EnableNotifications),
match permission {
web_sys::NotificationPermission::Granted => "Granted",
web_sys::NotificationPermission::Denied => "Denied",
_ => "Enable",
},
],
]
}
],
&ev(Ev::Click, |_| Msg::CloseSettingsDialog),
)
}
Expand Down
1 change: 1 addition & 0 deletions frontend/src/page/training_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,7 @@ pub fn update(
orders.notify(data::Msg::EndTrainingSession);
} else {
guide.section_start_time = Utc::now();
let notification = web_sys::Notification::new("Exercise");
}
}
update_guide_timer(model);
Expand Down

0 comments on commit 8058742

Please sign in to comment.