mirror of
https://github.com/darkmoonight/Rain.git
synced 2025-06-28 12:09:57 +00:00
Fix api code
This commit is contained in:
parent
46e1546e5b
commit
33ceb30885
3 changed files with 116 additions and 110 deletions
|
@ -11,21 +11,79 @@ class WeatherAPI {
|
|||
..options.baseUrl = 'https://api.open-meteo.com/v1/forecast?';
|
||||
final Dio dioLocation = Dio();
|
||||
|
||||
Future<MainWeatherCache> getWeatherData(double? lat, double? lon) async {
|
||||
String url =
|
||||
'latitude=$lat&longitude=$lon&hourly=temperature_2m,relativehumidity_2m,apparent_temperature,precipitation,rain,weathercode,surface_pressure,visibility,evapotranspiration,windspeed_10m,winddirection_10m,windgusts_10m,cloudcover,uv_index,dewpoint_2m,precipitation_probability,shortwave_radiation&daily=weathercode,temperature_2m_max,temperature_2m_min,apparent_temperature_max,apparent_temperature_min,sunrise,sunset,precipitation_sum,precipitation_probability_max,windspeed_10m_max,windgusts_10m_max,uv_index_max,rain_sum,winddirection_10m_dominant&forecast_days=12&timezone=auto';
|
||||
String urlWeather;
|
||||
settings.measurements == 'imperial' && settings.degrees == 'fahrenheit'
|
||||
? urlWeather =
|
||||
'$url&temperature_unit=fahrenheit&windspeed_unit=mph&precipitation_unit=inch'
|
||||
: settings.measurements == 'imperial'
|
||||
? urlWeather = '$url&windspeed_unit=mph&precipitation_unit=inch'
|
||||
: settings.degrees == 'fahrenheit'
|
||||
? urlWeather = '$url&temperature_unit=fahrenheit'
|
||||
: urlWeather = url;
|
||||
static const String _weatherParams =
|
||||
'hourly=temperature_2m,relativehumidity_2m,apparent_temperature,precipitation,rain,weathercode,surface_pressure,visibility,evapotranspiration,windspeed_10m,winddirection_10m,windgusts_10m,cloudcover,uv_index,dewpoint_2m,precipitation_probability,shortwave_radiation'
|
||||
'&daily=weathercode,temperature_2m_max,temperature_2m_min,apparent_temperature_max,apparent_temperature_min,sunrise,sunset,precipitation_sum,precipitation_probability_max,windspeed_10m_max,windgusts_10m_max,uv_index_max,rain_sum,winddirection_10m_dominant'
|
||||
'&forecast_days=12&timezone=auto';
|
||||
|
||||
String _buildWeatherUrl(double lat, double lon) {
|
||||
String url = 'latitude=$lat&longitude=$lon&$_weatherParams';
|
||||
if (settings.measurements == 'imperial') {
|
||||
url += '&windspeed_unit=mph&precipitation_unit=inch';
|
||||
}
|
||||
if (settings.degrees == 'fahrenheit') {
|
||||
url += '&temperature_unit=fahrenheit';
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
Future<MainWeatherCache> getWeatherData(double lat, double lon) async {
|
||||
final String urlWeather = _buildWeatherUrl(lat, lon);
|
||||
try {
|
||||
Response response = await dio.get(urlWeather);
|
||||
WeatherDataApi weatherData = WeatherDataApi.fromJson(response.data);
|
||||
return _mapWeatherDataToCache(weatherData);
|
||||
} on DioException catch (e) {
|
||||
if (kDebugMode) {
|
||||
print(e);
|
||||
}
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
Future<WeatherCard> getWeatherCard(double lat, double lon, String city,
|
||||
String district, String timezone) async {
|
||||
final String urlWeather = _buildWeatherUrl(lat, lon);
|
||||
try {
|
||||
Response response = await dio.get(urlWeather);
|
||||
WeatherDataApi weatherData = WeatherDataApi.fromJson(response.data);
|
||||
return _mapWeatherDataToCard(
|
||||
weatherData, lat, lon, city, district, timezone);
|
||||
} on DioException catch (e) {
|
||||
if (kDebugMode) {
|
||||
print(e);
|
||||
}
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
Future<Iterable<Result>> getCity(String query, Locale? locale) async {
|
||||
final String url =
|
||||
'https://geocoding-api.open-meteo.com/v1/search?name=$query&count=5&language=${locale?.languageCode}&format=json';
|
||||
try {
|
||||
Response response = await dioLocation.get(url);
|
||||
if (response.statusCode == 200) {
|
||||
CityApi cityData = CityApi.fromJson(response.data);
|
||||
return cityData.results.map(
|
||||
(e) => Result(
|
||||
admin1: e.admin1,
|
||||
name: e.name,
|
||||
latitude: e.latitude,
|
||||
longitude: e.longitude,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
throw Exception('Failed to load suggestions');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
if (kDebugMode) {
|
||||
print(e);
|
||||
}
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
MainWeatherCache _mapWeatherDataToCache(WeatherDataApi weatherData) {
|
||||
return MainWeatherCache(
|
||||
time: weatherData.hourly.time,
|
||||
temperature2M: weatherData.hourly.temperature2M,
|
||||
|
@ -64,30 +122,10 @@ class WeatherAPI {
|
|||
timezone: weatherData.timezone,
|
||||
timestamp: DateTime.now(),
|
||||
);
|
||||
} on DioException catch (e) {
|
||||
if (kDebugMode) {
|
||||
print(e);
|
||||
}
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
Future<WeatherCard> getWeatherCard(double? lat, double? lon, String city,
|
||||
String district, String timezone) async {
|
||||
String url =
|
||||
'latitude=$lat&longitude=$lon&hourly=temperature_2m,relativehumidity_2m,apparent_temperature,precipitation,rain,weathercode,surface_pressure,visibility,evapotranspiration,windspeed_10m,winddirection_10m,windgusts_10m,cloudcover,uv_index,dewpoint_2m,precipitation_probability,shortwave_radiation&daily=weathercode,temperature_2m_max,temperature_2m_min,apparent_temperature_max,apparent_temperature_min,sunrise,sunset,precipitation_sum,precipitation_probability_max,windspeed_10m_max,windgusts_10m_max,uv_index_max,rain_sum,winddirection_10m_dominant&forecast_days=12&timezone=auto';
|
||||
String urlWeather;
|
||||
settings.measurements == 'imperial' && settings.degrees == 'fahrenheit'
|
||||
? urlWeather =
|
||||
'$url&temperature_unit=fahrenheit&windspeed_unit=mph&precipitation_unit=inch'
|
||||
: settings.measurements == 'imperial'
|
||||
? urlWeather = '$url&windspeed_unit=mph&precipitation_unit=inch'
|
||||
: settings.degrees == 'fahrenheit'
|
||||
? urlWeather = '$url&temperature_unit=fahrenheit'
|
||||
: urlWeather = url;
|
||||
try {
|
||||
Response response = await dio.get(urlWeather);
|
||||
WeatherDataApi weatherData = WeatherDataApi.fromJson(response.data);
|
||||
WeatherCard _mapWeatherDataToCard(WeatherDataApi weatherData, double lat,
|
||||
double lon, String city, String district, String timezone) {
|
||||
return WeatherCard(
|
||||
time: weatherData.hourly.time,
|
||||
temperature2M: weatherData.hourly.temperature2M,
|
||||
|
@ -130,37 +168,5 @@ class WeatherAPI {
|
|||
timezone: timezone,
|
||||
timestamp: DateTime.now(),
|
||||
);
|
||||
} on DioException catch (e) {
|
||||
if (kDebugMode) {
|
||||
print(e);
|
||||
}
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
Future<Iterable<Result>> getCity(String query, Locale? locale) async {
|
||||
final url =
|
||||
'https://geocoding-api.open-meteo.com/v1/search?name=$query&count=5&language=${locale?.languageCode}&format=json';
|
||||
try {
|
||||
Response response = await dioLocation.get(url);
|
||||
if (response.statusCode == 200) {
|
||||
CityApi cityData = CityApi.fromJson(response.data);
|
||||
return cityData.results.map(
|
||||
(e) => Result(
|
||||
admin1: e.admin1,
|
||||
name: e.name,
|
||||
latitude: e.latitude,
|
||||
longitude: e.longitude,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
throw Exception('Failed to load suggestions');
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
if (kDebugMode) {
|
||||
print(e);
|
||||
}
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -316,8 +316,8 @@ class WeatherController extends GetxController {
|
|||
}
|
||||
|
||||
for (var oldCard in weatherCard) {
|
||||
var updatedCard = await WeatherAPI().getWeatherCard(oldCard.lat,
|
||||
oldCard.lon, oldCard.city!, oldCard.district!, oldCard.timezone!);
|
||||
var updatedCard = await WeatherAPI().getWeatherCard(oldCard.lat!,
|
||||
oldCard.lon!, oldCard.city!, oldCard.district!, oldCard.timezone!);
|
||||
isar.writeTxnSync(() {
|
||||
oldCard
|
||||
..time = updatedCard.time
|
||||
|
@ -372,8 +372,8 @@ class WeatherController extends GetxController {
|
|||
}
|
||||
|
||||
final updatedCard = await WeatherAPI().getWeatherCard(
|
||||
weatherCard.lat,
|
||||
weatherCard.lon,
|
||||
weatherCard.lat!,
|
||||
weatherCard.lon!,
|
||||
weatherCard.city!,
|
||||
weatherCard.district!,
|
||||
weatherCard.timezone!,
|
||||
|
|
|
@ -3,7 +3,7 @@ description: Weather application
|
|||
|
||||
publish_to: "none"
|
||||
|
||||
version: 1.3.2+35
|
||||
version: 1.3.4+37
|
||||
|
||||
environment:
|
||||
sdk: ">=3.4.3 <4.0.0"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue