diff --git a/widget/src/main.rs b/widget/src/main.rs index 9d20f82..b212ae6 100644 --- a/widget/src/main.rs +++ b/widget/src/main.rs @@ -22,39 +22,9 @@ fn main() -> Result<(), Box> { .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()), } diff --git a/widget/src/show_popup.rs b/widget/src/show_popup.rs index 0171fbc..f9a5072 100644 --- a/widget/src/show_popup.rs +++ b/widget/src/show_popup.rs @@ -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 { @@ -45,11 +26,35 @@ impl WeatherPopup { } fn view(&self) -> iced::Element { + // 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,16 +85,13 @@ impl WeatherPopup { .width(Length::Fill), // Weather description Row::with_children(vec![ - Text::new(format!( - "{} - {}", - self.weather_main, self.weather_description - )) - .font(Font { - family: Family::Name(default_font), - ..Font::DEFAULT - }) - .size(16) - .into(), + Text::new(format!("{weather_main} - {weather_description}")) + .font(Font { + family: Family::Name(default_font), + ..Font::DEFAULT + }) + .size(16) + .into(), ]) .width(Length::Fill), ] @@ -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.