mirror of
https://github.com/darkmoonight/Rain.git
synced 2025-06-28 20:19:58 +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?';
|
..options.baseUrl = 'https://api.open-meteo.com/v1/forecast?';
|
||||||
final Dio dioLocation = Dio();
|
final Dio dioLocation = Dio();
|
||||||
|
|
||||||
Future<MainWeatherCache> getWeatherData(double? lat, double? lon) async {
|
static const String _weatherParams =
|
||||||
String url =
|
'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'
|
||||||
'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';
|
'&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'
|
||||||
String urlWeather;
|
'&forecast_days=12&timezone=auto';
|
||||||
settings.measurements == 'imperial' && settings.degrees == 'fahrenheit'
|
|
||||||
? urlWeather =
|
String _buildWeatherUrl(double lat, double lon) {
|
||||||
'$url&temperature_unit=fahrenheit&windspeed_unit=mph&precipitation_unit=inch'
|
String url = 'latitude=$lat&longitude=$lon&$_weatherParams';
|
||||||
: settings.measurements == 'imperial'
|
if (settings.measurements == 'imperial') {
|
||||||
? urlWeather = '$url&windspeed_unit=mph&precipitation_unit=inch'
|
url += '&windspeed_unit=mph&precipitation_unit=inch';
|
||||||
: settings.degrees == 'fahrenheit'
|
}
|
||||||
? urlWeather = '$url&temperature_unit=fahrenheit'
|
if (settings.degrees == 'fahrenheit') {
|
||||||
: urlWeather = url;
|
url += '&temperature_unit=fahrenheit';
|
||||||
|
}
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<MainWeatherCache> getWeatherData(double lat, double lon) async {
|
||||||
|
final String urlWeather = _buildWeatherUrl(lat, lon);
|
||||||
try {
|
try {
|
||||||
Response response = await dio.get(urlWeather);
|
Response response = await dio.get(urlWeather);
|
||||||
WeatherDataApi weatherData = WeatherDataApi.fromJson(response.data);
|
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(
|
return MainWeatherCache(
|
||||||
time: weatherData.hourly.time,
|
time: weatherData.hourly.time,
|
||||||
temperature2M: weatherData.hourly.temperature2M,
|
temperature2M: weatherData.hourly.temperature2M,
|
||||||
|
@ -64,30 +122,10 @@ class WeatherAPI {
|
||||||
timezone: weatherData.timezone,
|
timezone: weatherData.timezone,
|
||||||
timestamp: DateTime.now(),
|
timestamp: DateTime.now(),
|
||||||
);
|
);
|
||||||
} on DioException catch (e) {
|
|
||||||
if (kDebugMode) {
|
|
||||||
print(e);
|
|
||||||
}
|
|
||||||
rethrow;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<WeatherCard> getWeatherCard(double? lat, double? lon, String city,
|
WeatherCard _mapWeatherDataToCard(WeatherDataApi weatherData, double lat,
|
||||||
String district, String timezone) async {
|
double lon, String city, String district, String timezone) {
|
||||||
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);
|
|
||||||
return WeatherCard(
|
return WeatherCard(
|
||||||
time: weatherData.hourly.time,
|
time: weatherData.hourly.time,
|
||||||
temperature2M: weatherData.hourly.temperature2M,
|
temperature2M: weatherData.hourly.temperature2M,
|
||||||
|
@ -130,37 +168,5 @@ class WeatherAPI {
|
||||||
timezone: timezone,
|
timezone: timezone,
|
||||||
timestamp: DateTime.now(),
|
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) {
|
for (var oldCard in weatherCard) {
|
||||||
var updatedCard = await WeatherAPI().getWeatherCard(oldCard.lat,
|
var updatedCard = await WeatherAPI().getWeatherCard(oldCard.lat!,
|
||||||
oldCard.lon, oldCard.city!, oldCard.district!, oldCard.timezone!);
|
oldCard.lon!, oldCard.city!, oldCard.district!, oldCard.timezone!);
|
||||||
isar.writeTxnSync(() {
|
isar.writeTxnSync(() {
|
||||||
oldCard
|
oldCard
|
||||||
..time = updatedCard.time
|
..time = updatedCard.time
|
||||||
|
@ -372,8 +372,8 @@ class WeatherController extends GetxController {
|
||||||
}
|
}
|
||||||
|
|
||||||
final updatedCard = await WeatherAPI().getWeatherCard(
|
final updatedCard = await WeatherAPI().getWeatherCard(
|
||||||
weatherCard.lat,
|
weatherCard.lat!,
|
||||||
weatherCard.lon,
|
weatherCard.lon!,
|
||||||
weatherCard.city!,
|
weatherCard.city!,
|
||||||
weatherCard.district!,
|
weatherCard.district!,
|
||||||
weatherCard.timezone!,
|
weatherCard.timezone!,
|
||||||
|
|
|
@ -3,7 +3,7 @@ description: Weather application
|
||||||
|
|
||||||
publish_to: "none"
|
publish_to: "none"
|
||||||
|
|
||||||
version: 1.3.2+35
|
version: 1.3.4+37
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
sdk: ">=3.4.3 <4.0.0"
|
sdk: ">=3.4.3 <4.0.0"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue