From d3474c27404ce33d5ed9da853acffb985a8c33c3 Mon Sep 17 00:00:00 2001 From: Harshad Sabne Date: Tue, 26 Oct 2021 21:08:19 +0530 Subject: [PATCH] Try - Implement refresh button event handler --- headlines/src/headlines.rs | 22 ++++++++++++++++++++++ headlines/src/main.rs | 8 +++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/headlines/src/headlines.rs b/headlines/src/headlines.rs index 4dcd402..318e585 100644 --- a/headlines/src/headlines.rs +++ b/headlines/src/headlines.rs @@ -24,6 +24,7 @@ pub enum Msg { pub struct HeadlinesConfig { pub dark_mode: bool, pub api_key: String, + pub refresh_news_data: bool, } impl Default for HeadlinesConfig { @@ -31,6 +32,7 @@ impl Default for HeadlinesConfig { Self { dark_mode: Default::default(), api_key: String::new(), + refresh_news_data: Default::default(), } } } @@ -85,6 +87,22 @@ impl Headlines { } pub fn render_news_cards(&self, ui: &mut eframe::egui::Ui) { + if self.config.refresh_news_data{ + let default_loading_msg = NewsCardData { + title: String::from("Loading..."), + desc: String::from(""), + url: String::from("") + }; + ui.add_space(PADDING); + if self.config.dark_mode { + ui.colored_label(WHITE, default_loading_msg.title); + } else { + ui.colored_label(BLACK, default_loading_msg.title); + } + return + } + + for a in &self.articles { ui.add_space(PADDING); // render title @@ -130,6 +148,9 @@ impl Headlines { frame.quit(); } let refresh_btn = ui.add(Button::new("🔄").text_style(egui::TextStyle::Body)); + if refresh_btn.clicked() { + self.config.refresh_news_data = true; + } let theme_btn = ui.add( Button::new({ if self.config.dark_mode { @@ -172,6 +193,7 @@ impl Headlines { HeadlinesConfig { dark_mode: self.config.dark_mode, api_key: self.config.api_key.to_string(), + refresh_news_data: self.config.refresh_news_data, }, ) { tracing::error!("Failed saving app state: {}", e); diff --git a/headlines/src/main.rs b/headlines/src/main.rs index 57f2cb8..92620d4 100644 --- a/headlines/src/main.rs +++ b/headlines/src/main.rs @@ -24,7 +24,7 @@ impl App for Headlines { _storage: Option<&dyn eframe::epi::Storage>, ) { let api_key = self.config.api_key.to_string(); - + let refresh_news_data = self.config.refresh_news_data; let (mut news_tx, news_rx) = channel(); let (app_tx, app_rx) = sync_channel(1); @@ -52,6 +52,12 @@ impl App for Headlines { } fn update(&mut self, ctx: &eframe::egui::CtxRef, frame: &mut eframe::epi::Frame<'_>) { ctx.request_repaint(); + if self.config.refresh_news_data{ + self.config.refresh_news_data = false; + // tracing::error!("Refresh event triggered."); + let (mut news_tx, _news_rx) = channel(); + fetch_news(&self.config.api_key.to_string(), &mut news_tx); + } if self.config.dark_mode { ctx.set_visuals(Visuals::dark());