Fix the minutes field to be persistent. (via Ask Brave)
This commit is contained in:
1 parent
810788e30e
commit
dc15af60dd
1 file changed
+11
-20
+11
-20
@@ -41,6 +41,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();
|
||||||
@@ -61,11 +62,11 @@ 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 linuxInit = DarwinInitializationSettings();
|
const iosInit = DarwinInitializationSettings();
|
||||||
await _notifications.initialize(
|
await _notifications.initialize(
|
||||||
const InitializationSettings(
|
const InitializationSettings(
|
||||||
android: androidInit,
|
android: androidInit,
|
||||||
iOS: linuxInit,
|
iOS: iosInit,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -81,6 +82,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 +150,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();
|
||||||
@@ -175,12 +176,10 @@ 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)) {
|
||||||
@@ -199,7 +198,7 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
fullScreenIntent: true,
|
fullScreenIntent: true,
|
||||||
category: AndroidNotificationCategory.alarm,
|
category: AndroidNotificationCategory.alarm,
|
||||||
);
|
);
|
||||||
const linuxDetails = DarwinNotificationDetails(
|
const iosDetails = DarwinNotificationDetails(
|
||||||
presentAlert: true,
|
presentAlert: true,
|
||||||
presentBadge: true,
|
presentBadge: true,
|
||||||
presentSound: true,
|
presentSound: true,
|
||||||
@@ -210,7 +209,7 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
'Sunset in ${_minutesBefore} min',
|
'Sunset in ${_minutesBefore} min',
|
||||||
'The sun sets at $sunsetText. Time to get ready!',
|
'The sun sets at $sunsetText. Time to get ready!',
|
||||||
tzAlarm,
|
tzAlarm,
|
||||||
NotificationDetails(android: androidDetails, iOS: linuxDetails),
|
NotificationDetails(android: androidDetails, iOS: iosDetails),
|
||||||
androidScheduleMode: AndroidScheduleMode.alarmClock,
|
androidScheduleMode: AndroidScheduleMode.alarmClock,
|
||||||
uiLocalNotificationDateInterpretation:
|
uiLocalNotificationDateInterpretation:
|
||||||
UILocalNotificationDateInterpretation.absoluteTime,
|
UILocalNotificationDateInterpretation.absoluteTime,
|
||||||
@@ -238,6 +237,7 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
_latController.dispose();
|
_latController.dispose();
|
||||||
_lngController.dispose();
|
_lngController.dispose();
|
||||||
_sunsetController.dispose();
|
_sunsetController.dispose();
|
||||||
|
_minutesController.dispose();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -333,16 +333,14 @@ 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(border: OutlineInputBorder()),
|
const InputDecoration(border: OutlineInputBorder()),
|
||||||
onEditingComplete: () async {
|
onChanged: (v) {
|
||||||
final val = int.tryParse(
|
final val = int.tryParse(v);
|
||||||
(findContextController() as TextEditingController?)?.text ?? '');
|
|
||||||
if (val != null && val > 0) {
|
if (val != null && val > 0) {
|
||||||
setState(() => _minutesBefore = val);
|
_minutesBefore = val;
|
||||||
await _savePrefs();
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
@@ -367,11 +365,4 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper to avoid recreating TextEditingController on each build
|
|
||||||
TextEditingController? _minutesController;
|
|
||||||
TextEditingController? findContextController() {
|
|
||||||
_minutesController ??= TextEditingController(text: '$_minutesBefore');
|
|
||||||
return _minutesController;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user