mirror of
https://github.com/PranshulGG/WeatherMaster.git
synced 2026-10-22 02:52:37 -05:00
added option to disable alerts closes #520
This commit is contained in:
1 parent
19a7874be1
commit
4e6cf8a381
4 files changed
+38
-11
No files matched your search
@@ -17,6 +17,7 @@ class UnitSettingsNotifier extends ChangeNotifier {
|
||||
|
||||
|
||||
|
||||
|
||||
// Getters
|
||||
String get tempUnit => _tempUnit;
|
||||
String get windUnit => _windUnit;
|
||||
@@ -32,6 +33,7 @@ class UnitSettingsNotifier extends ChangeNotifier {
|
||||
|
||||
|
||||
|
||||
|
||||
UnitSettingsNotifier() {
|
||||
_loadAllUnits();
|
||||
}
|
||||
@@ -51,6 +53,7 @@ class UnitSettingsNotifier extends ChangeNotifier {
|
||||
|
||||
|
||||
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@@ -136,5 +139,4 @@ class UnitSettingsNotifier extends ChangeNotifier {
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -980,7 +980,7 @@ final duskFormatted = formatInstantToLocalTime(dusk, timeUnit: timeFormatDUSKDAW
|
||||
children: [
|
||||
Text("${PreferencesHelper.getString("selectedWindUnit") == 'M/s' ? formattedAvgWindSpeed.toStringAsFixed(1) : formattedAvgWindSpeed.round()}", style: TextStyle(fontSize: 50, color: Theme.of(context).colorScheme.secondary, fontWeight: FontWeight.w500),),
|
||||
Padding(padding: EdgeInsets.only(bottom: 11, left: 8),
|
||||
child: Text(PreferencesHelper.getString("selectedWindUnit") ?? 'Km/h', style: TextStyle(fontSize: 20, color: Theme.of(context).colorScheme.onSurfaceVariant, fontWeight: FontWeight.w500),))
|
||||
child: Text(localizeWindUnit(windUnit, context.locale), style: TextStyle(fontSize: 20, color: Theme.of(context).colorScheme.onSurfaceVariant, fontWeight: FontWeight.w500),))
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
@@ -284,6 +284,18 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
},
|
||||
),
|
||||
|
||||
SettingSwitchTile(
|
||||
icon: Icon(Symbols.warning, fill: 1, weight: 500),
|
||||
title: Text("weather_alerts".tr()),
|
||||
description: Text("show_alerts_home_page_sub".tr()),
|
||||
toggled: PreferencesHelper.getBool("showAlerts") ?? true,
|
||||
onChanged: (value) {
|
||||
PreferencesHelper.setBool("showAlerts", value);
|
||||
setState(() {
|
||||
});
|
||||
},
|
||||
),
|
||||
|
||||
SettingActionTile(
|
||||
icon: Icon(Symbols.nest_farsight_weather, fill: 1, weight: 500),
|
||||
title: Text('weather_models'.tr()),
|
||||
|
||||
@@ -45,25 +45,38 @@ class WeatherService {
|
||||
'forecast_hours': '1',
|
||||
});
|
||||
|
||||
final alertUri = Uri.parse('https://api.weatherapi.com/v1/alerts.json').replace(queryParameters: {
|
||||
'key': dotenv.env['API_KEY_WEATHERAPI']!.toString(),
|
||||
'q': '$lat,$lon',
|
||||
});
|
||||
|
||||
// final response = await http.get(uri);
|
||||
// final data = json.decode(response.body) as Map<String, dynamic>;
|
||||
|
||||
|
||||
bool showAlerts = PreferencesHelper.getBool("showAlerts") ?? true;
|
||||
|
||||
final responses = await Future.wait([
|
||||
// Prepare list of HTTP requests (always include weather and air quality)
|
||||
final requests = <Future<http.Response>>[
|
||||
http.get(uri),
|
||||
http.get(airQualityUri),
|
||||
http.get(alertUri),
|
||||
]);
|
||||
];
|
||||
|
||||
// Conditionally add alerts request only if user wants them
|
||||
if (showAlerts) {
|
||||
final alertUri = Uri.parse('https://api.weatherapi.com/v1/alerts.json').replace(queryParameters: {
|
||||
'key': dotenv.env['API_KEY_WEATHERAPI']!.toString(),
|
||||
'q': '$lat,$lon',
|
||||
});
|
||||
requests.add(http.get(alertUri));
|
||||
}
|
||||
|
||||
// Execute all HTTP requests in parallel
|
||||
final responses = await Future.wait(requests);
|
||||
|
||||
// Parse responses
|
||||
final weatherData = json.decode(responses[0].body) as Map<String, dynamic>;
|
||||
final airQualityData = json.decode(responses[1].body) as Map<String, dynamic>;
|
||||
final alertData = json.decode(responses[2].body) as Map<String, dynamic>;
|
||||
|
||||
// Conditionally decode alert data if it was fetched
|
||||
final alertData = showAlerts && responses.length > 2
|
||||
? json.decode(responses[2].body) as Map<String, dynamic>
|
||||
: {};
|
||||
|
||||
// Check if we need fallback data for missing fields
|
||||
Map<String, dynamic> finalWeatherData = weatherData;
|
||||
|
||||
Reference in new issue
Block a user