Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions headlines/src/headlines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ pub enum Msg {
pub struct HeadlinesConfig {
pub dark_mode: bool,
pub api_key: String,
pub refresh_news_data: bool,
Copy link
Owner

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.

}

impl Default for HeadlinesConfig {
fn default() -> Self {
Self {
dark_mode: Default::default(),
api_key: String::new(),
refresh_news_data: Default::default(),
}
}
}
Expand Down Expand Up @@ -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 {
Copy link
Owner

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion headlines/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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();
Copy link
Owner

Choose a reason for hiding this comment

The 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());
Expand Down