diff --git a/widget/src/main.rs b/widget/src/main.rs index 4d29fc1..d252ab6 100644 --- a/widget/src/main.rs +++ b/widget/src/main.rs @@ -10,9 +10,9 @@ fn main() -> Result<(), Box> { .ok_or("No config dir found")? .join("candywidgets/openweathermap.toml"); + // Load config let toml_str = fs::read_to_string(&path) .map_err(|_| format!("Failed to read config: {}", path.display()))?; - let cfg: Config = toml::from_str(&toml_str)?; match cfg.general.api_version() { @@ -21,40 +21,11 @@ fn main() -> Result<(), Box> { .ok_or("No cache dir found")? .join("candydesktop/owm_widget.json"); + // Read weather data from cache file 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 3c3e50a..da7bba1 100644 --- a/widget/src/show_popup.rs +++ b/widget/src/show_popup.rs @@ -1,18 +1,34 @@ +use owm_rs::free_api_v25::current::WeatherResponse; +use owm_widg_config::config::Config; use slint::SharedString; slint::include_modules!(); -/// Create and show the UI window populated with weather data. -pub fn show_popup( - city: String, - country: String, - weather_main: String, - weather_description: String, - icon_code: String, - temperature: String, - unit: char, -) -> Result<(), Box> { +pub fn show_popup(resp: &WeatherResponse, cfg: &Config) -> Result<(), Box> { let ui = MainWindow::new()?; + + let city = resp.name.clone().unwrap_or_else(|| "Unknown".into()); + let country = resp + .sys + .as_ref() + .and_then(|s| s.country.clone()) + .unwrap_or_default(); + + let (weather_main, weather_description, icon_code) = resp.weather.first().map_or_else( + || ("N/A".into(), "N/A".into(), String::new()), + |w| (w.main.clone(), w.description.clone(), w.icon.clone()), + ); + + 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', + _ => '?', + }; + ui.set_city(SharedString::from(city)); ui.set_country(SharedString::from(country)); ui.set_weather_main(SharedString::from(weather_main));