Skip to content

Resolves issue#827, where timeUntilAlarm wasn't working as Expected #833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,7 @@ class AddOrUpdateAlarmController extends GetxController {
timeToAlarm.value = Utils.timeUntilAlarm(
TimeOfDay.fromDateTime(selectedTime.value),
repeatDays,
selectedDate.value,
);

repeatDays.value = alarmRecord.value.days;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -1272,6 +1281,7 @@ class AddOrUpdateAlarmController extends GetxController {
String timeToAlarm = Utils.timeUntilAlarm(
Utils.stringToTimeOfDay(alarmRecord.alarmTime),
alarmRecord.days,
Utils.stringToDate(alarmRecord.alarmDate),
);

Fluttertoast.showToast(
Expand Down
5 changes: 4 additions & 1 deletion lib/app/modules/addOrUpdateAlarm/views/alarm_date_tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(

Expand Down
4 changes: 4 additions & 0 deletions lib/app/modules/addOrUpdateAlarm/views/repeat_tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 3 additions & 0 deletions lib/app/modules/home/controllers/home_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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';

Expand All @@ -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';
});
Expand Down
52 changes: 33 additions & 19 deletions lib/app/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ class Utils {
return deg * (pi / 180);
}

static String timeUntilAlarm(TimeOfDay alarmTime, List<bool> days) {
static String timeUntilAlarm(TimeOfDay alarmTime, List<bool> days, DateTime alarmDate) {
final now = DateTime.now();
final todayAlarm = DateTime(
now.year,
Expand All @@ -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);
Expand All @@ -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;
}
}

Expand All @@ -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) {
Expand All @@ -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';
Expand Down