1+ use chrono:: { DateTime , Datelike , Timelike } ;
12use serde:: Deserialize ;
3+ use slint:: { Model , ModelRc , VecModel } ;
24
3- use crate :: { Api , Coordinates } ;
5+ use crate :: { Api , Coordinates , Date , Time , WeatherDaily } ;
46
57#[ derive( Debug , Clone , PartialEq , Deserialize , Default ) ]
6- pub struct WeatherData {
7- pub daily : WeatherDaily ,
8- pub hourly : WeatherHourly ,
9- pub utc_offset_seconds : i32 ,
10- }
11-
12- #[ derive( Debug , Clone , PartialEq , Deserialize , Default ) ]
13- pub struct WeatherDaily {
8+ pub struct WeatherApiDaily {
149 pub temperature_2m_max : Vec < f32 > ,
1510 pub temperature_2m_min : Vec < f32 > ,
1611 pub time : Vec < String > ,
@@ -22,7 +17,7 @@ pub struct WeatherDaily {
2217}
2318
2419#[ derive( Debug , Clone , PartialEq , Deserialize , Default ) ]
25- pub struct WeatherHourly {
20+ pub struct WeatherApiHourly {
2621 pub temperature_2m : Vec < f32 > ,
2722 pub precipitation : Vec < f32 > ,
2823 pub time : Vec < String > ,
@@ -31,8 +26,8 @@ pub struct WeatherHourly {
3126
3227#[ derive( Debug , Clone , PartialEq , Deserialize , Default ) ]
3328struct WeatherApiData {
34- daily : WeatherDaily ,
35- hourly : WeatherHourly ,
29+ daily : WeatherApiDaily ,
30+ hourly : WeatherApiHourly ,
3631 utc_offset_seconds : i32 ,
3732}
3833
@@ -76,8 +71,69 @@ async fn fetch_weather(coordinates: Coordinates) -> WeatherApiData {
7671
7772pub async fn set_weather ( api : Api < ' _ > ) {
7873 let coordinates = api. get_coordinates ( ) ;
79- if coordinates. latitude == 0.0 {
74+ if coordinates. latitude == 0.0 || coordinates . longitude == 0.0 {
8075 return ;
8176 }
82- // TODO: Move the wether types into slint UI
77+
78+ let api_data = fetch_weather ( coordinates) . await ;
79+
80+ let offset_sec = api_data. utc_offset_seconds / 60 / 60 ;
81+ let offset_hours = format ! ( "+{offset_sec}:00" ) ;
82+
83+ let daily = api_data. daily . clone ( ) ;
84+
85+ let mut weather_daily: Vec < WeatherDaily > = vec ! [ ] ;
86+ api_data
87+ . daily
88+ . time
89+ . iter ( )
90+ . enumerate ( )
91+ . for_each ( |( i, time) | {
92+ let api_date = DateTime :: parse_from_rfc3339 ( & format ! ( "{time}T00:00:00{offset_hours}" ) ) ;
93+
94+ let mut date = Date :: default ( ) ;
95+ if let Ok ( d) = api_date {
96+ date. year = d. year ( ) as i32 ;
97+ date. month = d. month ( ) as i32 ;
98+ date. day = d. day ( ) as i32 ;
99+ }
100+
101+ let mut sunrise = Time :: default ( ) ;
102+ let api_sunrise = DateTime :: parse_from_rfc3339 ( & format ! (
103+ "{}:00{offset_hours}" ,
104+ api_data. daily. sunrise[ i]
105+ ) ) ;
106+ if let Ok ( t) = api_sunrise {
107+ sunrise. hour = t. hour ( ) as i32 ;
108+ sunrise. minute = t. minute ( ) as i32 ;
109+ sunrise. second = t. second ( ) as i32 ;
110+ }
111+
112+ let api_sunset = DateTime :: parse_from_rfc3339 ( & format ! (
113+ "{}:00{offset_hours}" ,
114+ api_data. daily. sunset[ i]
115+ ) ) ;
116+
117+ let mut sunset = Time :: default ( ) ;
118+
119+ if let Ok ( t) = api_sunset {
120+ sunset. hour = t. hour ( ) as i32 ;
121+ sunset. minute = t. minute ( ) as i32 ;
122+ sunset. second = t. second ( ) as i32 ;
123+ }
124+
125+ weather_daily. push ( WeatherDaily {
126+ weather_code : daily. weather_code [ i] ,
127+ temperature_max : daily. temperature_2m_max [ i] ,
128+ temperature_min : daily. temperature_2m_min [ i] ,
129+ precipitation_sum : daily. precipitation_sum [ i] ,
130+ precipitation_probability_max : daily. precipitation_probability_max [ i] ,
131+ date : date,
132+ sunrise : sunrise,
133+ sunset : sunset,
134+ } ) ;
135+ } ) ;
136+
137+ // TODO: Figure this out
138+ api. set_weather_daily ( ModelRc :: from ( weather_daily) ) ;
83139}
0 commit comments