diff --git a/lib/main.dart b/lib/main.dart index d5df216..28afb6d 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -13,9 +13,10 @@ import 'package:alarm/alarm.dart'; import 'dart:convert'; /// TBD: -/// v0.3.0 +/// - Add other languages (Spanish, Croatian) +/// v1.1.0 /// - Change sunset grabber to checkbox and retrieve it each alarm or open. -/// v1.0.0 +/// v2.0.0 /// - Add sunrise section and change app name to Sunshine Alarm? /// :TBD @@ -71,12 +72,13 @@ class _HomeScreenState extends State { final FlutterLocalNotificationsPlugin _notifications = FlutterLocalNotificationsPlugin(); + bool _loading = false; bool _alarmEnabled = false; int _minutesBefore = 30; - bool _loading = false; + String _plural = 's'; - String? _sunsetErrorText = null; - String? _minuteErrorText = null; + String? _sunsetErrorText; + String? _minuteErrorText; static const _alarmId = 1001; @@ -202,21 +204,6 @@ class _HomeScreenState extends State { Timer? _alarmTimer; Future _scheduleAlarm() async { - // Get the Notifications permission: - final android = - _notifications - .resolvePlatformSpecificImplementation< - AndroidFlutterLocalNotificationsPlugin - >(); - await android?.requestNotificationsPermission(); - - // Ensure that Android has the correct alarm permissions. - final canSchedule = await android?.canScheduleExactNotifications(); - if (canSchedule != true) { - _showSnack('Permissions Need Granted, Requesting...'); - await android?.requestExactAlarmsPermission(); - } - final sunsetText = _sunsetController.text.trim(); if (sunsetText.isEmpty) return; @@ -259,7 +246,22 @@ class _HomeScreenState extends State { 'Alarm Enabled @ ${alarmTime.hour.toString().padLeft(2, '0')}:${alarmTime.minute.toString().padLeft(2, '0')} (App Must Stay Open!)', ); } else if (Platform.isAndroid) { - /*** Notifcation Method - Works ***/ + // Get the Notifications permission: + final android = + _notifications + .resolvePlatformSpecificImplementation< + AndroidFlutterLocalNotificationsPlugin + >(); + await android?.requestNotificationsPermission(); + + // Ensure that Android has the correct alarm permissions. + final canSchedule = await android?.canScheduleExactNotifications(); + if (canSchedule != true) { + _showSnack('Permissions Need Granted, Requesting...'); + await android?.requestExactAlarmsPermission(); + } + + /*** Notification Method - Works ***/ if (ANDROID_USE_NOTIF) { // Android: use zonedSchedule (works in background) final tzAlarm = tz.TZDateTime.from(alarmTime, tz.local); @@ -289,7 +291,7 @@ class _HomeScreenState extends State { } // End Notification Method ***/ - /*** Alarm Method - WIP ***/ + /*** Alarm Method - Works ***/ if (ANDROID_USE_ALARM) { // Schedule: final settings = AlarmSettings( @@ -305,7 +307,7 @@ class _HomeScreenState extends State { ), notificationSettings: NotificationSettings( title: 'Sunset Alarm', - body: 'Sun is setting in ${_minutesBefore} minute(s)!', + body: 'Sun is setting in ${_minutesBefore} minute${_plural}!', stopButton: 'Dismiss', icon: 'ic_notification', ), @@ -343,6 +345,7 @@ class _HomeScreenState extends State { @override void dispose() { + _savePrefs(); _latController.dispose(); _lngController.dispose(); _sunsetController.dispose(); @@ -519,6 +522,7 @@ class _HomeScreenState extends State { } else { _minuteErrorText = 'Invalid Minute Value'; } + _plural = (_minutesBefore == 1) ? '' : 's'; }); }, ), @@ -531,7 +535,7 @@ class _HomeScreenState extends State { title: Text(_alarmEnabled ? 'Sunset Alarm Enabled' : ''), subtitle: Text( _alarmEnabled - ? 'Will alert ${_minutesBefore} min before sunset. ($_alarmDisplayTime)' + ? '${_minutesBefore} Minute${_plural} Before @ $_alarmDisplayTime' : '', ), value: _alarmEnabled,