@@ -28,8 +28,6 @@ class HomeModel extends ChangeNotifier {
2828 RealtimeWeather ? _weather;
2929 List <History > _alerts = const [];
3030 Map <String , dynamic >? _forecast;
31- bool _isLoading = false ;
32- Object ? _error;
3331 Timer ? _autoRefreshTimer;
3432
3533 /// Creates a [HomeModel] backed by [settingsLocation] .
@@ -44,58 +42,35 @@ class HomeModel extends ChangeNotifier {
4442 if (_temporaryCode == null ) _doRefresh ();
4543 }
4644
47- Future <void > _doRefresh () async {
48- final code = _temporaryCode ?? _settingsLocation.code;
49- double ? lat;
50- double ? lon;
51-
52- if (code != null ) {
53- final loc = Global .location[code];
54- if (loc != null ) {
55- lat = loc.lat;
56- lon = loc.lng;
57- }
45+ /// Runs [task] and logs failures under [tag] ; returns `null` on error.
46+ static Future <T ?> _safe <T >(String tag, Future <T > Function () task) async {
47+ try {
48+ return await task ();
49+ } catch (e) {
50+ TalkerManager .instance.error ('HomeModel $tag ' , e);
51+ return null ;
5852 }
53+ }
5954
60- lat ?? = _settingsLocation.coordinates? .latitude;
61- lon ?? = _settingsLocation.coordinates? .longitude;
62-
55+ Future <void > _doRefresh () async {
56+ final code = _temporaryCode ?? _settingsLocation.code;
57+ final loc = code != null ? Global .location[code] : null ;
58+ final lat = loc? .lat ?? _settingsLocation.coordinates? .latitude;
59+ final lon = loc? .lng ?? _settingsLocation.coordinates? .longitude;
6360 if (lat == null || lon == null ) return ;
6461
65- _isLoading = true ;
66- notifyListeners ();
67-
6862 // Fetch weather + alerts + forecast in parallel.
6963 final results = await Future .wait <Object ?>([
70- Global .api.getWeatherRealtimeByCoords (lat, lon).then <Object ?>((v) => v).catchError ((e) {
71- TalkerManager .instance.error ('HomeModel weather' , e);
72- return null ;
73- }),
74- code == null
75- ? Future <Object ?>.value (null )
76- : Global .api.getRealtimeRegion (code).then <Object ?>((v) => v).catchError ((e) {
77- TalkerManager .instance.error ('HomeModel alerts' , e);
78- return null ;
79- }),
80- code == null
81- ? Future <Object ?>.value (null )
82- : Global .api.getWeatherForecast (code).then <Object ?>((v) => v).catchError ((e) {
83- TalkerManager .instance.error ('HomeModel forecast' , e);
84- return null ;
85- }),
64+ _safe ('weather' , () => Global .api.getWeatherRealtimeByCoords (lat, lon)),
65+ if (code != null ) _safe ('alerts' , () => Global .api.getRealtimeRegion (code)) else Future .value (null ),
66+ if (code != null ) _safe ('forecast' , () => Global .api.getWeatherForecast (code)) else Future .value (null ),
8667 ]);
8768
88- final weather = results[0 ];
89- final alerts = results[1 ];
90- final forecast = results[2 ];
91-
92- _weather = weather is RealtimeWeather ? weather : _weather;
93- _alerts = alerts is List <History >
94- ? alerts.sorted ((a, b) => b.time.send.compareTo (a.time.send))
69+ if (results[0 ] is RealtimeWeather ) _weather = results[0 ] as RealtimeWeather ;
70+ _alerts = results[1 ] is List <History >
71+ ? (results[1 ]! as List <History >).sorted ((a, b) => b.time.send.compareTo (a.time.send))
9572 : const [];
96- _forecast = forecast is Map <String , dynamic > ? forecast : _forecast;
97- _error = (weather == null && _weather == null ) ? Exception ('weather fetch failed' ) : null ;
98- _isLoading = false ;
73+ if (results[2 ] is Map <String , dynamic >) _forecast = results[2 ] as Map <String , dynamic >;
9974 notifyListeners ();
10075 }
10176
@@ -112,12 +87,6 @@ class HomeModel extends ChangeNotifier {
11287 /// The 24-hour weather forecast for the active location, or `null` if missing.
11388 Map <String , dynamic >? get forecast => _forecast;
11489
115- /// Whether a weather fetch is currently in progress.
116- bool get isLoading => _isLoading;
117-
118- /// The error from the last failed fetch, or `null` when the last fetch succeeded.
119- Object ? get error => _error;
120-
12190 /// The currently active temporary location code, or `null` when unset.
12291 String ? get temporaryCode => _temporaryCode;
12392
0 commit comments