Skip to content

Commit 5d202c8

Browse files
Stage 1: Added alarmDate parameter to timeUntilAlarm
1 parent 255ba43 commit 5d202c8

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

lib/app/modules/addOrUpdateAlarm/controllers/add_or_update_alarm_controller.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,7 @@ class AddOrUpdateAlarmController extends GetxController {
740740
timeToAlarm.value = Utils.timeUntilAlarm(
741741
TimeOfDay.fromDateTime(selectedTime.value),
742742
repeatDays,
743+
selectedDate.value,
743744
);
744745

745746
repeatDays.value = alarmRecord.value.days;
@@ -850,6 +851,7 @@ class AddOrUpdateAlarmController extends GetxController {
850851
timeToAlarm.value = Utils.timeUntilAlarm(
851852
TimeOfDay.fromDateTime(selectedTime.value),
852853
repeatDays,
854+
selectedDate.value
853855
);
854856

855857
// store initial values of the variables
@@ -895,9 +897,16 @@ class AddOrUpdateAlarmController extends GetxController {
895897
selectedTime.listen((time) {
896898
debugPrint('CHANGED CHANGED CHANGED CHANGED');
897899
timeToAlarm.value =
898-
Utils.timeUntilAlarm(TimeOfDay.fromDateTime(time), repeatDays);
900+
Utils.timeUntilAlarm(TimeOfDay.fromDateTime(time), repeatDays, selectedDate.value);
899901
_compareAndSetChange('selectedTime', time);
900902
});
903+
904+
selectedDate.listen((date) {
905+
debugPrint('CHANGED CHANGED CHANGED CHANGED');
906+
timeToAlarm.value =
907+
Utils.timeUntilAlarm(TimeOfDay.fromDateTime(selectedTime.value), repeatDays, date);
908+
_compareAndSetChange('selectedTime', date);
909+
});
901910

902911
//Updating UI to show repeated days
903912
repeatDays.listen((days) {
@@ -1272,6 +1281,7 @@ class AddOrUpdateAlarmController extends GetxController {
12721281
String timeToAlarm = Utils.timeUntilAlarm(
12731282
Utils.stringToTimeOfDay(alarmRecord.alarmTime),
12741283
alarmRecord.days,
1284+
Utils.stringToDate(alarmRecord.alarmDate),
12751285
);
12761286

12771287
Fluttertoast.showToast(

lib/app/modules/home/controllers/home_controller.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ class HomeController extends GetxController {
327327
String timeToAlarm = Utils.timeUntilAlarm(
328328
Utils.stringToTimeOfDay(latestAlarm.alarmTime),
329329
latestAlarm.days,
330+
Utils.stringToDate(latestAlarm.alarmDate),
330331
);
331332
alarmTime.value = 'Rings in $timeToAlarm';
332333
// This function is necessary when alarms are deleted/enabled
@@ -362,6 +363,7 @@ class HomeController extends GetxController {
362363
timeToAlarm = Utils.timeUntilAlarm(
363364
Utils.stringToTimeOfDay(latestAlarm.alarmTime),
364365
latestAlarm.days,
366+
Utils.stringToDate(latestAlarm.alarmDate),
365367
);
366368
alarmTime.value = 'Rings in $timeToAlarm';
367369

@@ -377,6 +379,7 @@ class HomeController extends GetxController {
377379
timeToAlarm = Utils.timeUntilAlarm(
378380
Utils.stringToTimeOfDay(latestAlarm.alarmTime),
379381
latestAlarm.days,
382+
Utils.stringToDate(latestAlarm.alarmDate),
380383
);
381384
alarmTime.value = 'Rings in $timeToAlarm';
382385
});

lib/app/utils/utils.dart

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ class Utils {
270270
return deg * (pi / 180);
271271
}
272272

273-
static String timeUntilAlarm(TimeOfDay alarmTime, List<bool> days) {
273+
static String timeUntilAlarm(TimeOfDay alarmTime, List<bool> days, DateTime alarmDate) {
274274
final now = DateTime.now();
275275
final todayAlarm = DateTime(
276276
now.year,
@@ -282,7 +282,20 @@ class Utils {
282282

283283
Duration duration;
284284

285-
// Check if the alarm is a one-time alarm
285+
// If alarm is set for a specific date (future date)
286+
if (alarmDate.isAfter(now)) {
287+
final specificDateAlarm = DateTime(
288+
alarmDate.year,
289+
alarmDate.month,
290+
alarmDate.day,
291+
alarmTime.hour,
292+
alarmTime.minute,
293+
);
294+
duration = specificDateAlarm.difference(now);
295+
return _formatTimeToRingDuration(duration);
296+
}
297+
298+
// Check if the alarm is a one-time alarm (no days selected)
286299
if (days.every((day) => !day)) {
287300
if (now.isBefore(todayAlarm)) {
288301
duration = todayAlarm.difference(now);
@@ -292,14 +305,15 @@ class Utils {
292305
duration = nextAlarm.difference(now);
293306
}
294307
} else if (now.isBefore(todayAlarm) && days[now.weekday - 1]) {
308+
// If alarm is set for today and time hasn't passed
295309
duration = todayAlarm.difference(now);
296310
} else {
311+
// Finding the next day when alarm will ring
297312
int daysUntilNextAlarm = 7;
298313
DateTime? nextAlarm;
299314

300315
for (int i = 1; i <= 7; i++) {
301316
int nextDayIndex = (now.weekday + i - 1) % 7;
302-
303317
if (days[nextDayIndex]) {
304318
if (i < daysUntilNextAlarm) {
305319
daysUntilNextAlarm = i;
@@ -321,6 +335,10 @@ class Utils {
321335
}
322336
}
323337

338+
return _formatTimeToRingDuration(duration);
339+
}
340+
341+
static String _formatTimeToRingDuration(Duration duration) {
324342
if (duration.inMinutes < 1) {
325343
return 'less than 1 minute';
326344
} else if (duration.inHours < 24) {
@@ -330,12 +348,10 @@ class Utils {
330348
return minutes == 1 ? '$minutes minute' : '$minutes minutes';
331349
} else if (minutes == 0) {
332350
return hours == 1 ? '$hours hour' : '$hours hours';
333-
} else if (hours == 1) {
334-
return minutes == 1
335-
? '$hours hour $minutes minute'
336-
: '$hours hour $minutes minutes';
337351
} else {
338-
return '$hours hour $minutes minutes';
352+
return hours == 1
353+
? '$hours hour ${minutes == 1 ? "$minutes minute" : "$minutes minutes"}'
354+
: '$hours hours ${minutes == 1 ? "$minutes minute" : "$minutes minutes"}';
339355
}
340356
} else if (duration.inDays == 1) {
341357
return '1 day';

0 commit comments

Comments
 (0)