diff --git a/lib/app/modules/addOrUpdateAlarm/controllers/add_or_update_alarm_controller.dart b/lib/app/modules/addOrUpdateAlarm/controllers/add_or_update_alarm_controller.dart index baf81538..bdb56afd 100644 --- a/lib/app/modules/addOrUpdateAlarm/controllers/add_or_update_alarm_controller.dart +++ b/lib/app/modules/addOrUpdateAlarm/controllers/add_or_update_alarm_controller.dart @@ -740,6 +740,7 @@ class AddOrUpdateAlarmController extends GetxController { timeToAlarm.value = Utils.timeUntilAlarm( TimeOfDay.fromDateTime(selectedTime.value), repeatDays, + selectedDate.value, ); repeatDays.value = alarmRecord.value.days; @@ -850,6 +851,7 @@ class AddOrUpdateAlarmController extends GetxController { timeToAlarm.value = Utils.timeUntilAlarm( TimeOfDay.fromDateTime(selectedTime.value), repeatDays, + selectedDate.value ); // store initial values of the variables @@ -895,9 +897,16 @@ class AddOrUpdateAlarmController extends GetxController { selectedTime.listen((time) { debugPrint('CHANGED CHANGED CHANGED CHANGED'); timeToAlarm.value = - Utils.timeUntilAlarm(TimeOfDay.fromDateTime(time), repeatDays); + Utils.timeUntilAlarm(TimeOfDay.fromDateTime(time), repeatDays, selectedDate.value); _compareAndSetChange('selectedTime', time); }); + + selectedDate.listen((date) { + debugPrint('CHANGED CHANGED CHANGED CHANGED'); + timeToAlarm.value = + Utils.timeUntilAlarm(TimeOfDay.fromDateTime(selectedTime.value), repeatDays, date); + _compareAndSetChange('selectedTime', date); + }); //Updating UI to show repeated days repeatDays.listen((days) { @@ -1272,6 +1281,7 @@ class AddOrUpdateAlarmController extends GetxController { String timeToAlarm = Utils.timeUntilAlarm( Utils.stringToTimeOfDay(alarmRecord.alarmTime), alarmRecord.days, + Utils.stringToDate(alarmRecord.alarmDate), ); Fluttertoast.showToast( diff --git a/lib/app/modules/addOrUpdateAlarm/views/alarm_date_tile.dart b/lib/app/modules/addOrUpdateAlarm/views/alarm_date_tile.dart index bb628ef2..fc923f7c 100644 --- a/lib/app/modules/addOrUpdateAlarm/views/alarm_date_tile.dart +++ b/lib/app/modules/addOrUpdateAlarm/views/alarm_date_tile.dart @@ -19,7 +19,10 @@ class AlarmDateTile extends StatelessWidget { return Obx(() => InkWell( onTap: () async { - controller.datePicker(context); + await controller.datePicker(context); + if (controller.selectedDate.value != DateTime.now()) { + controller.repeatDays.value = [false, false, false, false, false, false, false]; + } }, child: ListTile( diff --git a/lib/app/modules/addOrUpdateAlarm/views/repeat_tile.dart b/lib/app/modules/addOrUpdateAlarm/views/repeat_tile.dart index 1c2a3432..cea9f4b0 100644 --- a/lib/app/modules/addOrUpdateAlarm/views/repeat_tile.dart +++ b/lib/app/modules/addOrUpdateAlarm/views/repeat_tile.dart @@ -70,6 +70,10 @@ class RepeatTile extends StatelessWidget { ), onPressed: () { Utils.hapticFeedback(); + if (controller.repeatDays != [false, false, false, false, false, false, false]) { + controller.selectedDate.value = DateTime.now(); + controller.isFutureDate.value = false; + } Get.back(); }, child: Text( diff --git a/lib/app/modules/home/controllers/home_controller.dart b/lib/app/modules/home/controllers/home_controller.dart index 449ca94f..b16abe06 100644 --- a/lib/app/modules/home/controllers/home_controller.dart +++ b/lib/app/modules/home/controllers/home_controller.dart @@ -327,6 +327,7 @@ class HomeController extends GetxController { String timeToAlarm = Utils.timeUntilAlarm( Utils.stringToTimeOfDay(latestAlarm.alarmTime), latestAlarm.days, + Utils.stringToDate(latestAlarm.alarmDate), ); alarmTime.value = 'Rings in $timeToAlarm'; // This function is necessary when alarms are deleted/enabled @@ -362,6 +363,7 @@ class HomeController extends GetxController { timeToAlarm = Utils.timeUntilAlarm( Utils.stringToTimeOfDay(latestAlarm.alarmTime), latestAlarm.days, + Utils.stringToDate(latestAlarm.alarmDate), ); alarmTime.value = 'Rings in $timeToAlarm'; @@ -377,6 +379,7 @@ class HomeController extends GetxController { timeToAlarm = Utils.timeUntilAlarm( Utils.stringToTimeOfDay(latestAlarm.alarmTime), latestAlarm.days, + Utils.stringToDate(latestAlarm.alarmDate), ); alarmTime.value = 'Rings in $timeToAlarm'; }); diff --git a/lib/app/utils/utils.dart b/lib/app/utils/utils.dart index 53c16127..8e5cdf9b 100644 --- a/lib/app/utils/utils.dart +++ b/lib/app/utils/utils.dart @@ -270,7 +270,7 @@ class Utils { return deg * (pi / 180); } - static String timeUntilAlarm(TimeOfDay alarmTime, List days) { + static String timeUntilAlarm(TimeOfDay alarmTime, List days, DateTime alarmDate) { final now = DateTime.now(); final todayAlarm = DateTime( now.year, @@ -282,7 +282,20 @@ class Utils { Duration duration; - // Check if the alarm is a one-time alarm + // If alarm is set for a specific date (future date) + if (alarmDate.isAfter(now)) { + final specificDateAlarm = DateTime( + alarmDate.year, + alarmDate.month, + alarmDate.day, + alarmTime.hour, + alarmTime.minute, + ); + duration = specificDateAlarm.difference(now); + return _formatTimeToRingDuration(duration); + } + + // Check if the alarm is a one-time alarm (no days selected) if (days.every((day) => !day)) { if (now.isBefore(todayAlarm)) { duration = todayAlarm.difference(now); @@ -292,25 +305,24 @@ class Utils { duration = nextAlarm.difference(now); } } else if (now.isBefore(todayAlarm) && days[now.weekday - 1]) { - duration = todayAlarm.difference(now); + duration = todayAlarm.difference(now); } else { + // Finding the next day when alarm will ring int daysUntilNextAlarm = 7; DateTime? nextAlarm; for (int i = 1; i <= 7; i++) { int nextDayIndex = (now.weekday + i - 1) % 7; - if (days[nextDayIndex]) { - if (i < daysUntilNextAlarm) { - daysUntilNextAlarm = i; - nextAlarm = DateTime( - now.year, - now.month, - now.day + i, - alarmTime.hour, - alarmTime.minute, - ); - } + daysUntilNextAlarm = i; + nextAlarm = DateTime( + now.year, + now.month, + now.day + i, + alarmTime.hour, + alarmTime.minute, + ); + break; } } @@ -321,6 +333,10 @@ class Utils { } } + return _formatTimeToRingDuration(duration); + } + + static String _formatTimeToRingDuration(Duration duration) { if (duration.inMinutes < 1) { return 'less than 1 minute'; } else if (duration.inHours < 24) { @@ -330,12 +346,10 @@ class Utils { return minutes == 1 ? '$minutes minute' : '$minutes minutes'; } else if (minutes == 0) { return hours == 1 ? '$hours hour' : '$hours hours'; - } else if (hours == 1) { - return minutes == 1 - ? '$hours hour $minutes minute' - : '$hours hour $minutes minutes'; } else { - return '$hours hour $minutes minutes'; + return hours == 1 + ? '$hours hour ${minutes == 1 ? "$minutes minute" : "$minutes minutes"}' + : '$hours hours ${minutes == 1 ? "$minutes minute" : "$minutes minutes"}'; } } else if (duration.inDays == 1) { return '1 day';