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

@@ -16,6 +16,8 @@ import '../../../data/local/hive_boxes.dart';
import '../../../data/local/models/app_settings.dart';
import '../../../data/local/models/daily_worship_log.dart';
import '../../../data/local/models/quran_bookmark.dart';
import '../../../data/services/location_service.dart';
import '../../../data/services/myquran_sholat_service.dart';
import '../../../data/services/notification_service.dart';
import '../data/prayer_times_provider.dart';
@@ -75,6 +77,307 @@ class _DashboardScreenState extends ConsumerState<DashboardScreen> {
setState(() {});
}
void _showAdhanControlInfo() {
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(
'Tombol Sound mengatur notifikasi adzan semua waktu shalat (Subuh, Dzuhur, Ashar, Maghrib, Isya).',
),
duration: Duration(seconds: 3),
),
);
}
void _showQuickLocationDialog(BuildContext context) {
final searchCtrl = TextEditingController();
bool isSearching = false;
bool isDetecting = false;
String? currentCityLabel;
String? currentCityId;
String? currentCityError;
List<Map<String, dynamic>> results = [];
Timer? debounce;
showDialog(
context: context,
builder: (ctx) => StatefulBuilder(
builder: (ctx, setDialogState) => AlertDialog(
insetPadding:
const EdgeInsets.symmetric(horizontal: 20, vertical: 24),
title: const Text('Ganti Kota'),
content: SizedBox(
width: MediaQuery.of(context).size.width * 0.85,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
width: double.infinity,
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: AppColors.primary.withValues(alpha: 0.08),
borderRadius: BorderRadius.circular(12),
border: Border.all(
color: AppColors.primary.withValues(alpha: 0.2),
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
const Icon(
LucideIcons.mapPin,
size: 16,
color: AppColors.primary,
),
const SizedBox(width: 8),
Expanded(
child: Text(
currentCityLabel == null
? 'Deteksi lokasi saat ini'
: 'Anda di $currentCityLabel',
style: const TextStyle(
fontSize: 13,
fontWeight: FontWeight.w600,
),
),
),
if (isDetecting)
const SizedBox(
width: 16,
height: 16,
child: CircularProgressIndicator(strokeWidth: 2),
),
],
),
if (currentCityError != null) ...[
const SizedBox(height: 6),
Text(
currentCityError!,
style: const TextStyle(
fontSize: 12,
color: Colors.red,
),
),
],
const SizedBox(height: 8),
Row(
children: [
TextButton(
onPressed: isDetecting
? null
: () async {
setDialogState(() {
isDetecting = true;
currentCityError = null;
});
try {
final pos = await LocationService.instance
.getCurrentLocation();
final fallbackLocation = pos == null
? LocationService.instance
.getLastKnownLocation()
: null;
final lat = pos?.latitude ??
fallbackLocation?.lat;
final lng = pos?.longitude ??
fallbackLocation?.lng;
if (lat == null || lng == null) {
setDialogState(() {
currentCityError =
'Lokasi tidak tersedia. Pastikan izin lokasi aktif.';
currentCityLabel = null;
currentCityId = null;
});
return;
}
final detectedLabel =
(await LocationService.instance
.getCityName(lat, lng))
.split(',')
.first
.trim();
final resolved = await LocationService
.instance
.resolveMyQuranCityFromCoordinates(
lat: lat,
lng: lng,
);
if (resolved == null) {
setDialogState(() {
final fallbackCity =
detectedLabel.isEmpty
? 'lokasi saat ini'
: detectedLabel;
currentCityError =
'Lokasi Anda terdeteksi di $fallbackCity. Kami belum menemukan ID kota yang cocok di data jadwal. Silakan cari manual kota terdekat.';
currentCityLabel = null;
currentCityId = null;
});
return;
}
setDialogState(() {
currentCityId = resolved.id;
currentCityLabel = resolved.name;
currentCityError = null;
});
} catch (_) {
setDialogState(() {
currentCityError =
'Deteksi lokasi gagal. Coba lagi.';
currentCityLabel = null;
currentCityId = null;
});
} finally {
if (ctx.mounted) {
setDialogState(() {
isDetecting = false;
});
}
}
},
child: const Text('Deteksi Lokasi'),
),
const SizedBox(width: 8),
if (currentCityId != null && currentCityLabel != null)
FilledButton(
onPressed: () {
final cityName = currentCityLabel!;
final cityId = currentCityId!;
final box =
Hive.box<AppSettings>(HiveBoxes.settings);
final settings =
box.get('default') ?? AppSettings();
settings.lastCityName = '$cityName|$cityId';
unawaited(settings.save());
ref.invalidate(cityNameProvider);
ref.invalidate(prayerTimesProvider);
unawaited(ref.read(prayerTimesProvider.future));
Navigator.pop(ctx);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Lokasi diubah ke $cityName'),
),
);
},
child: const Text('Gunakan Lokasi Ini'),
),
],
),
],
),
),
const SizedBox(height: 10),
TextField(
controller: searchCtrl,
decoration: const InputDecoration(
hintText: 'Cari kota/kabupaten...',
border: OutlineInputBorder(),
),
autofocus: true,
onChanged: (q) {
debounce?.cancel();
debounce =
Timer(const Duration(milliseconds: 350), () async {
final query = q.trim();
if (query.length < 2) {
setDialogState(() {
results = [];
});
return;
}
setDialogState(() {
isSearching = true;
});
try {
final found =
await MyQuranSholatService.instance.searchCity(
query,
);
if (!ctx.mounted) return;
setDialogState(() {
results = found;
isSearching = false;
});
} catch (_) {
if (!ctx.mounted) return;
setDialogState(() {
results = [];
isSearching = false;
});
}
});
},
),
const SizedBox(height: 10),
if (isSearching)
const Padding(
padding: EdgeInsets.symmetric(vertical: 12),
child: CircularProgressIndicator(),
)
else if (results.isEmpty)
const Padding(
padding: EdgeInsets.symmetric(vertical: 12),
child: Text(
'Ketik minimal 2 huruf untuk mencari kota.',
style: TextStyle(fontSize: 12),
),
)
else
SizedBox(
height: 260,
child: ListView.separated(
itemCount: results.length,
separatorBuilder: (_, __) => const Divider(height: 1),
itemBuilder: (_, i) {
final city = results[i];
final cityName = city['lokasi']?.toString() ?? '';
final cityId = city['id']?.toString() ?? '';
return ListTile(
dense: true,
title: Text(cityName),
onTap: () {
if (cityName.isEmpty || cityId.isEmpty) return;
final box =
Hive.box<AppSettings>(HiveBoxes.settings);
final settings =
box.get('default') ?? AppSettings();
settings.lastCityName = '$cityName|$cityId';
unawaited(settings.save());
ref.invalidate(cityNameProvider);
ref.invalidate(prayerTimesProvider);
unawaited(ref.read(prayerTimesProvider.future));
Navigator.pop(ctx);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Lokasi diubah ke $cityName'),
),
);
},
);
},
),
),
],
),
),
actions: [
TextButton(
onPressed: () => Navigator.pop(ctx),
child: const Text('Tutup'),
),
],
),
),
).whenComplete(() {
debounce?.cancel();
searchCtrl.dispose();
});
}
@override
void dispose() {
_countdownTimer?.cancel();
@@ -620,8 +923,26 @@ class _DashboardScreenState extends ConsumerState<DashboardScreen> {
),
),
const SizedBox(width: 12),
GestureDetector(
onTap: () => _showQuickLocationDialog(context),
child: Container(
width: 48,
height: 48,
decoration: BoxDecoration(
color: Colors.white.withValues(alpha: 0.2),
shape: BoxShape.circle,
),
child: const Icon(
LucideIcons.mapPin,
color: AppColors.onPrimary,
size: 20,
),
),
),
const SizedBox(width: 12),
GestureDetector(
onTap: _toggleAdhanFromHero,
onLongPress: _showAdhanControlInfo,
child: Container(
width: 48,
height: 48,