Skip to content

Commit

Permalink
Add notification toasts.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmackdev committed Sep 26, 2023
1 parent 4a35287 commit fd21b79
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 9 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ opt-level = 2 # fast and small wasm
[profile.dev.package."*"]
opt-level = 2


[patch.crates-io]
egui = { git = "https://github.com/emilk/egui", rev = "23ce4e7" }
egui_extras = { git = "https://github.com/emilk/egui", rev = "23ce4e7" }
Expand Down
1 change: 1 addition & 0 deletions pubsubman/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ tokio-util = { workspace = true }
pubsubman_backend = { version = "0.1.0", path = "../pubsubman_backend" }
serde_json = "1.0.104"
clap = { version = "4.4.0", features = ["derive"] }
egui-notify = "0.9.0"
11 changes: 9 additions & 2 deletions pubsubman/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
actions::{create_subscription, delete_subscriptions, refresh_topics},
column_settings::ColumnSettings,
exit_state::{ExitState, SubscriptionCleanupState},
notifications::Notifications,
settings::Settings,
ui::{render_topic_name, MessagesView, PublishView},
};
Expand All @@ -33,6 +34,7 @@ pub struct App {
memory: Memory,
front_tx: Sender<FrontendMessage>,
back_rx: Receiver<BackendMessage>,
notifications: Notifications,
}

impl App {
Expand Down Expand Up @@ -63,15 +65,19 @@ impl App {
memory,
front_tx,
back_rx,
notifications: Notifications::default(),
}
}

fn handle_backend_message(&mut self) {
match self.back_rx.try_recv() {
Ok(message) => match message {
BackendMessage::ClientInitialised => {}
BackendMessage::ClientInitialised(project_id) => self
.notifications
.success(format!("Successfully authenticated to: {}.", project_id)),
BackendMessage::TopicsUpdated(topic_names) => {
self.topic_names = topic_names;

refresh_topics(&self.front_tx, Some(5000));
}
BackendMessage::SubscriptionCreated(topic_name, sub_name) => {
Expand All @@ -94,7 +100,7 @@ impl App {

self.exit_state.subscription_cleanup_state = SubscriptionCleanupState::Complete;
}
BackendMessage::Error(_) => {}
BackendMessage::Error(err) => self.notifications.error(err),
},
Err(_err) => {}
}
Expand Down Expand Up @@ -282,6 +288,7 @@ impl eframe::App for App {
self.render_topics_panel(ctx);
self.render_central_panel(ctx);
self.handle_exit(ctx, frame);
self.notifications.show(ctx);
}

fn save(&mut self, storage: &mut dyn eframe::Storage) {
Expand Down
1 change: 1 addition & 0 deletions pubsubman/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod actions;
mod app;
mod column_settings;
mod exit_state;
mod notifications;
mod settings;
mod ui;
pub use app::App;
27 changes: 27 additions & 0 deletions pubsubman/src/notifications.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use egui_notify::Toasts;
use pubsubman_backend::message::BackendError;

#[derive(Default)]
pub struct Notifications {
toasts: Toasts,
}

impl Notifications {
pub fn show(&mut self, ctx: &egui::Context) {
let ctx = ctx.clone();

ctx.style_mut(|style| {
style.visuals.widgets.noninteractive.bg_fill = egui::Color32::from_rgb(50, 50, 50);
});

self.toasts.show(&ctx);
}

pub fn success(&mut self, message: String) {
self.toasts.success(message).set_show_progress_bar(false);
}

pub fn error(&mut self, error: BackendError) {
self.toasts.error(error.to_string());
}
}
1 change: 1 addition & 0 deletions pubsubman_backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ google-cloud-gax = "0.15.0"
google-cloud-googleapis = "0.11.0"
google-cloud-pubsub = "0.20.0"
serde = { workspace = true }
thiserror = "1.0.48"
tokio = { workspace = true }
tokio-util = { workspace = true }

Expand Down
12 changes: 8 additions & 4 deletions pubsubman_backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ impl Backend {
.unwrap();

match rt.block_on(async { create_client(emulator_project_id).await }) {
Ok(client) => {
Ok((client, project_id)) => {
let back_tx_clone = back_tx.clone();
rt.spawn(async move {
back_tx_clone
.send(BackendMessage::ClientInitialised)
.send(BackendMessage::ClientInitialised(project_id))
.await
.unwrap();
});
Expand Down Expand Up @@ -235,7 +235,9 @@ impl Backend {
}
}

async fn create_client(emulator_project_id: Option<String>) -> Result<Client, Box<dyn Error>> {
async fn create_client(
emulator_project_id: Option<String>,
) -> Result<(Client, String), Box<dyn Error>> {
let mut config = ClientConfig::default().with_auth().await?;

if let (Environment::Emulator(_), Some(emulator_project_id)) =
Expand All @@ -244,5 +246,7 @@ async fn create_client(emulator_project_id: Option<String>) -> Result<Client, Bo
config.project_id = Some(emulator_project_id);
}

Ok(Client::new(config).await?)
let project_id = config.project_id.clone();

Ok((Client::new(config).await?, project_id.unwrap()))
}
9 changes: 7 additions & 2 deletions pubsubman_backend/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,24 @@ pub enum FrontendMessage {

#[derive(Debug)]
pub enum BackendMessage {
ClientInitialised,
ClientInitialised(String),
TopicsUpdated(Vec<TopicName>),
SubscriptionCreated(TopicName, SubscriptionName),
MessageReceived(TopicName, PubsubMessage),
SubscriptionsDeleted(Vec<Result<SubscriptionName, SubscriptionName>>),
Error(BackendError),
}

#[derive(Debug)]
#[derive(Debug, thiserror::Error)]
pub enum BackendError {
#[error("Failed to initialise client.")]
ClientInitFailed,
#[error("Failed to get topics.")]
GetTopicsFailed,
#[error("Failed to create Subscription for {0}.")]
CreateSubscriptionFailed(TopicName),
#[error("Failed to get messages from {0}.")]
StreamMessagesFailed(TopicName, SubscriptionName),
#[error("Failed to publish message to {0}.")]
PublishMessageFailed(TopicName),
}
14 changes: 14 additions & 0 deletions pubsubman_backend/src/model/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
mod pubsub_message;
mod pubsub_message_to_publish;

use std::fmt::Display;

pub use pubsub_message::PubsubMessage;
pub use pubsub_message_to_publish::PubsubMessageToPublish;

Expand All @@ -9,7 +11,19 @@ pub use pubsub_message_to_publish::PubsubMessageToPublish;
)]
pub struct TopicName(pub String);

impl Display for TopicName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}

#[derive(
Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, serde::Deserialize, serde::Serialize,
)]
pub struct SubscriptionName(pub String);

impl Display for SubscriptionName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}

0 comments on commit fd21b79

Please sign in to comment.