From 5d202c8e12808921c93bafeb221b9d02dd657f44 Mon Sep 17 00:00:00 2001 From: Gaurav Kushwaha Date: Tue, 22 Apr 2025 10:51:40 +0530 Subject: [PATCH 1/3] Stage 1: Added alarmDate parameter to timeUntilAlarm --- .../add_or_update_alarm_controller.dart | 12 ++++++- .../home/controllers/home_controller.dart | 3 ++ lib/app/utils/utils.dart | 32 ++++++++++++++----- 3 files changed, 38 insertions(+), 9 deletions(-) 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/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..88d27144 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,14 +305,15 @@ class Utils { duration = nextAlarm.difference(now); } } else if (now.isBefore(todayAlarm) && days[now.weekday - 1]) { + // If alarm is set for today and time hasn't passed 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; @@ -321,6 +335,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 +348,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'; From ec099d9a719536b9af3bb6d1d4e98ff1d502d74f Mon Sep 17 00:00:00 2001 From: Gaurav Kushwaha Date: Fri, 25 Apr 2025 00:30:57 +0530 Subject: [PATCH 2/3] Updated and Controller --- lib/app/modules/addOrUpdateAlarm/views/alarm_date_tile.dart | 5 ++++- lib/app/modules/addOrUpdateAlarm/views/repeat_tile.dart | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) 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( From 7803a1e1204f7270305fc2d3451ae479c74921ad Mon Sep 17 00:00:00 2001 From: Gaurav Kushwaha Date: Fri, 25 Apr 2025 01:37:39 +0530 Subject: [PATCH 3/3] Done --- lib/app/utils/utils.dart | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/lib/app/utils/utils.dart b/lib/app/utils/utils.dart index 88d27144..8e5cdf9b 100644 --- a/lib/app/utils/utils.dart +++ b/lib/app/utils/utils.dart @@ -305,8 +305,7 @@ class Utils { duration = nextAlarm.difference(now); } } else if (now.isBefore(todayAlarm) && days[now.weekday - 1]) { - // If alarm is set for today and time hasn't passed - duration = todayAlarm.difference(now); + duration = todayAlarm.difference(now); } else { // Finding the next day when alarm will ring int daysUntilNextAlarm = 7; @@ -315,16 +314,15 @@ class Utils { 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; } }