0.1.2 #1

Merged
me merged 40 commits from dev into main 2026-09-28 04:36:44 -07:00
Showing only changes of commit 71e6f4e13d - Show all commits
+51 -13
View File
@@ -8,6 +8,8 @@ import 'package:timezone/timezone.dart' as tz;
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'dart:io';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
tz.initializeTimeZones();
@@ -160,6 +162,8 @@ class _HomeScreenState extends State<HomeScreen> {
}
}
Timer? _alarmTimer;
Future<void> _scheduleAlarm() async {
final sunsetText = _sunsetController.text.trim();
if (sunsetText.isEmpty) return;
@@ -175,7 +179,6 @@ class _HomeScreenState extends State<HomeScreen> {
if (alarmTime.isBefore(now)) {
alarmTime = alarmTime.add(const Duration(days: 1));
}
alarmTime = alarmTime.subtract(Duration(minutes: _minutesBefore));
if (alarmTime.isBefore(now)) {
@@ -183,8 +186,29 @@ class _HomeScreenState extends State<HomeScreen> {
alarmTime = alarmTime.add(const Duration(days: 1));
}
if (Platform.isLinux) {
// Linux: use a Timer (app must stay open)
_alarmTimer?.cancel();
final delay = alarmTime.difference(now);
_alarmTimer = Timer(delay, () {
const linuxDetails = LinuxNotificationDetails(
urgency: LinuxNotificationUrgency.critical,
resident: true,
timeout: LinuxNotificationTimeout.expiresNever(),
);
_notifications.show(
_alarmId,
'Sunset in ${_minutesBefore} min',
'The sun sets at $sunsetText. Time to get ready!',
const NotificationDetails(linux: linuxDetails),
);
});
_showSnack(
'Alarm set for ${alarmTime.hour.toString().padLeft(2, '0')}:${alarmTime.minute.toString().padLeft(2, '0')} (app must stay open)',
);
} else {
// Android: use zonedSchedule (works in background)
final tzAlarm = tz.TZDateTime.from(alarmTime, tz.local);
const androidDetails = AndroidNotificationDetails(
'sunset_alarm',
'Sunset Alarm',
@@ -194,22 +218,18 @@ class _HomeScreenState extends State<HomeScreen> {
fullScreenIntent: true,
category: AndroidNotificationCategory.alarm,
);
const iosDetails = DarwinNotificationDetails(
presentAlert: true,
presentBadge: true,
presentSound: true,
);
await _notifications.zonedSchedule(
_alarmId,
'Sunset in ${_minutesBefore} min',
'The sun sets at $sunsetText. Time to get ready!',
tzAlarm,
NotificationDetails(android: androidDetails, iOS: iosDetails),
const NotificationDetails(android: androidDetails),
androidScheduleMode: AndroidScheduleMode.alarmClock,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
);
_showSnack('Alarm scheduled.');
}
}
Future<void> _toggleAlarm(bool value) async {
@@ -217,9 +237,11 @@ class _HomeScreenState extends State<HomeScreen> {
await _savePrefs();
if (value) {
await _scheduleAlarm();
_showSnack('Alarm scheduled.');
} else {
_alarmTimer?.cancel();
if (!Platform.isLinux) {
await _notifications.cancel(_alarmId);
}
_showSnack('Alarm cancelled.');
}
}
@@ -353,8 +375,7 @@ class _HomeScreenState extends State<HomeScreen> {
},
),
),
],
),
const SizedBox(height: 24),
// --- Toggle ---
@@ -362,7 +383,7 @@ class _HomeScreenState extends State<HomeScreen> {
title: const Text('Enable Sunset Alarm'),
subtitle: Text(
_alarmEnabled
? 'Will alert ${_minutesBefore} min before sunset'
? 'Will alert ${_minutesBefore} min before sunset. ($_alarmDisplayTime)'
: 'Alarm is off',
),
value: _alarmEnabled,
@@ -370,7 +391,24 @@ class _HomeScreenState extends State<HomeScreen> {
),
],
),
],
),
),
);
}
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')}';
}
}