Separate UI loading from main()

- Pass weather data and config directly to `show_popup()`
This commit is contained in:
Candifloss 2025-11-26 11:30:23 +05:30
parent ec99e817c1
commit 0a6cc079c2
2 changed files with 30 additions and 43 deletions

View File

@ -10,9 +10,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.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<dyn std::error::Error>> {
.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()),
}

View File

@ -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<dyn std::error::Error>> {
pub fn show_popup(resp: &WeatherResponse, cfg: &Config) -> Result<(), Box<dyn std::error::Error>> {
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));