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 _lngController = TextEditingController();
|
||||
final _sunsetController = TextEditingController();
|
||||
final _minutesController = TextEditingController(text: '30');
|
||||
|
||||
final FlutterLocalNotificationsPlugin _notifications =
|
||||
FlutterLocalNotificationsPlugin();
|
||||
@@ -61,11 +62,11 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
Future<void> _initNotifications() async {
|
||||
const androidInit =
|
||||
AndroidInitializationSettings('@mipmap/ic_launcher');
|
||||
const linuxInit = DarwinInitializationSettings();
|
||||
const iosInit = DarwinInitializationSettings();
|
||||
await _notifications.initialize(
|
||||
const InitializationSettings(
|
||||
android: androidInit,
|
||||
iOS: linuxInit,
|
||||
iOS: iosInit,
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -81,6 +82,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 +150,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();
|
||||
@@ -175,12 +176,10 @@ 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)) {
|
||||
@@ -199,7 +198,7 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
fullScreenIntent: true,
|
||||
category: AndroidNotificationCategory.alarm,
|
||||
);
|
||||
const linuxDetails = DarwinNotificationDetails(
|
||||
const iosDetails = DarwinNotificationDetails(
|
||||
presentAlert: true,
|
||||
presentBadge: true,
|
||||
presentSound: true,
|
||||
@@ -210,7 +209,7 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
'Sunset in ${_minutesBefore} min',
|
||||
'The sun sets at $sunsetText. Time to get ready!',
|
||||
tzAlarm,
|
||||
NotificationDetails(android: androidDetails, iOS: linuxDetails),
|
||||
NotificationDetails(android: androidDetails, iOS: iosDetails),
|
||||
androidScheduleMode: AndroidScheduleMode.alarmClock,
|
||||
uiLocalNotificationDateInterpretation:
|
||||
UILocalNotificationDateInterpretation.absoluteTime,
|
||||
@@ -238,6 +237,7 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
_latController.dispose();
|
||||
_lngController.dispose();
|
||||
_sunsetController.dispose();
|
||||
_minutesController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@@ -333,16 +333,14 @@ 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 ?? '');
|
||||
onChanged: (v) {
|
||||
final val = int.tryParse(v);
|
||||
if (val != null && val > 0) {
|
||||
setState(() => _minutesBefore = val);
|
||||
await _savePrefs();
|
||||
_minutesBefore = val;
|
||||
}
|
||||
},
|
||||
),
|
||||
@@ -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