2023-09-08 18:55:26 +03:00
|
|
|
import 'dart:io';
|
2023-06-17 20:57:57 +03:00
|
|
|
import 'package:flutter/material.dart';
|
2023-09-08 18:55:26 +03:00
|
|
|
import 'package:flutter/services.dart';
|
2023-07-09 23:41:51 +03:00
|
|
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
2023-06-17 20:57:57 +03:00
|
|
|
import 'package:geocoding/geocoding.dart';
|
|
|
|
import 'package:geolocator/geolocator.dart';
|
|
|
|
import 'package:get/get.dart';
|
2023-10-01 22:38:43 +03:00
|
|
|
import 'package:home_widget/home_widget.dart';
|
2023-06-17 20:57:57 +03:00
|
|
|
import 'package:isar/isar.dart';
|
2023-09-08 18:55:26 +03:00
|
|
|
import 'package:path_provider/path_provider.dart';
|
2023-06-17 20:57:57 +03:00
|
|
|
import 'package:rain/app/api/api.dart';
|
|
|
|
import 'package:rain/app/data/weather.dart';
|
2023-07-08 15:19:13 +03:00
|
|
|
import 'package:rain/app/services/notification.dart';
|
2023-09-05 21:31:29 +03:00
|
|
|
import 'package:rain/app/services/utils.dart';
|
2023-09-03 09:08:43 +03:00
|
|
|
import 'package:rain/app/widgets/status/status_weather.dart';
|
|
|
|
import 'package:rain/app/widgets/status/status_data.dart';
|
2023-06-17 20:57:57 +03:00
|
|
|
import 'package:rain/main.dart';
|
|
|
|
import 'package:timezone/standalone.dart' as tz;
|
|
|
|
import 'package:lat_lng_to_timezone/lat_lng_to_timezone.dart' as tzmap;
|
|
|
|
import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';
|
|
|
|
|
2023-09-01 20:18:40 +03:00
|
|
|
class WeatherController extends GetxController {
|
2023-06-17 20:57:57 +03:00
|
|
|
final isLoading = true.obs;
|
|
|
|
final _district = ''.obs;
|
|
|
|
final _city = ''.obs;
|
|
|
|
final _latitude = 0.0.obs;
|
|
|
|
final _longitude = 0.0.obs;
|
|
|
|
|
|
|
|
String get district => _district.value;
|
|
|
|
String get city => _city.value;
|
|
|
|
double get latitude => _latitude.value;
|
|
|
|
double get longitude => _longitude.value;
|
|
|
|
|
|
|
|
final _mainWeather = MainWeatherCache().obs;
|
|
|
|
final _location = LocationCache().obs;
|
|
|
|
final _weatherCard = WeatherCard().obs;
|
|
|
|
|
2023-07-25 21:21:21 +03:00
|
|
|
final weatherCards = <WeatherCard>[].obs;
|
|
|
|
|
2023-06-17 20:57:57 +03:00
|
|
|
MainWeatherCache get mainWeather => _mainWeather.value;
|
|
|
|
LocationCache get location => _location.value;
|
|
|
|
WeatherCard get weatherCard => _weatherCard.value;
|
|
|
|
|
|
|
|
final hourOfDay = 0.obs;
|
|
|
|
final dayOfNow = 0.obs;
|
2023-08-04 21:19:30 +03:00
|
|
|
final itemScrollController = ItemScrollController();
|
2023-09-22 15:11:03 +03:00
|
|
|
final cacheExpiry = DateTime.now().subtract(const Duration(hours: 12));
|
2023-06-17 20:57:57 +03:00
|
|
|
|
2023-07-25 21:21:21 +03:00
|
|
|
@override
|
|
|
|
void onInit() {
|
2023-09-05 21:31:29 +03:00
|
|
|
weatherCards
|
|
|
|
.assignAll(isar.weatherCards.where().sortByIndex().findAllSync());
|
2023-07-25 21:21:21 +03:00
|
|
|
super.onInit();
|
|
|
|
}
|
|
|
|
|
2023-06-17 20:57:57 +03:00
|
|
|
Future<Position> determinePosition() async {
|
|
|
|
LocationPermission permission;
|
|
|
|
|
|
|
|
permission = await Geolocator.checkPermission();
|
|
|
|
if (permission == LocationPermission.denied) {
|
|
|
|
permission = await Geolocator.requestPermission();
|
|
|
|
if (permission == LocationPermission.denied) {
|
|
|
|
return Future.error('Location permissions are denied');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (permission == LocationPermission.deniedForever) {
|
|
|
|
return Future.error(
|
|
|
|
'Location permissions are permanently denied, we cannot request permissions.');
|
|
|
|
}
|
|
|
|
return await Geolocator.getCurrentPosition(
|
|
|
|
desiredAccuracy: LocationAccuracy.high);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> setLocation() async {
|
|
|
|
if (settings.location) {
|
|
|
|
await getCurrentLocation();
|
|
|
|
} else {
|
2023-09-18 16:26:08 +03:00
|
|
|
if ((isar.locationCaches.where().findAllSync()).isNotEmpty) {
|
2023-06-17 20:57:57 +03:00
|
|
|
LocationCache locationCity =
|
2023-09-18 16:26:08 +03:00
|
|
|
(isar.locationCaches.where().findFirstSync())!;
|
2023-06-17 20:57:57 +03:00
|
|
|
await getLocation(locationCity.lat!, locationCity.lon!,
|
|
|
|
locationCity.district!, locationCity.city!);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> getCurrentLocation() async {
|
|
|
|
bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
|
2023-09-05 21:31:29 +03:00
|
|
|
|
2023-09-09 21:15:47 +03:00
|
|
|
if (!isOnline) {
|
2023-09-05 21:31:29 +03:00
|
|
|
showSnackBar(content: 'no_inter'.tr);
|
2023-06-17 20:57:57 +03:00
|
|
|
await readCache();
|
2023-09-09 21:15:47 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!serviceEnabled) {
|
2023-09-05 21:31:29 +03:00
|
|
|
showSnackBar(
|
2023-09-09 21:15:47 +03:00
|
|
|
content: 'no_location'.tr,
|
|
|
|
onPressed: () => Geolocator.openLocationSettings(),
|
|
|
|
);
|
2023-06-17 20:57:57 +03:00
|
|
|
await readCache();
|
2023-09-09 21:15:47 +03:00
|
|
|
return;
|
2023-06-17 20:57:57 +03:00
|
|
|
}
|
2023-09-09 21:15:47 +03:00
|
|
|
|
2023-09-15 15:29:45 +03:00
|
|
|
if ((isar.mainWeatherCaches.where().findAllSync()).isNotEmpty) {
|
|
|
|
await readCache();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-09 21:15:47 +03:00
|
|
|
Position position = await determinePosition();
|
|
|
|
List<Placemark> placemarks =
|
|
|
|
await placemarkFromCoordinates(position.latitude, position.longitude);
|
|
|
|
Placemark place = placemarks[0];
|
|
|
|
|
|
|
|
_latitude.value = position.latitude;
|
|
|
|
_longitude.value = position.longitude;
|
|
|
|
_district.value = '${place.administrativeArea}';
|
|
|
|
_city.value = '${place.locality}';
|
|
|
|
|
|
|
|
_mainWeather.value =
|
|
|
|
await WeatherAPI().getWeatherData(_latitude.value, _longitude.value);
|
|
|
|
|
2023-09-22 19:10:11 +03:00
|
|
|
notificationCheck();
|
2023-09-18 16:26:08 +03:00
|
|
|
|
2023-09-09 21:15:47 +03:00
|
|
|
await writeCache();
|
|
|
|
await readCache();
|
2023-06-17 20:57:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> getLocation(double latitude, double longitude, String district,
|
|
|
|
String locality) async {
|
2023-09-09 21:15:47 +03:00
|
|
|
if (!isOnline) {
|
2023-09-05 21:31:29 +03:00
|
|
|
showSnackBar(content: 'no_inter'.tr);
|
2023-06-17 20:57:57 +03:00
|
|
|
await readCache();
|
2023-09-15 15:29:45 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((isar.mainWeatherCaches.where().findAllSync()).isNotEmpty) {
|
|
|
|
await readCache();
|
2023-09-09 21:15:47 +03:00
|
|
|
return;
|
2023-06-17 20:57:57 +03:00
|
|
|
}
|
2023-09-09 21:15:47 +03:00
|
|
|
|
|
|
|
_latitude.value = latitude;
|
|
|
|
_longitude.value = longitude;
|
|
|
|
_district.value = district;
|
|
|
|
_city.value = locality;
|
|
|
|
|
|
|
|
_mainWeather.value =
|
|
|
|
await WeatherAPI().getWeatherData(_latitude.value, _longitude.value);
|
|
|
|
|
2023-09-22 19:10:11 +03:00
|
|
|
notificationCheck();
|
2023-09-09 21:15:47 +03:00
|
|
|
|
|
|
|
await writeCache();
|
|
|
|
await readCache();
|
2023-06-17 20:57:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> readCache() async {
|
|
|
|
MainWeatherCache? mainWeatherCache;
|
|
|
|
LocationCache? locationCache;
|
|
|
|
|
|
|
|
while (mainWeatherCache == null || locationCache == null) {
|
2023-09-18 16:26:08 +03:00
|
|
|
mainWeatherCache = isar.mainWeatherCaches.where().findFirstSync();
|
|
|
|
locationCache = isar.locationCaches.where().findFirstSync();
|
2023-06-17 20:57:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_mainWeather.value = mainWeatherCache;
|
|
|
|
_location.value = locationCache;
|
|
|
|
|
|
|
|
hourOfDay.value =
|
|
|
|
getTime(_mainWeather.value.time!, _mainWeather.value.timezone!);
|
|
|
|
dayOfNow.value =
|
|
|
|
getDay(_mainWeather.value.timeDaily!, _mainWeather.value.timezone!);
|
|
|
|
|
2023-10-01 22:38:43 +03:00
|
|
|
updateWidget();
|
|
|
|
|
2023-06-17 20:57:57 +03:00
|
|
|
isLoading.value = false;
|
2023-09-09 21:15:47 +03:00
|
|
|
|
|
|
|
Future.delayed(const Duration(milliseconds: 30), () {
|
2023-06-17 20:57:57 +03:00
|
|
|
itemScrollController.scrollTo(
|
|
|
|
index: hourOfDay.value,
|
|
|
|
duration: const Duration(seconds: 2),
|
|
|
|
curve: Curves.easeInOutCubic,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> writeCache() async {
|
|
|
|
final locationCaches = LocationCache(
|
|
|
|
lat: _latitude.value,
|
|
|
|
lon: _longitude.value,
|
|
|
|
city: _city.value,
|
|
|
|
district: _district.value,
|
|
|
|
);
|
|
|
|
|
2023-09-18 16:26:08 +03:00
|
|
|
isar.writeTxnSync(() {
|
2023-09-09 21:15:47 +03:00
|
|
|
final mainWeatherCachesIsEmpty =
|
2023-09-18 16:26:08 +03:00
|
|
|
(isar.mainWeatherCaches.where().findAllSync()).isEmpty;
|
2023-09-09 21:15:47 +03:00
|
|
|
final locationCachesIsEmpty =
|
2023-09-18 16:26:08 +03:00
|
|
|
(isar.locationCaches.where().findAllSync()).isEmpty;
|
2023-09-09 21:15:47 +03:00
|
|
|
|
|
|
|
if (mainWeatherCachesIsEmpty) {
|
2023-09-18 16:26:08 +03:00
|
|
|
isar.mainWeatherCaches.putSync(_mainWeather.value);
|
2023-06-17 20:57:57 +03:00
|
|
|
}
|
|
|
|
|
2023-09-09 21:15:47 +03:00
|
|
|
if (locationCachesIsEmpty) {
|
2023-09-18 16:26:08 +03:00
|
|
|
isar.locationCaches.putSync(locationCaches);
|
2023-06-17 20:57:57 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> deleteCache() async {
|
2023-09-22 19:10:11 +03:00
|
|
|
if (!isOnline) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
isar.writeTxnSync(() {
|
|
|
|
isar.mainWeatherCaches
|
|
|
|
.filter()
|
|
|
|
.timestampLessThan(cacheExpiry)
|
|
|
|
.deleteAllSync();
|
|
|
|
});
|
|
|
|
if ((isar.mainWeatherCaches.where().findAllSync()).isEmpty) {
|
|
|
|
await flutterLocalNotificationsPlugin.cancelAll();
|
2023-06-17 20:57:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> deleteAll(bool changeCity) async {
|
2023-09-09 21:15:47 +03:00
|
|
|
if (!isOnline) {
|
|
|
|
return;
|
2023-06-17 20:57:57 +03:00
|
|
|
}
|
2023-09-09 21:15:47 +03:00
|
|
|
|
|
|
|
bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
|
2023-09-22 19:10:11 +03:00
|
|
|
await flutterLocalNotificationsPlugin.cancelAll();
|
2023-09-09 21:15:47 +03:00
|
|
|
|
2023-09-18 16:26:08 +03:00
|
|
|
isar.writeTxnSync(() {
|
2023-09-09 21:15:47 +03:00
|
|
|
if (!settings.location) {
|
2023-09-18 16:26:08 +03:00
|
|
|
isar.mainWeatherCaches.where().deleteAllSync();
|
2023-09-09 21:15:47 +03:00
|
|
|
}
|
|
|
|
if ((settings.location && serviceEnabled) || changeCity) {
|
2023-09-18 16:26:08 +03:00
|
|
|
isar.mainWeatherCaches.where().deleteAllSync();
|
|
|
|
isar.locationCaches.where().deleteAllSync();
|
2023-09-09 21:15:47 +03:00
|
|
|
}
|
|
|
|
});
|
2023-06-17 20:57:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Card Weather
|
|
|
|
Future<void> addCardWeather(
|
|
|
|
double latitude, double longitude, String city, String district) async {
|
2023-09-18 18:33:43 +03:00
|
|
|
if (!isOnline) {
|
2023-09-05 21:31:29 +03:00
|
|
|
showSnackBar(content: 'no_inter'.tr);
|
2023-09-18 18:33:43 +03:00
|
|
|
return;
|
2023-06-17 20:57:57 +03:00
|
|
|
}
|
2023-09-18 18:33:43 +03:00
|
|
|
|
|
|
|
String tz = tzmap.latLngToTimezoneString(latitude, longitude);
|
|
|
|
_weatherCard.value = await WeatherAPI()
|
|
|
|
.getWeatherCard(latitude, longitude, city, district, tz);
|
|
|
|
isar.writeTxnSync(() {
|
|
|
|
weatherCards.add(_weatherCard.value);
|
|
|
|
isar.weatherCards.putSync(_weatherCard.value);
|
|
|
|
});
|
2023-06-17 20:57:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> updateCacheCard(bool refresh) async {
|
|
|
|
List<WeatherCard> weatherCard = refresh
|
2023-09-18 16:26:08 +03:00
|
|
|
? isar.weatherCards.where().sortByIndex().findAllSync()
|
|
|
|
: isar.weatherCards
|
2023-06-17 20:57:57 +03:00
|
|
|
.filter()
|
|
|
|
.timestampLessThan(cacheExpiry)
|
2023-07-25 21:21:21 +03:00
|
|
|
.sortByIndex()
|
2023-09-18 16:26:08 +03:00
|
|
|
.findAllSync();
|
2023-06-17 20:57:57 +03:00
|
|
|
|
2023-09-22 19:10:11 +03:00
|
|
|
if (!isOnline || weatherCard.isEmpty) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var oldCard in weatherCard) {
|
|
|
|
var updatedCard = await WeatherAPI().getWeatherCard(oldCard.lat,
|
|
|
|
oldCard.lon, oldCard.city!, oldCard.district!, oldCard.timezone!);
|
|
|
|
isar.writeTxnSync(() {
|
|
|
|
oldCard
|
|
|
|
..time = updatedCard.time
|
|
|
|
..temperature2M = updatedCard.temperature2M
|
|
|
|
..relativehumidity2M = updatedCard.relativehumidity2M
|
|
|
|
..apparentTemperature = updatedCard.apparentTemperature
|
|
|
|
..precipitation = updatedCard.precipitation
|
|
|
|
..rain = updatedCard.rain
|
|
|
|
..weathercode = updatedCard.weathercode
|
|
|
|
..surfacePressure = updatedCard.surfacePressure
|
|
|
|
..visibility = updatedCard.visibility
|
|
|
|
..evapotranspiration = updatedCard.evapotranspiration
|
|
|
|
..windspeed10M = updatedCard.windspeed10M
|
|
|
|
..winddirection10M = updatedCard.winddirection10M
|
|
|
|
..windgusts10M = updatedCard.windgusts10M
|
|
|
|
..timeDaily = updatedCard.timeDaily
|
|
|
|
..weathercodeDaily = updatedCard.weathercodeDaily
|
|
|
|
..temperature2MMax = updatedCard.temperature2MMax
|
|
|
|
..temperature2MMin = updatedCard.temperature2MMin
|
|
|
|
..apparentTemperatureMax = updatedCard.apparentTemperatureMax
|
|
|
|
..apparentTemperatureMin = updatedCard.apparentTemperatureMin
|
|
|
|
..sunrise = updatedCard.sunrise
|
|
|
|
..sunset = updatedCard.sunset
|
|
|
|
..precipitationSum = updatedCard.precipitationSum
|
|
|
|
..precipitationProbabilityMax =
|
|
|
|
updatedCard.precipitationProbabilityMax
|
|
|
|
..windspeed10MMax = updatedCard.windspeed10MMax
|
|
|
|
..windgusts10MMax = updatedCard.windgusts10MMax
|
|
|
|
..uvIndexMax = updatedCard.uvIndexMax
|
|
|
|
..rainSum = updatedCard.rainSum
|
|
|
|
..winddirection10MDominant = updatedCard.winddirection10MDominant
|
|
|
|
..timestamp = DateTime.now();
|
|
|
|
|
|
|
|
isar.weatherCards.putSync(oldCard);
|
|
|
|
|
|
|
|
var newCard = oldCard;
|
|
|
|
int oldIdx = weatherCard.indexOf(oldCard);
|
|
|
|
weatherCards[oldIdx] = newCard;
|
|
|
|
weatherCards.refresh();
|
|
|
|
});
|
2023-06-17 20:57:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> updateCard(WeatherCard weatherCard) async {
|
2023-09-18 18:33:43 +03:00
|
|
|
if (!isOnline) {
|
|
|
|
return;
|
2023-06-17 20:57:57 +03:00
|
|
|
}
|
2023-09-18 18:33:43 +03:00
|
|
|
|
|
|
|
final updatedCard = await WeatherAPI().getWeatherCard(
|
|
|
|
weatherCard.lat,
|
|
|
|
weatherCard.lon,
|
|
|
|
weatherCard.city!,
|
|
|
|
weatherCard.district!,
|
|
|
|
weatherCard.timezone!,
|
|
|
|
);
|
|
|
|
|
|
|
|
isar.writeTxnSync(() {
|
|
|
|
weatherCard
|
|
|
|
..time = updatedCard.time
|
|
|
|
..temperature2M = updatedCard.temperature2M
|
|
|
|
..relativehumidity2M = updatedCard.relativehumidity2M
|
|
|
|
..apparentTemperature = updatedCard.apparentTemperature
|
|
|
|
..precipitation = updatedCard.precipitation
|
|
|
|
..rain = updatedCard.rain
|
|
|
|
..weathercode = updatedCard.weathercode
|
|
|
|
..surfacePressure = updatedCard.surfacePressure
|
|
|
|
..visibility = updatedCard.visibility
|
|
|
|
..evapotranspiration = updatedCard.evapotranspiration
|
|
|
|
..windspeed10M = updatedCard.windspeed10M
|
|
|
|
..winddirection10M = updatedCard.winddirection10M
|
|
|
|
..windgusts10M = updatedCard.windgusts10M
|
|
|
|
..timeDaily = updatedCard.timeDaily
|
|
|
|
..weathercodeDaily = updatedCard.weathercodeDaily
|
|
|
|
..temperature2MMax = updatedCard.temperature2MMax
|
|
|
|
..temperature2MMin = updatedCard.temperature2MMin
|
|
|
|
..apparentTemperatureMax = updatedCard.apparentTemperatureMax
|
|
|
|
..apparentTemperatureMin = updatedCard.apparentTemperatureMin
|
|
|
|
..sunrise = updatedCard.sunrise
|
|
|
|
..sunset = updatedCard.sunset
|
|
|
|
..precipitationSum = updatedCard.precipitationSum
|
|
|
|
..precipitationProbabilityMax = updatedCard.precipitationProbabilityMax
|
|
|
|
..windspeed10MMax = updatedCard.windspeed10MMax
|
|
|
|
..windgusts10MMax = updatedCard.windgusts10MMax
|
|
|
|
..uvIndexMax = updatedCard.uvIndexMax
|
|
|
|
..rainSum = updatedCard.rainSum
|
|
|
|
..winddirection10MDominant = updatedCard.winddirection10MDominant
|
|
|
|
..timestamp = DateTime.now();
|
|
|
|
|
|
|
|
isar.weatherCards.putSync(weatherCard);
|
|
|
|
});
|
2023-06-17 20:57:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> deleteCardWeather(WeatherCard weatherCard) async {
|
2023-09-18 16:26:08 +03:00
|
|
|
isar.writeTxnSync(() {
|
2023-07-25 21:21:21 +03:00
|
|
|
weatherCards.remove(weatherCard);
|
2023-09-18 16:26:08 +03:00
|
|
|
isar.weatherCards.deleteSync(weatherCard.id);
|
2023-06-17 20:57:57 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
int getTime(List<String> time, String timezone) {
|
|
|
|
int getTime = 0;
|
|
|
|
for (var i = 0; i < time.length; i++) {
|
|
|
|
if (tz.TZDateTime.now(tz.getLocation(timezone)).hour ==
|
|
|
|
DateTime.parse(time[i]).hour &&
|
|
|
|
tz.TZDateTime.now(tz.getLocation(timezone)).day ==
|
|
|
|
DateTime.parse(time[i]).day) {
|
|
|
|
getTime = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return getTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getDay(List<DateTime> time, String timezone) {
|
|
|
|
int getDay = 0;
|
|
|
|
for (var i = 0; i < time.length; i++) {
|
|
|
|
if (tz.TZDateTime.now(tz.getLocation(timezone)).day == time[i].day) {
|
|
|
|
getDay = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return getDay;
|
|
|
|
}
|
2023-07-08 15:19:13 +03:00
|
|
|
|
2023-07-26 22:20:30 +03:00
|
|
|
TimeOfDay timeConvert(String normTime) {
|
|
|
|
int hh = 0;
|
|
|
|
if (normTime.endsWith('PM')) hh = 12;
|
|
|
|
normTime = normTime.split(' ')[0];
|
|
|
|
return TimeOfDay(
|
2023-09-24 18:52:15 +03:00
|
|
|
hour: hh + int.parse(normTime.split(':')[0]) % 24,
|
|
|
|
minute: int.parse(normTime.split(':')[1]) % 60,
|
2023-07-26 22:20:30 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-09-08 18:55:26 +03:00
|
|
|
Future<String> getLocalImagePath(String icon) async {
|
|
|
|
final directory = await getTemporaryDirectory();
|
|
|
|
final imagePath = '${directory.path}/$icon';
|
|
|
|
|
|
|
|
final ByteData data = await rootBundle.load('assets/images/$icon');
|
|
|
|
final List<int> bytes = data.buffer.asUint8List();
|
|
|
|
|
|
|
|
await File(imagePath).writeAsBytes(bytes);
|
|
|
|
|
|
|
|
return imagePath;
|
|
|
|
}
|
|
|
|
|
2023-07-26 22:20:30 +03:00
|
|
|
void notlification(MainWeatherCache mainWeatherCache) async {
|
|
|
|
DateTime now = DateTime.now();
|
|
|
|
int startHour = timeConvert(timeStart).hour;
|
|
|
|
int endHour = timeConvert(timeEnd).hour;
|
|
|
|
|
|
|
|
for (var i = 0; i < mainWeatherCache.time!.length; i += timeRange) {
|
|
|
|
DateTime notificationTime = DateTime.parse(mainWeatherCache.time![i]);
|
|
|
|
|
|
|
|
if (notificationTime.isAfter(now) &&
|
|
|
|
notificationTime.hour >= startHour &&
|
|
|
|
notificationTime.hour <= endHour) {
|
2023-09-08 18:55:26 +03:00
|
|
|
for (var j = 0; j < mainWeatherCache.timeDaily!.length; j++) {
|
|
|
|
if (mainWeatherCache.timeDaily![j].day == notificationTime.day) {
|
|
|
|
NotificationShow().showNotification(
|
|
|
|
UniqueKey().hashCode,
|
|
|
|
'$city: ${mainWeatherCache.temperature2M![i]}°',
|
|
|
|
'${StatusWeather().getText(mainWeatherCache.weathercode![i])} · ${StatusData().getTimeFormat(mainWeatherCache.time![i])}',
|
|
|
|
notificationTime,
|
|
|
|
StatusWeather().getImageNotification(
|
|
|
|
mainWeatherCache.weathercode![i],
|
|
|
|
mainWeatherCache.time![i],
|
|
|
|
mainWeatherCache.sunrise![j],
|
|
|
|
mainWeatherCache.sunset![j],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2023-07-08 15:19:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-05 21:31:29 +03:00
|
|
|
|
2023-09-22 19:10:11 +03:00
|
|
|
void notificationCheck() async {
|
|
|
|
if (settings.notifications) {
|
|
|
|
final List<PendingNotificationRequest> pendingNotificationRequests =
|
|
|
|
await flutterLocalNotificationsPlugin.pendingNotificationRequests();
|
|
|
|
if (pendingNotificationRequests.isEmpty) {
|
|
|
|
notlification(_mainWeather.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-05 21:31:29 +03:00
|
|
|
void reorder(oldIndex, newIndex) {
|
|
|
|
if (newIndex > oldIndex) {
|
|
|
|
newIndex -= 1;
|
|
|
|
}
|
|
|
|
final element = weatherCards.removeAt(oldIndex);
|
|
|
|
weatherCards.insert(newIndex, element);
|
|
|
|
|
|
|
|
for (int i = 0; i < weatherCards.length; i++) {
|
|
|
|
final item = weatherCards[i];
|
|
|
|
item.index = i;
|
2023-09-18 16:26:08 +03:00
|
|
|
isar.writeTxnSync(() => isar.weatherCards.putSync(item));
|
2023-09-05 21:31:29 +03:00
|
|
|
}
|
|
|
|
}
|
2023-10-01 22:38:43 +03:00
|
|
|
|
|
|
|
void updateWidget() async {
|
|
|
|
HomeWidget.saveWidgetData(
|
|
|
|
'weather_icon',
|
|
|
|
await getLocalImagePath(StatusWeather().getImageNotification(
|
|
|
|
mainWeather.weathercode![hourOfDay.value],
|
|
|
|
mainWeather.time![hourOfDay.value],
|
|
|
|
mainWeather.sunrise![dayOfNow.value],
|
|
|
|
mainWeather.sunset![dayOfNow.value],
|
|
|
|
)));
|
|
|
|
HomeWidget.saveWidgetData(
|
|
|
|
'weather_degree',
|
|
|
|
'${mainWeather.temperature2M?[hourOfDay.value].round()}°',
|
|
|
|
);
|
|
|
|
HomeWidget.updateWidget(androidName: androidWidgetName);
|
|
|
|
}
|
2023-06-17 20:57:57 +03:00
|
|
|
}
|