Alter message to show when the alarm is set for. Try fixing the alarm for Linux.
This commit is contained in:
1 parent
0e46307815
commit
71e6f4e13d
1 file changed
+81
-43
+81
-43
@@ -8,6 +8,8 @@ import 'package:timezone/timezone.dart' as tz;
|
|||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
void main() async {
|
void main() async {
|
||||||
WidgetsFlutterBinding.ensureInitialized();
|
WidgetsFlutterBinding.ensureInitialized();
|
||||||
tz.initializeTimeZones();
|
tz.initializeTimeZones();
|
||||||
@@ -160,6 +162,8 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Timer? _alarmTimer;
|
||||||
|
|
||||||
Future<void> _scheduleAlarm() async {
|
Future<void> _scheduleAlarm() async {
|
||||||
final sunsetText = _sunsetController.text.trim();
|
final sunsetText = _sunsetController.text.trim();
|
||||||
if (sunsetText.isEmpty) return;
|
if (sunsetText.isEmpty) return;
|
||||||
@@ -175,7 +179,6 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
if (alarmTime.isBefore(now)) {
|
if (alarmTime.isBefore(now)) {
|
||||||
alarmTime = alarmTime.add(const Duration(days: 1));
|
alarmTime = alarmTime.add(const Duration(days: 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
alarmTime = alarmTime.subtract(Duration(minutes: _minutesBefore));
|
alarmTime = alarmTime.subtract(Duration(minutes: _minutesBefore));
|
||||||
|
|
||||||
if (alarmTime.isBefore(now)) {
|
if (alarmTime.isBefore(now)) {
|
||||||
@@ -183,33 +186,50 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
alarmTime = alarmTime.add(const Duration(days: 1));
|
alarmTime = alarmTime.add(const Duration(days: 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
final tzAlarm = tz.TZDateTime.from(alarmTime, tz.local);
|
if (Platform.isLinux) {
|
||||||
|
// Linux: use a Timer (app must stay open)
|
||||||
const androidDetails = AndroidNotificationDetails(
|
_alarmTimer?.cancel();
|
||||||
'sunset_alarm',
|
final delay = alarmTime.difference(now);
|
||||||
'Sunset Alarm',
|
_alarmTimer = Timer(delay, () {
|
||||||
channelDescription: 'Alarm X minutes before sunset',
|
const linuxDetails = LinuxNotificationDetails(
|
||||||
importance: Importance.max,
|
urgency: LinuxNotificationUrgency.critical,
|
||||||
priority: Priority.high,
|
resident: true,
|
||||||
fullScreenIntent: true,
|
timeout: LinuxNotificationTimeout.expiresNever(),
|
||||||
category: AndroidNotificationCategory.alarm,
|
);
|
||||||
);
|
_notifications.show(
|
||||||
const iosDetails = DarwinNotificationDetails(
|
_alarmId,
|
||||||
presentAlert: true,
|
'Sunset in ${_minutesBefore} min',
|
||||||
presentBadge: true,
|
'The sun sets at $sunsetText. Time to get ready!',
|
||||||
presentSound: true,
|
const NotificationDetails(linux: linuxDetails),
|
||||||
);
|
);
|
||||||
|
});
|
||||||
await _notifications.zonedSchedule(
|
_showSnack(
|
||||||
_alarmId,
|
'Alarm set for ${alarmTime.hour.toString().padLeft(2, '0')}:${alarmTime.minute.toString().padLeft(2, '0')} (app must stay open)',
|
||||||
'Sunset in ${_minutesBefore} min',
|
);
|
||||||
'The sun sets at $sunsetText. Time to get ready!',
|
} else {
|
||||||
tzAlarm,
|
// Android: use zonedSchedule (works in background)
|
||||||
NotificationDetails(android: androidDetails, iOS: iosDetails),
|
final tzAlarm = tz.TZDateTime.from(alarmTime, tz.local);
|
||||||
androidScheduleMode: AndroidScheduleMode.alarmClock,
|
const androidDetails = AndroidNotificationDetails(
|
||||||
uiLocalNotificationDateInterpretation:
|
'sunset_alarm',
|
||||||
UILocalNotificationDateInterpretation.absoluteTime,
|
'Sunset Alarm',
|
||||||
);
|
channelDescription: 'Alarm X minutes before sunset',
|
||||||
|
importance: Importance.max,
|
||||||
|
priority: Priority.high,
|
||||||
|
fullScreenIntent: true,
|
||||||
|
category: AndroidNotificationCategory.alarm,
|
||||||
|
);
|
||||||
|
await _notifications.zonedSchedule(
|
||||||
|
_alarmId,
|
||||||
|
'Sunset in ${_minutesBefore} min',
|
||||||
|
'The sun sets at $sunsetText. Time to get ready!',
|
||||||
|
tzAlarm,
|
||||||
|
const NotificationDetails(android: androidDetails),
|
||||||
|
androidScheduleMode: AndroidScheduleMode.alarmClock,
|
||||||
|
uiLocalNotificationDateInterpretation:
|
||||||
|
UILocalNotificationDateInterpretation.absoluteTime,
|
||||||
|
);
|
||||||
|
_showSnack('Alarm scheduled.');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _toggleAlarm(bool value) async {
|
Future<void> _toggleAlarm(bool value) async {
|
||||||
@@ -217,9 +237,11 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
await _savePrefs();
|
await _savePrefs();
|
||||||
if (value) {
|
if (value) {
|
||||||
await _scheduleAlarm();
|
await _scheduleAlarm();
|
||||||
_showSnack('Alarm scheduled.');
|
|
||||||
} else {
|
} else {
|
||||||
await _notifications.cancel(_alarmId);
|
_alarmTimer?.cancel();
|
||||||
|
if (!Platform.isLinux) {
|
||||||
|
await _notifications.cancel(_alarmId);
|
||||||
|
}
|
||||||
_showSnack('Alarm cancelled.');
|
_showSnack('Alarm cancelled.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -353,24 +375,40 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
|
||||||
),
|
|
||||||
const SizedBox(height: 24),
|
|
||||||
|
|
||||||
// --- Toggle ---
|
const SizedBox(height: 24),
|
||||||
SwitchListTile(
|
|
||||||
title: const Text('Enable Sunset Alarm'),
|
// --- Toggle ---
|
||||||
subtitle: Text(
|
SwitchListTile(
|
||||||
_alarmEnabled
|
title: const Text('Enable Sunset Alarm'),
|
||||||
? 'Will alert ${_minutesBefore} min before sunset'
|
subtitle: Text(
|
||||||
: 'Alarm is off',
|
_alarmEnabled
|
||||||
),
|
? 'Will alert ${_minutesBefore} min before sunset. ($_alarmDisplayTime)'
|
||||||
value: _alarmEnabled,
|
: 'Alarm is off',
|
||||||
onChanged: _toggleAlarm,
|
),
|
||||||
|
value: _alarmEnabled,
|
||||||
|
onChanged: _toggleAlarm,
|
||||||
|
),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String get _alarmDisplayTime {
|
||||||
|
final sunsetText = _sunsetController.text.trim();
|
||||||
|
if (sunsetText.isEmpty) return '—';
|
||||||
|
final parts = sunsetText.split(':');
|
||||||
|
if (parts.length < 2) return '—';
|
||||||
|
final h = int.tryParse(parts[0]);
|
||||||
|
final m = int.tryParse(parts[1]);
|
||||||
|
if (h == null || m == null) return '—';
|
||||||
|
final alarmMinutes = h * 60 + m - _minutesBefore;
|
||||||
|
final displayMin = alarmMinutes % (24 * 60);
|
||||||
|
final dh = (displayMin ~/ 60) % 24;
|
||||||
|
final dm = displayMin % 60;
|
||||||
|
return '${dh.toString().padLeft(2, '0')}:${dm.toString().padLeft(2, '0')}';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user