Refactor weather popup to use WeatherResponse and Config directly
- Update `main.rs` to pass `WeatherResponse` and `Config` directly to the `show_popup` function. - Refactore `show_popup.rs` to receive `WeatherResponse` and `Config` as parameters and handle UI rendering accordingly. - Remove redundant parsing of weather data and configuration within the popup. - Simplifie the data flow by directly passing the necessary structures to the popup.
This commit is contained in:
parent
b169bc9530
commit
3540f8d75a
@ -22,39 +22,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
.join("candydesktop/owm_widget.json");
|
||||
|
||||
let json_data = fs::read_to_string(&cache_path)?;
|
||||
let resp: WeatherResponse = serde_json::from_str(&json_data)?;
|
||||
let weather_data: WeatherResponse = serde_json::from_str(&json_data)?;
|
||||
|
||||
let city = resp.name.clone().unwrap_or_else(|| "Unknown".into());
|
||||
let country = resp
|
||||
.sys
|
||||
.as_ref()
|
||||
.and_then(|s| s.country.clone())
|
||||
.unwrap_or_else(|| String::new());
|
||||
|
||||
let (weather_main, weather_description, icon) = if let Some(w) = resp.weather.first() {
|
||||
(w.main.clone(), w.description.clone(), w.icon.clone())
|
||||
} else {
|
||||
("N/A".into(), "N/A".into(), String::new())
|
||||
};
|
||||
|
||||
let temp = resp.main.as_ref().and_then(|m| m.temp).unwrap_or(0.0);
|
||||
let temperature = format!("{temp:.1}");
|
||||
let unit = match cfg.query_params.units.as_str() {
|
||||
"metric" => 'C',
|
||||
"imperial" => 'F',
|
||||
"standard" => 'K',
|
||||
_ => '?',
|
||||
};
|
||||
|
||||
show_popup::show_popup(
|
||||
city,
|
||||
country,
|
||||
weather_main,
|
||||
weather_description,
|
||||
icon,
|
||||
temperature,
|
||||
unit,
|
||||
)?;
|
||||
show_popup::show_popup(weather_data, cfg)?;
|
||||
}
|
||||
other => return Err(format!("Unsupported api_version: {other:?}").into()),
|
||||
}
|
||||
|
||||
@ -5,39 +5,20 @@ use iced::{
|
||||
widget::{Column, Row, Space, Text, column, row},
|
||||
window,
|
||||
};
|
||||
use owm_rs::free_api_v25::current::WeatherResponse;
|
||||
use owm_widg_config;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
enum Message {}
|
||||
|
||||
struct WeatherPopup {
|
||||
city: String,
|
||||
country: String,
|
||||
weather_main: String,
|
||||
weather_description: String,
|
||||
icon_code: String,
|
||||
temperature: String,
|
||||
unit: char,
|
||||
resp: WeatherResponse,
|
||||
conf: owm_widg_config::config::Config,
|
||||
}
|
||||
|
||||
impl WeatherPopup {
|
||||
fn new(
|
||||
city: String,
|
||||
country: String,
|
||||
weather_main: String,
|
||||
weather_description: String,
|
||||
icon_code: String,
|
||||
temperature: String,
|
||||
unit: char,
|
||||
) -> Self {
|
||||
Self {
|
||||
city,
|
||||
country,
|
||||
weather_main,
|
||||
weather_description,
|
||||
icon_code,
|
||||
temperature,
|
||||
unit,
|
||||
}
|
||||
fn new(resp: WeatherResponse, conf: owm_widg_config::config::Config) -> Self {
|
||||
Self { resp, conf }
|
||||
}
|
||||
|
||||
fn update(&mut self, _message: Message) -> Task<Message> {
|
||||
@ -45,11 +26,35 @@ impl WeatherPopup {
|
||||
}
|
||||
|
||||
fn view(&self) -> iced::Element<Message> {
|
||||
// Data
|
||||
let default_font = "IosevkaTermSlab Nerd Font Mono";
|
||||
let city = self.resp.name.clone().unwrap_or_else(|| "Unknown".into());
|
||||
let country = self
|
||||
.resp
|
||||
.sys
|
||||
.as_ref()
|
||||
.and_then(|s| s.country.clone())
|
||||
.unwrap_or_else(|| String::new());
|
||||
let (weather_main, weather_description, icon_code) =
|
||||
if let Some(w) = self.resp.weather.first() {
|
||||
(w.main.clone(), w.description.clone(), w.icon.clone())
|
||||
} else {
|
||||
("N/A".into(), "N/A".into(), String::new())
|
||||
};
|
||||
let temp = self.resp.main.as_ref().and_then(|m| m.temp).unwrap_or(0.0);
|
||||
let temperature = format!("{temp:.1}");
|
||||
let unit = match self.conf.query_params.units.as_str() {
|
||||
"metric" => 'C',
|
||||
"imperial" => 'F',
|
||||
"standard" => 'K',
|
||||
_ => '?',
|
||||
};
|
||||
|
||||
// UI
|
||||
column![
|
||||
// City and country
|
||||
Row::with_children(vec![
|
||||
Text::new(format!("{}, {}", self.city, self.country))
|
||||
Text::new(format!("{city}, {country}"))
|
||||
.font(Font {
|
||||
family: Family::Name(default_font),
|
||||
..Font::DEFAULT
|
||||
@ -60,7 +65,7 @@ impl WeatherPopup {
|
||||
.width(Length::Fill),
|
||||
// Weather icon and temperature
|
||||
Row::with_children(vec![
|
||||
Text::new(icon_to_nerd_font(&self.icon_code))
|
||||
Text::new(icon_to_nerd_font(&icon_code))
|
||||
.font(Font {
|
||||
family: Family::Name(default_font),
|
||||
..Font::DEFAULT
|
||||
@ -69,7 +74,7 @@ impl WeatherPopup {
|
||||
.size(40)
|
||||
.into(),
|
||||
Space::with_width(Length::Fill).into(),
|
||||
Text::new(format!("{}°{}", self.temperature, self.unit))
|
||||
Text::new(format!("{temperature}°{unit}"))
|
||||
.font(Font {
|
||||
family: Family::Name(default_font),
|
||||
..Font::DEFAULT
|
||||
@ -80,10 +85,7 @@ impl WeatherPopup {
|
||||
.width(Length::Fill),
|
||||
// Weather description
|
||||
Row::with_children(vec![
|
||||
Text::new(format!(
|
||||
"{} - {}",
|
||||
self.weather_main, self.weather_description
|
||||
))
|
||||
Text::new(format!("{weather_main} - {weather_description}"))
|
||||
.font(Font {
|
||||
family: Family::Name(default_font),
|
||||
..Font::DEFAULT
|
||||
@ -100,15 +102,7 @@ impl WeatherPopup {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn show_popup(
|
||||
city: String,
|
||||
country: String,
|
||||
weather_main: String,
|
||||
weather_description: String,
|
||||
icon_code: String,
|
||||
temperature: String,
|
||||
unit: char,
|
||||
) -> iced::Result {
|
||||
pub fn show_popup(resp: WeatherResponse, conf: owm_widg_config::config::Config) -> iced::Result {
|
||||
iced::application(
|
||||
"Weather Popup", // Title
|
||||
WeatherPopup::update,
|
||||
@ -123,20 +117,7 @@ pub fn show_popup(
|
||||
decorations: false,
|
||||
..window::Settings::default()
|
||||
})
|
||||
.run_with(move || {
|
||||
(
|
||||
WeatherPopup::new(
|
||||
city,
|
||||
country,
|
||||
weather_main,
|
||||
weather_description,
|
||||
icon_code,
|
||||
temperature,
|
||||
unit,
|
||||
),
|
||||
Task::none(),
|
||||
)
|
||||
})
|
||||
.run_with(move || (WeatherPopup::new(resp, conf), Task::none()))
|
||||
}
|
||||
|
||||
/// Convert OWM icon codes (e.g. "01d", "09n") to Nerd Font weather glyphs.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user