Skip to content

Commit 68873c0

Browse files
authored
fix: Display 'No Alarm Scheduled' Message When No Alarm is Set (#440)
* fix: issue 284 Display 'No Alarm Scheduled' Message When No Alarm is Set * refactor code * fix: issue 284 timer was not getting refreshed when stream is being updated.
1 parent 011b3c7 commit 68873c0

File tree

2 files changed

+915
-905
lines changed

2 files changed

+915
-905
lines changed

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

+43-48
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class HomeController extends GetxController {
3636
final alarmTime = 'No upcoming alarms!'.obs;
3737
bool refreshTimer = false;
3838
bool isEmpty = true;
39+
Timer? _delayTimer;
3940
Timer _timer = Timer.periodic(const Duration(milliseconds: 1), (timer) {});
4041
List alarms = [].obs;
4142
int lastRefreshTime = DateTime.now().millisecondsSinceEpoch;
@@ -252,8 +253,9 @@ class HomeController extends GetxController {
252253
delayToSchedule = Timer(const Duration(seconds: 1), () async {
253254
lastRefreshTime = DateTime.now().millisecondsSinceEpoch;
254255
// Cancel timer if we have to refresh
255-
if (refreshTimer == true && _timer.isActive) {
256-
_timer.cancel();
256+
if (refreshTimer == true) {
257+
if (_timer.isActive) _timer.cancel();
258+
if (_delayTimer != null) _delayTimer?.cancel();
257259
refreshTimer = false;
258260
}
259261

@@ -276,7 +278,6 @@ class HomeController extends GetxController {
276278
latestAlarm.days,
277279
);
278280
alarmTime.value = 'Rings in $timeToAlarm';
279-
280281
// This function is necessary when alarms are deleted/enabled
281282
await scheduleNextAlarm(
282283
alarmRecord,
@@ -296,38 +297,38 @@ class HomeController extends GetxController {
296297

297298
// Adding a delay till that difference between seconds upto the next
298299
// minute
299-
await Future.delayed(delay);
300-
301-
// Update the value of timeToAlarm only once till it settles it's time
302-
// with the upcoming alarm
303-
// Doing this because of an bug :
304-
// If we are not doing the below three lines of code the
305-
// time is not updating for 2 min after running
306-
// Why is it happening?? -> BECAUSE OUR VALUE WILL BE UPDATED
307-
// AFTER 1 MIN ACCORDING TO BELOW TIMER WHICH WILL CAUSE
308-
// MISCALCULATION FOR INITIAL MINUTES
309-
// This is just to make sure that our calculated time-to-alarm is
310-
// upto date with the real time for next alarm
311-
timeToAlarm = Utils.timeUntilAlarm(
312-
Utils.stringToTimeOfDay(latestAlarm.alarmTime),
313-
latestAlarm.days,
314-
);
315-
alarmTime.value = 'Rings in $timeToAlarm';
316-
317-
// Running a timer of periodic one minute as it is now in sync with
318-
// the current time
319-
_timer = Timer.periodic(
320-
Duration(
321-
milliseconds: Utils.getMillisecondsToAlarm(
322-
DateTime.now(),
323-
DateTime.now().add(const Duration(minutes: 1)),
324-
),
325-
), (timer) {
300+
_delayTimer = Timer(delay, () {
301+
// Update the value of timeToAlarm only once till it settles it's time
302+
// with the upcoming alarm
303+
// Doing this because of an bug :
304+
// If we are not doing the below three lines of code the
305+
// time is not updating for 2 min after running
306+
// Why is it happening?? -> BECAUSE OUR VALUE WILL BE UPDATED
307+
// AFTER 1 MIN ACCORDING TO BELOW TIMER WHICH WILL CAUSE
308+
// MISCALCULATION FOR INITIAL MINUTES
309+
// This is just to make sure that our calculated time-to-alarm is
310+
// upto date with the real time for next alarm
326311
timeToAlarm = Utils.timeUntilAlarm(
327312
Utils.stringToTimeOfDay(latestAlarm.alarmTime),
328313
latestAlarm.days,
329314
);
330315
alarmTime.value = 'Rings in $timeToAlarm';
316+
317+
// Running a timer of periodic one minute as it is now in sync with
318+
// the current time
319+
_timer = Timer.periodic(
320+
Duration(
321+
milliseconds: Utils.getMillisecondsToAlarm(
322+
DateTime.now(),
323+
DateTime.now().add(const Duration(minutes: 1)),
324+
),
325+
), (timer) {
326+
timeToAlarm = Utils.timeUntilAlarm(
327+
Utils.stringToTimeOfDay(latestAlarm.alarmTime),
328+
latestAlarm.days,
329+
);
330+
alarmTime.value = 'Rings in $timeToAlarm';
331+
});
331332
});
332333
} else {
333334
alarmTime.value = 'No upcoming alarms!';
@@ -567,7 +568,8 @@ class HomeController extends GetxController {
567568
content: Column(
568569
children: [
569570
Text(
570-
'This action will permanently delete these alarms from your device.'.tr,
571+
'This action will permanently delete these alarms from your device.'
572+
.tr,
571573
style: Theme.of(context).textTheme.bodyMedium,
572574
textAlign: TextAlign.center,
573575
),
@@ -583,17 +585,13 @@ class HomeController extends GetxController {
583585
Get.back();
584586
},
585587
style: ButtonStyle(
586-
backgroundColor:
587-
MaterialStateProperty.all(kprimaryColor),
588+
backgroundColor: MaterialStateProperty.all(kprimaryColor),
588589
),
589590
child: Text(
590591
'Cancel'.tr,
591-
style: Theme.of(context)
592-
.textTheme
593-
.displaySmall!
594-
.copyWith(
595-
color: kprimaryBackgroundColor,
596-
),
592+
style: Theme.of(context).textTheme.displaySmall!.copyWith(
593+
color: kprimaryBackgroundColor,
594+
),
597595
),
598596
),
599597
OutlinedButton(
@@ -613,7 +611,7 @@ class HomeController extends GetxController {
613611

614612
Get.offNamedUntil(
615613
'/bottom-navigation-bar',
616-
(route) => route.settings.name == '/splash-screen',
614+
(route) => route.settings.name == '/splash-screen',
617615
);
618616
},
619617
style: OutlinedButton.styleFrom(
@@ -626,14 +624,11 @@ class HomeController extends GetxController {
626624
),
627625
child: Text(
628626
'Okay'.tr,
629-
style: Theme.of(context)
630-
.textTheme
631-
.displaySmall!
632-
.copyWith(
633-
color: themeController.isLightMode.value
634-
? Colors.red.withOpacity(0.9)
635-
: Colors.red,
636-
),
627+
style: Theme.of(context).textTheme.displaySmall!.copyWith(
628+
color: themeController.isLightMode.value
629+
? Colors.red.withOpacity(0.9)
630+
: Colors.red,
631+
),
637632
),
638633
),
639634
],

0 commit comments

Comments
 (0)