Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
71e6f4e13d | ||
|
|
0e46307815 | ||
|
|
dc15af60dd |
+121
-84
@@ -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();
|
||||
@@ -41,6 +43,7 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
final _latController = TextEditingController();
|
||||
final _lngController = TextEditingController();
|
||||
final _sunsetController = TextEditingController();
|
||||
final _minutesController = TextEditingController(text: '30');
|
||||
|
||||
final FlutterLocalNotificationsPlugin _notifications =
|
||||
FlutterLocalNotificationsPlugin();
|
||||
@@ -59,14 +62,10 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
}
|
||||
|
||||
Future<void> _initNotifications() async {
|
||||
const androidInit =
|
||||
AndroidInitializationSettings('@mipmap/ic_launcher');
|
||||
const linuxInit = DarwinInitializationSettings();
|
||||
const androidInit = AndroidInitializationSettings('@mipmap/ic_launcher');
|
||||
const iosInit = DarwinInitializationSettings();
|
||||
await _notifications.initialize(
|
||||
const InitializationSettings(
|
||||
android: androidInit,
|
||||
iOS: linuxInit,
|
||||
),
|
||||
const InitializationSettings(android: androidInit, iOS: iosInit),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -81,6 +80,7 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
if (lat != null) _latController.text = lat.toString();
|
||||
if (lng != null) _lngController.text = lng.toString();
|
||||
if (sunset != null) _sunsetController.text = sunset;
|
||||
_minutesController.text = '$_minutesBefore';
|
||||
});
|
||||
if (_alarmEnabled && _sunsetController.text.isNotEmpty) {
|
||||
_scheduleAlarm();
|
||||
@@ -148,7 +148,6 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
if (response.statusCode == 200) {
|
||||
final data = jsonDecode(response.body);
|
||||
final sunsetStr = data['daily']['sunset'][0] as String;
|
||||
// "2026-09-12T19:42" -> "19:42"
|
||||
final timePart = sunsetStr.split('T')[1];
|
||||
setState(() => _sunsetController.text = timePart);
|
||||
await _savePrefs();
|
||||
@@ -163,6 +162,8 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
}
|
||||
}
|
||||
|
||||
Timer? _alarmTimer;
|
||||
|
||||
Future<void> _scheduleAlarm() async {
|
||||
final sunsetText = _sunsetController.text.trim();
|
||||
if (sunsetText.isEmpty) return;
|
||||
@@ -175,12 +176,9 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
final m = int.parse(parts[1]);
|
||||
var alarmTime = DateTime(now.year, now.month, now.day, h, m);
|
||||
|
||||
// If the alarm time has already passed today, schedule for tomorrow
|
||||
if (alarmTime.isBefore(now)) {
|
||||
alarmTime = alarmTime.add(const Duration(days: 1));
|
||||
}
|
||||
|
||||
// Subtract the "minutes before" offset
|
||||
alarmTime = alarmTime.subtract(Duration(minutes: _minutesBefore));
|
||||
|
||||
if (alarmTime.isBefore(now)) {
|
||||
@@ -188,33 +186,50 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
alarmTime = alarmTime.add(const Duration(days: 1));
|
||||
}
|
||||
|
||||
final tzAlarm = tz.TZDateTime.from(alarmTime, tz.local);
|
||||
|
||||
const androidDetails = AndroidNotificationDetails(
|
||||
'sunset_alarm',
|
||||
'Sunset Alarm',
|
||||
channelDescription: 'Alarm X minutes before sunset',
|
||||
importance: Importance.max,
|
||||
priority: Priority.high,
|
||||
fullScreenIntent: true,
|
||||
category: AndroidNotificationCategory.alarm,
|
||||
);
|
||||
const linuxDetails = 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: linuxDetails),
|
||||
androidScheduleMode: AndroidScheduleMode.alarmClock,
|
||||
uiLocalNotificationDateInterpretation:
|
||||
UILocalNotificationDateInterpretation.absoluteTime,
|
||||
);
|
||||
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',
|
||||
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 {
|
||||
@@ -222,9 +237,11 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
await _savePrefs();
|
||||
if (value) {
|
||||
await _scheduleAlarm();
|
||||
_showSnack('Alarm scheduled.');
|
||||
} else {
|
||||
await _notifications.cancel(_alarmId);
|
||||
_alarmTimer?.cancel();
|
||||
if (!Platform.isLinux) {
|
||||
await _notifications.cancel(_alarmId);
|
||||
}
|
||||
_showSnack('Alarm cancelled.');
|
||||
}
|
||||
}
|
||||
@@ -238,6 +255,7 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
_latController.dispose();
|
||||
_lngController.dispose();
|
||||
_sunsetController.dispose();
|
||||
_minutesController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@@ -251,16 +269,20 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// --- Location section ---
|
||||
const Text('Location',
|
||||
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
|
||||
const Text(
|
||||
'Location',
|
||||
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: TextField(
|
||||
controller: _latController,
|
||||
keyboardType:
|
||||
const TextInputType.numberWithOptions(signed: true, decimal: true),
|
||||
keyboardType: const TextInputType.numberWithOptions(
|
||||
signed: true,
|
||||
decimal: true,
|
||||
),
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Latitude',
|
||||
border: OutlineInputBorder(),
|
||||
@@ -272,8 +294,10 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
Expanded(
|
||||
child: TextField(
|
||||
controller: _lngController,
|
||||
keyboardType:
|
||||
const TextInputType.numberWithOptions(signed: true, decimal: true),
|
||||
keyboardType: const TextInputType.numberWithOptions(
|
||||
signed: true,
|
||||
decimal: true,
|
||||
),
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Longitude',
|
||||
border: OutlineInputBorder(),
|
||||
@@ -292,8 +316,10 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// --- Sunset time section ---
|
||||
const Text('Sunset',
|
||||
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
|
||||
const Text(
|
||||
'Sunset',
|
||||
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Row(
|
||||
children: [
|
||||
@@ -309,13 +335,14 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
IconButton.filledTonal(
|
||||
icon: _loading
|
||||
? const SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Icon(Icons.cloud_done_outlined),
|
||||
icon:
|
||||
_loading
|
||||
? const SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Icon(Icons.cloud_done_outlined),
|
||||
tooltip: 'Fetch sunset from Open-Meteo',
|
||||
onPressed: _loading ? null : _fetchSunset,
|
||||
),
|
||||
@@ -324,8 +351,10 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// --- Minutes before ---
|
||||
const Text('Alert Before Sunset',
|
||||
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
|
||||
const Text(
|
||||
'Alert Before Sunset',
|
||||
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Row(
|
||||
children: [
|
||||
@@ -333,34 +362,34 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
SizedBox(
|
||||
width: 80,
|
||||
child: TextField(
|
||||
controller: TextEditingController(text: '$_minutesBefore'),
|
||||
controller: _minutesController,
|
||||
keyboardType: TextInputType.number,
|
||||
decoration:
|
||||
const InputDecoration(border: OutlineInputBorder()),
|
||||
onEditingComplete: () async {
|
||||
final val = int.tryParse(
|
||||
(findContextController() as TextEditingController?)?.text ?? '');
|
||||
decoration: const InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
onChanged: (v) {
|
||||
final val = int.tryParse(v);
|
||||
if (val != null && val > 0) {
|
||||
setState(() => _minutesBefore = val);
|
||||
await _savePrefs();
|
||||
_minutesBefore = val;
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
|
||||
// --- Toggle ---
|
||||
SwitchListTile(
|
||||
title: const Text('Enable Sunset Alarm'),
|
||||
subtitle: Text(
|
||||
_alarmEnabled
|
||||
? 'Will alert ${_minutesBefore} min before sunset'
|
||||
: 'Alarm is off',
|
||||
),
|
||||
value: _alarmEnabled,
|
||||
onChanged: _toggleAlarm,
|
||||
const SizedBox(height: 24),
|
||||
|
||||
// --- Toggle ---
|
||||
SwitchListTile(
|
||||
title: const Text('Enable Sunset Alarm'),
|
||||
subtitle: Text(
|
||||
_alarmEnabled
|
||||
? 'Will alert ${_minutesBefore} min before sunset. ($_alarmDisplayTime)'
|
||||
: 'Alarm is off',
|
||||
),
|
||||
value: _alarmEnabled,
|
||||
onChanged: _toggleAlarm,
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
@@ -368,10 +397,18 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
// Helper to avoid recreating TextEditingController on each build
|
||||
TextEditingController? _minutesController;
|
||||
TextEditingController? findContextController() {
|
||||
_minutesController ??= TextEditingController(text: '$_minutesBefore');
|
||||
return _minutesController;
|
||||
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