Compare commits
3
Commits
810788e30e
...
71e6f4e13d
| 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 '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();
|
||||||
@@ -41,6 +43,7 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
final _latController = TextEditingController();
|
final _latController = TextEditingController();
|
||||||
final _lngController = TextEditingController();
|
final _lngController = TextEditingController();
|
||||||
final _sunsetController = TextEditingController();
|
final _sunsetController = TextEditingController();
|
||||||
|
final _minutesController = TextEditingController(text: '30');
|
||||||
|
|
||||||
final FlutterLocalNotificationsPlugin _notifications =
|
final FlutterLocalNotificationsPlugin _notifications =
|
||||||
FlutterLocalNotificationsPlugin();
|
FlutterLocalNotificationsPlugin();
|
||||||
@@ -59,14 +62,10 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _initNotifications() async {
|
Future<void> _initNotifications() async {
|
||||||
const androidInit =
|
const androidInit = AndroidInitializationSettings('@mipmap/ic_launcher');
|
||||||
AndroidInitializationSettings('@mipmap/ic_launcher');
|
const iosInit = DarwinInitializationSettings();
|
||||||
const linuxInit = DarwinInitializationSettings();
|
|
||||||
await _notifications.initialize(
|
await _notifications.initialize(
|
||||||
const InitializationSettings(
|
const InitializationSettings(android: androidInit, iOS: iosInit),
|
||||||
android: androidInit,
|
|
||||||
iOS: linuxInit,
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,6 +80,7 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
if (lat != null) _latController.text = lat.toString();
|
if (lat != null) _latController.text = lat.toString();
|
||||||
if (lng != null) _lngController.text = lng.toString();
|
if (lng != null) _lngController.text = lng.toString();
|
||||||
if (sunset != null) _sunsetController.text = sunset;
|
if (sunset != null) _sunsetController.text = sunset;
|
||||||
|
_minutesController.text = '$_minutesBefore';
|
||||||
});
|
});
|
||||||
if (_alarmEnabled && _sunsetController.text.isNotEmpty) {
|
if (_alarmEnabled && _sunsetController.text.isNotEmpty) {
|
||||||
_scheduleAlarm();
|
_scheduleAlarm();
|
||||||
@@ -148,7 +148,6 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
if (response.statusCode == 200) {
|
if (response.statusCode == 200) {
|
||||||
final data = jsonDecode(response.body);
|
final data = jsonDecode(response.body);
|
||||||
final sunsetStr = data['daily']['sunset'][0] as String;
|
final sunsetStr = data['daily']['sunset'][0] as String;
|
||||||
// "2026-09-12T19:42" -> "19:42"
|
|
||||||
final timePart = sunsetStr.split('T')[1];
|
final timePart = sunsetStr.split('T')[1];
|
||||||
setState(() => _sunsetController.text = timePart);
|
setState(() => _sunsetController.text = timePart);
|
||||||
await _savePrefs();
|
await _savePrefs();
|
||||||
@@ -163,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,12 +176,9 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
final m = int.parse(parts[1]);
|
final m = int.parse(parts[1]);
|
||||||
var alarmTime = DateTime(now.year, now.month, now.day, h, m);
|
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)) {
|
if (alarmTime.isBefore(now)) {
|
||||||
alarmTime = alarmTime.add(const Duration(days: 1));
|
alarmTime = alarmTime.add(const Duration(days: 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Subtract the "minutes before" offset
|
|
||||||
alarmTime = alarmTime.subtract(Duration(minutes: _minutesBefore));
|
alarmTime = alarmTime.subtract(Duration(minutes: _minutesBefore));
|
||||||
|
|
||||||
if (alarmTime.isBefore(now)) {
|
if (alarmTime.isBefore(now)) {
|
||||||
@@ -188,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 linuxDetails = 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: linuxDetails),
|
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 {
|
||||||
@@ -222,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.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -238,6 +255,7 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
_latController.dispose();
|
_latController.dispose();
|
||||||
_lngController.dispose();
|
_lngController.dispose();
|
||||||
_sunsetController.dispose();
|
_sunsetController.dispose();
|
||||||
|
_minutesController.dispose();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -251,16 +269,20 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
// --- Location section ---
|
// --- Location section ---
|
||||||
const Text('Location',
|
const Text(
|
||||||
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
|
'Location',
|
||||||
|
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
|
||||||
|
),
|
||||||
const SizedBox(height: 8),
|
const SizedBox(height: 8),
|
||||||
Row(
|
Row(
|
||||||
children: [
|
children: [
|
||||||
Expanded(
|
Expanded(
|
||||||
child: TextField(
|
child: TextField(
|
||||||
controller: _latController,
|
controller: _latController,
|
||||||
keyboardType:
|
keyboardType: const TextInputType.numberWithOptions(
|
||||||
const TextInputType.numberWithOptions(signed: true, decimal: true),
|
signed: true,
|
||||||
|
decimal: true,
|
||||||
|
),
|
||||||
decoration: const InputDecoration(
|
decoration: const InputDecoration(
|
||||||
labelText: 'Latitude',
|
labelText: 'Latitude',
|
||||||
border: OutlineInputBorder(),
|
border: OutlineInputBorder(),
|
||||||
@@ -272,8 +294,10 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
Expanded(
|
Expanded(
|
||||||
child: TextField(
|
child: TextField(
|
||||||
controller: _lngController,
|
controller: _lngController,
|
||||||
keyboardType:
|
keyboardType: const TextInputType.numberWithOptions(
|
||||||
const TextInputType.numberWithOptions(signed: true, decimal: true),
|
signed: true,
|
||||||
|
decimal: true,
|
||||||
|
),
|
||||||
decoration: const InputDecoration(
|
decoration: const InputDecoration(
|
||||||
labelText: 'Longitude',
|
labelText: 'Longitude',
|
||||||
border: OutlineInputBorder(),
|
border: OutlineInputBorder(),
|
||||||
@@ -292,8 +316,10 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
|
|
||||||
// --- Sunset time section ---
|
// --- Sunset time section ---
|
||||||
const Text('Sunset',
|
const Text(
|
||||||
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
|
'Sunset',
|
||||||
|
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
|
||||||
|
),
|
||||||
const SizedBox(height: 8),
|
const SizedBox(height: 8),
|
||||||
Row(
|
Row(
|
||||||
children: [
|
children: [
|
||||||
@@ -309,13 +335,14 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
),
|
),
|
||||||
const SizedBox(width: 8),
|
const SizedBox(width: 8),
|
||||||
IconButton.filledTonal(
|
IconButton.filledTonal(
|
||||||
icon: _loading
|
icon:
|
||||||
? const SizedBox(
|
_loading
|
||||||
width: 20,
|
? const SizedBox(
|
||||||
height: 20,
|
width: 20,
|
||||||
child: CircularProgressIndicator(strokeWidth: 2),
|
height: 20,
|
||||||
)
|
child: CircularProgressIndicator(strokeWidth: 2),
|
||||||
: const Icon(Icons.cloud_done_outlined),
|
)
|
||||||
|
: const Icon(Icons.cloud_done_outlined),
|
||||||
tooltip: 'Fetch sunset from Open-Meteo',
|
tooltip: 'Fetch sunset from Open-Meteo',
|
||||||
onPressed: _loading ? null : _fetchSunset,
|
onPressed: _loading ? null : _fetchSunset,
|
||||||
),
|
),
|
||||||
@@ -324,8 +351,10 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
|
|
||||||
// --- Minutes before ---
|
// --- Minutes before ---
|
||||||
const Text('Alert Before Sunset',
|
const Text(
|
||||||
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
|
'Alert Before Sunset',
|
||||||
|
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
|
||||||
|
),
|
||||||
const SizedBox(height: 8),
|
const SizedBox(height: 8),
|
||||||
Row(
|
Row(
|
||||||
children: [
|
children: [
|
||||||
@@ -333,34 +362,34 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
SizedBox(
|
SizedBox(
|
||||||
width: 80,
|
width: 80,
|
||||||
child: TextField(
|
child: TextField(
|
||||||
controller: TextEditingController(text: '$_minutesBefore'),
|
controller: _minutesController,
|
||||||
keyboardType: TextInputType.number,
|
keyboardType: TextInputType.number,
|
||||||
decoration:
|
decoration: const InputDecoration(
|
||||||
const InputDecoration(border: OutlineInputBorder()),
|
border: OutlineInputBorder(),
|
||||||
onEditingComplete: () async {
|
),
|
||||||
final val = int.tryParse(
|
onChanged: (v) {
|
||||||
(findContextController() as TextEditingController?)?.text ?? '');
|
final val = int.tryParse(v);
|
||||||
if (val != null && val > 0) {
|
if (val != null && val > 0) {
|
||||||
setState(() => _minutesBefore = val);
|
_minutesBefore = val;
|
||||||
await _savePrefs();
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
|
||||||
),
|
|
||||||
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,
|
||||||
|
),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
@@ -368,10 +397,18 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper to avoid recreating TextEditingController on each build
|
String get _alarmDisplayTime {
|
||||||
TextEditingController? _minutesController;
|
final sunsetText = _sunsetController.text.trim();
|
||||||
TextEditingController? findContextController() {
|
if (sunsetText.isEmpty) return '—';
|
||||||
_minutesController ??= TextEditingController(text: '$_minutesBefore');
|
final parts = sunsetText.split(':');
|
||||||
return _minutesController;
|
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