-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathmain.rs
More file actions
143 lines (128 loc) · 4.25 KB
/
Copy pathmain.rs
File metadata and controls
143 lines (128 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
mod headlines;
use std::{
sync::mpsc::{channel, sync_channel},
thread,
};
use eframe::{
egui::{
CentralPanel, CtxRef, Hyperlink, Label, ScrollArea, Separator, TextStyle, TopBottomPanel,
Ui, Vec2, Visuals,
},
epi::App,
run_native, NativeOptions,
};
use headlines::{Headlines, Msg, NewsCardData, PADDING};
use newsapi::NewsAPI;
impl App for Headlines {
fn setup(
&mut self,
ctx: &eframe::egui::CtxRef,
_frame: &mut eframe::epi::Frame<'_>,
_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);
self.app_tx = Some(app_tx);
self.news_rx = Some(news_rx);
thread::spawn(move || {
if !api_key.is_empty() {
fetch_news(&api_key, &mut news_tx);
} else {
loop {
match app_rx.recv() {
Ok(Msg::ApiKeySet(api_key)) => {
fetch_news(&api_key, &mut news_tx);
}
Err(e) => {
tracing::error!("failed receiving msg: {}", e);
}
}
}
}
});
self.configure_fonts(ctx);
}
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());
} else {
ctx.set_visuals(Visuals::light());
}
if !self.api_key_initialized {
self.render_config(ctx);
} else {
self.preload_articles();
self.render_top_panel(ctx, frame);
CentralPanel::default().show(ctx, |ui| {
render_header(ui);
ScrollArea::auto_sized().show(ui, |ui| {
self.render_news_cards(ui);
});
render_footer(ctx);
});
}
}
fn name(&self) -> &str {
"Headlines"
}
}
fn fetch_news(api_key: &str, news_tx: &mut std::sync::mpsc::Sender<NewsCardData>) {
if let Ok(response) = NewsAPI::new(&api_key).fetch() {
let resp_articles = response.articles();
for a in resp_articles.iter() {
let news = NewsCardData {
title: a.title().to_string(),
url: a.url().to_string(),
desc: a.desc().map(|s| s.to_string()).unwrap_or("...".to_string()),
};
if let Err(e) = news_tx.send(news) {
tracing::error!("Error sending news data: {}", e);
}
}
} else {
tracing::error!("failed fetching news");
}
}
fn render_footer(ctx: &CtxRef) {
TopBottomPanel::bottom("footer").show(ctx, |ui| {
ui.vertical_centered(|ui| {
ui.add_space(10.);
ui.add(Label::new("API source: newsapi.org").monospace());
ui.add(
Hyperlink::new("https://github.com/emilk/egui")
.text("Made with egui")
.text_style(TextStyle::Monospace),
);
ui.add(
Hyperlink::new("https://github.com/creativcoder/headlines")
.text("creativcoder/headlines")
.text_style(TextStyle::Monospace),
);
ui.add_space(10.);
})
});
}
fn render_header(ui: &mut Ui) {
ui.vertical_centered(|ui| {
ui.heading("headlines");
});
ui.add_space(PADDING);
let sep = Separator::default().spacing(20.);
ui.add(sep);
}
fn main() {
tracing_subscriber::fmt::init();
let app = Headlines::new();
let mut win_option = NativeOptions::default();
win_option.initial_window_size = Some(Vec2::new(540., 960.));
run_native(Box::new(app), win_option);
}