Fix notification delivery and settings flows
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -124,6 +124,14 @@ class _DzikirScreenState extends ConsumerState<DzikirScreen>
|
||||
return _todayKey;
|
||||
}
|
||||
|
||||
bool _isFocusMode(String mode) {
|
||||
return mode == 'focus' || mode == 'slide';
|
||||
}
|
||||
|
||||
bool _useCircleCounterButton(String position) {
|
||||
return position == 'fabCircle' || position == 'circle';
|
||||
}
|
||||
|
||||
Future<void> _loadData() async {
|
||||
_refreshTodayScope();
|
||||
setState(() {
|
||||
@@ -658,7 +666,7 @@ class _DzikirScreenState extends ConsumerState<DzikirScreen>
|
||||
.listenable(keys: ['default']),
|
||||
builder: (_, settingsBox, __) {
|
||||
final settings = settingsBox.get('default') ?? AppSettings();
|
||||
final isFocusMode = settings.dzikirDisplayMode == 'focus';
|
||||
final isFocusMode = _isFocusMode(settings.dzikirDisplayMode);
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
@@ -1172,7 +1180,9 @@ class _DzikirScreenState extends ConsumerState<DzikirScreen>
|
||||
);
|
||||
},
|
||||
),
|
||||
if (settings.dzikirCounterButtonPosition == 'fabCircle')
|
||||
if (_useCircleCounterButton(
|
||||
settings.dzikirCounterButtonPosition,
|
||||
))
|
||||
Positioned(
|
||||
right: 8,
|
||||
bottom: 12,
|
||||
|
||||
@@ -10,6 +10,19 @@ import '../../../data/services/notification_analytics_service.dart';
|
||||
import '../../../data/services/notification_inbox_service.dart';
|
||||
import '../../../data/services/notification_service.dart';
|
||||
|
||||
int _todayUpcomingAlarmCount(List<NotificationPendingAlert> alarms) {
|
||||
final now = DateTime.now();
|
||||
final todayStart = DateTime(now.year, now.month, now.day);
|
||||
final todayEnd = todayStart.add(const Duration(days: 1));
|
||||
return alarms.where((alarm) {
|
||||
final when = alarm.scheduledAt;
|
||||
if (when == null) return false;
|
||||
return !when.isBefore(now) &&
|
||||
!when.isBefore(todayStart) &&
|
||||
when.isBefore(todayEnd);
|
||||
}).length;
|
||||
}
|
||||
|
||||
class NotificationCenterScreen extends StatefulWidget {
|
||||
const NotificationCenterScreen({super.key});
|
||||
|
||||
@@ -56,6 +69,14 @@ class _NotificationCenterScreenState extends State<NotificationCenterScreen>
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _clearNonCritical() async {
|
||||
await NotificationInboxService.instance.clearNonCritical();
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Notifikasi non-kritis dibersihkan.')),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final isDark = Theme.of(context).brightness == Brightness.dark;
|
||||
@@ -91,10 +112,19 @@ class _NotificationCenterScreenState extends State<NotificationCenterScreen>
|
||||
builder: (context, _, __) {
|
||||
final unread =
|
||||
NotificationInboxService.instance.unreadCount();
|
||||
if (unread <= 0) return const SizedBox.shrink();
|
||||
return TextButton(
|
||||
onPressed: _markAllRead,
|
||||
child: const Text('Tandai semua'),
|
||||
return Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
TextButton(
|
||||
onPressed: _clearNonCritical,
|
||||
child: const Text('Bersihkan'),
|
||||
),
|
||||
if (unread > 0)
|
||||
TextButton(
|
||||
onPressed: _markAllRead,
|
||||
child: const Text('Tandai semua'),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
@@ -113,7 +143,9 @@ class _NotificationCenterScreenState extends State<NotificationCenterScreen>
|
||||
FutureBuilder<List<NotificationPendingAlert>>(
|
||||
future: _alarmsFuture,
|
||||
builder: (context, snapshot) {
|
||||
final count = snapshot.data?.length ?? 0;
|
||||
final count = _todayUpcomingAlarmCount(
|
||||
snapshot.data ?? const <NotificationPendingAlert>[],
|
||||
);
|
||||
return Tab(text: count > 0 ? 'Alarm ($count)' : 'Alarm');
|
||||
},
|
||||
),
|
||||
@@ -170,13 +202,21 @@ class _AlarmTabState extends State<_AlarmTab> {
|
||||
|
||||
final alarms = snapshot.data ?? const <NotificationPendingAlert>[];
|
||||
final now = DateTime.now();
|
||||
final upcoming = alarms
|
||||
.where((alarm) =>
|
||||
alarm.scheduledAt == null || !alarm.scheduledAt!.isBefore(now))
|
||||
.toList();
|
||||
final todayStart = DateTime(now.year, now.month, now.day);
|
||||
final todayEnd = todayStart.add(const Duration(days: 1));
|
||||
final upcoming = alarms.where((alarm) {
|
||||
final when = alarm.scheduledAt;
|
||||
if (when == null) return false;
|
||||
return !when.isBefore(now) &&
|
||||
!when.isBefore(todayStart) &&
|
||||
when.isBefore(todayEnd);
|
||||
}).toList();
|
||||
final passed = alarms
|
||||
.where((alarm) =>
|
||||
alarm.scheduledAt != null && alarm.scheduledAt!.isBefore(now))
|
||||
alarm.scheduledAt != null &&
|
||||
alarm.scheduledAt!.isBefore(now) &&
|
||||
!alarm.scheduledAt!.isBefore(todayStart) &&
|
||||
alarm.scheduledAt!.isBefore(todayEnd))
|
||||
.toList();
|
||||
final visible = _alarmFilter == 'past' ? passed : upcoming;
|
||||
|
||||
@@ -254,7 +294,7 @@ class _AlarmTabState extends State<_AlarmTab> {
|
||||
),
|
||||
child: Text(
|
||||
_alarmFilter == 'upcoming'
|
||||
? 'Tidak ada alarm akan datang.'
|
||||
? 'Tidak ada alarm tersisa untuk hari ini.'
|
||||
: 'Belum ada alarm sudah lewat.',
|
||||
textAlign: TextAlign.center,
|
||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
@@ -368,9 +408,7 @@ class _AlarmTabState extends State<_AlarmTab> {
|
||||
runSpacing: 8,
|
||||
children: [
|
||||
_FilterChip(
|
||||
label: upcomingCount > 0
|
||||
? 'Akan Datang ($upcomingCount)'
|
||||
: 'Akan Datang',
|
||||
label: upcomingCount > 0 ? 'Hari Ini ($upcomingCount)' : 'Hari Ini',
|
||||
selected: _alarmFilter == 'upcoming',
|
||||
isDark: isDark,
|
||||
onTap: () => setState(() => _alarmFilter = 'upcoming'),
|
||||
@@ -627,6 +665,16 @@ class _InboxTabState extends State<_InboxTab> {
|
||||
label: _inboxLabel(item.type),
|
||||
color: accent,
|
||||
),
|
||||
if ((item.meta['isGrouped'] == true) &&
|
||||
(item.meta['groupCount'] is num))
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(left: 8),
|
||||
child: _TypeBadge(
|
||||
label:
|
||||
'${(item.meta['groupCount'] as num).toInt()} item',
|
||||
color: AppColors.sage,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
_formatInboxTime(item.createdAt),
|
||||
@@ -694,6 +742,12 @@ class _InboxTabState extends State<_InboxTab> {
|
||||
isDark: isDark,
|
||||
onTap: () => setState(() => _filter = 'system'),
|
||||
),
|
||||
_FilterChip(
|
||||
label: 'Kritis',
|
||||
selected: _filter == 'critical',
|
||||
isDark: isDark,
|
||||
onTap: () => setState(() => _filter = 'critical'),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user