-
Notifications
You must be signed in to change notification settings - Fork 24
Fix #1 - Implement refresh button event handler #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,13 +24,15 @@ pub enum Msg { | |
pub struct HeadlinesConfig { | ||
pub dark_mode: bool, | ||
pub api_key: String, | ||
pub refresh_news_data: bool, | ||
} | ||
|
||
impl Default for HeadlinesConfig { | ||
fn default() -> Self { | ||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could do this, but this seems unnecessary. Reason: We're needlessly adding dummy data, only to render a loading state. |
||
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); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're also creating a fresh channel pair in update method, which isn't ideal. |
||
fetch_news(&self.config.api_key.to_string(), &mut news_tx); | ||
} | ||
|
||
if self.config.dark_mode { | ||
ctx.set_visuals(Visuals::dark()); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This need not be part of
HeadlinesConfig
as it's a state that is applicable while the app is running.