Fix notification delivery and settings flows

This commit is contained in:
Dwindi Ramadhana
2026-05-31 20:40:20 +07:00
parent 5195ba19ad
commit 2bd8e3666a
12 changed files with 1419 additions and 435 deletions

View File

@@ -117,7 +117,7 @@ final selectedCityIdProvider = StateProvider<String>((ref) {
/// Provider for today's prayer times using myQuran API.
final prayerTimesProvider = FutureProvider<DaySchedule?>((ref) async {
final cityId = ref.watch(selectedCityIdProvider);
final cityId = await _resolveCityIdWithAutoDetect(ref);
final today = DateFormat('yyyy-MM-dd').format(DateTime.now());
DaySchedule? schedule;
@@ -202,6 +202,42 @@ final prayerTimesProvider = FutureProvider<DaySchedule?>((ref) async {
return fallbackSchedule;
});
Future<String> _resolveCityIdWithAutoDetect(Ref ref) async {
final settingsBox = Hive.box<AppSettings>(HiveBoxes.settings);
final settings = settingsBox.get('default') ?? AppSettings();
final stored = settings.lastCityName ?? '';
if (stored.contains('|')) {
return stored.split('|').last;
}
final position = await LocationService.instance.getCurrentLocation();
final fallbackLocation =
position == null ? LocationService.instance.getLastKnownLocation() : null;
final lat = position?.latitude ?? fallbackLocation?.lat;
final lng = position?.longitude ?? fallbackLocation?.lng;
if (lat != null && lng != null) {
try {
final resolved = await LocationService.instance
.resolveMyQuranCityFromCoordinates(lat: lat, lng: lng);
if (resolved != null) {
settings.lastCityName = '${resolved.name}|${resolved.id}';
if (settings.isInBox) {
await settings.save();
} else {
await settingsBox.put('default', settings);
}
ref.read(selectedCityIdProvider.notifier).state = resolved.id;
return resolved.id;
}
} catch (_) {
// Non-fatal: fallback to default city id.
}
}
return _defaultCityId;
}
Future<void> _syncAdhanNotifications(
String cityId, DaySchedule schedule) async {
try {