main, use global variables for now to decide between notifications and alarms on Android, may become a setting later.
This commit is contained in:
1 parent
7f908a7fab
commit
4926c869b9
1 file changed
+14
-4
+14
-4
@@ -6,6 +6,7 @@ import 'package:geolocator/geolocator.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
import 'package:timezone/data/latest_all.dart' as tz;
|
||||
import 'package:timezone/timezone.dart' as tz;
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:alarm/alarm.dart';
|
||||
|
||||
@@ -25,6 +26,9 @@ void main() async {
|
||||
runApp(const SunsetAlarmApp());
|
||||
}
|
||||
|
||||
const ANDROID_USE_NOTIF = false;
|
||||
const ANDROID_USE_ALARM = !ANDROID_USE_NOTIF;
|
||||
|
||||
class SunsetAlarmApp extends StatelessWidget {
|
||||
const SunsetAlarmApp({super.key});
|
||||
|
||||
@@ -251,7 +255,8 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
'Alarm Enabled @ ${alarmTime.hour.toString().padLeft(2, '0')}:${alarmTime.minute.toString().padLeft(2, '0')} (App Must Stay Open!)',
|
||||
);
|
||||
} else if (Platform.isAndroid) {
|
||||
/*** Notifcation Method - Works *** /
|
||||
/*** Notifcation Method - Works ***/
|
||||
if (ANDROID_USE_NOTIF) {
|
||||
// Android: use zonedSchedule (works in background)
|
||||
final tzAlarm = tz.TZDateTime.from(alarmTime, tz.local);
|
||||
const androidDetails = AndroidNotificationDetails(
|
||||
@@ -268,16 +273,20 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
title: 'Sunset in ${_minutesBefore} min',
|
||||
body: 'The sun sets at $sunsetText. Time to get ready!',
|
||||
scheduledDate: tzAlarm,
|
||||
notificationDetails: const NotificationDetails(android: androidDetails),
|
||||
notificationDetails: const NotificationDetails(
|
||||
android: androidDetails,
|
||||
),
|
||||
androidScheduleMode: AndroidScheduleMode.alarmClock,
|
||||
);
|
||||
_showSnack('Alarm scheduled!');
|
||||
debugPrint('DEBUG: Alarm time: ${tzAlarm.toString()}');
|
||||
debugPrint('DEBUG: Current time: ${DateTime.now().toUtc().toString()}');
|
||||
debugPrint('DEBUG: ${_notifications.toString()}');
|
||||
}
|
||||
// End Notification Method ***/
|
||||
|
||||
/*** Alarm Method - WIP ***/
|
||||
if (ANDROID_USE_ALARM) {
|
||||
// Schedule:
|
||||
final settings = AlarmSettings(
|
||||
id: _alarmId,
|
||||
@@ -297,6 +306,7 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
),
|
||||
);
|
||||
await Alarm.set(alarmSettings: settings);
|
||||
}
|
||||
// End Alarm Method ***/
|
||||
_showSnack('Alarm Scheduled @ ${alarmTime}');
|
||||
} else {
|
||||
@@ -313,10 +323,10 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
_alarmTimer?.cancel();
|
||||
if (!Platform.isLinux) {
|
||||
// Notification Method
|
||||
//await _notifications.cancel(id: _alarmId);
|
||||
if (ANDROID_USE_NOTIF) await _notifications.cancel(id: _alarmId);
|
||||
|
||||
// Alarm Method
|
||||
await Alarm.stop(_alarmId);
|
||||
if (ANDROID_USE_ALARM) await Alarm.stop(_alarmId);
|
||||
}
|
||||
_showSnack('Alarm Cancelled');
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user