From fde73ee9277efcbd969b4c5ca6deb3965de73fdd Mon Sep 17 00:00:00 2001 From: Gautier de Lataillade <32983806+gdelataillade@users.noreply.github.com> Date: Wed, 27 Sep 2023 23:44:50 +0200 Subject: [PATCH] Refactor setAlarm methods arguments --- lib/alarm.dart | 18 ++---------------- lib/src/android_alarm.dart | 30 +++++++++++++----------------- lib/src/ios_alarm.dart | 31 +++++++++++++------------------ 3 files changed, 28 insertions(+), 51 deletions(-) diff --git a/lib/alarm.dart b/lib/alarm.dart index d6de96df..3848b1e0 100644 --- a/lib/alarm.dart +++ b/lib/alarm.dart @@ -103,27 +103,13 @@ class Alarm { if (iOS) { return IOSAlarm.setAlarm( - alarmSettings.id, - alarmSettings.dateTime, + alarmSettings, () => ringStream.add(alarmSettings), - alarmSettings.assetAudioPath, - alarmSettings.loopAudio, - alarmSettings.vibrate, - alarmSettings.volume, - alarmSettings.fadeDuration, - alarmSettings.enableNotificationOnKill, ); } else if (android) { return await AndroidAlarm.set( - alarmSettings.id, - alarmSettings.dateTime, + alarmSettings, () => ringStream.add(alarmSettings), - alarmSettings.assetAudioPath, - alarmSettings.loopAudio, - alarmSettings.vibrate, - alarmSettings.volume, - alarmSettings.fadeDuration, - alarmSettings.enableNotificationOnKill, ); } diff --git a/lib/src/android_alarm.dart b/lib/src/android_alarm.dart index 3a8b947a..173ef119 100644 --- a/lib/src/android_alarm.dart +++ b/lib/src/android_alarm.dart @@ -32,16 +32,10 @@ class AndroidAlarm { /// Creates isolate communication channel and set alarm at given [dateTime]. static Future set( - int id, - DateTime dateTime, + AlarmSettings settings, void Function()? onRing, - String assetAudioPath, - bool loopAudio, - bool vibrate, - double? volume, - double fadeDuration, - bool enableNotificationOnKill, ) async { + final id = settings.id; try { final port = ReceivePort(); final success = IsolateNameServer.registerPortWithName( @@ -57,15 +51,17 @@ class AndroidAlarm { alarmPrint('$message'); if (message == 'ring') { ringing = true; - if (volume != null) setMaximumVolume(); + if (settings.volume != null) setMaximumVolume(); onRing?.call(); } else { - if (vibrate && message is String && message.startsWith('vibrate')) { + if (settings.vibrate && + message is String && + message.startsWith('vibrate')) { final audioDuration = message.split('-').last; if (int.tryParse(audioDuration) != null) { final duration = Duration(seconds: int.parse(audioDuration)); - triggerVibrations(duration: loopAudio ? null : duration); + triggerVibrations(duration: settings.loopAudio ? null : duration); } } } @@ -75,7 +71,7 @@ class AndroidAlarm { } final res = await AndroidAlarmManager.oneShotAt( - dateTime, + settings.dateTime, id, AndroidAlarm.playAlarm, alarmClock: true, @@ -84,17 +80,17 @@ class AndroidAlarm { rescheduleOnReboot: true, wakeup: true, params: { - 'assetAudioPath': assetAudioPath, - 'loopAudio': loopAudio, - 'fadeDuration': fadeDuration, + 'assetAudioPath': settings.assetAudioPath, + 'loopAudio': settings.loopAudio, + 'fadeDuration': settings.fadeDuration, }, ); alarmPrint( - 'Alarm with id $id scheduled ${res ? 'successfully' : 'failed'} at $dateTime', + 'Alarm with id $id scheduled ${res ? 'successfully' : 'failed'} at ${settings.dateTime}', ); - if (enableNotificationOnKill && !hasOtherAlarms) { + if (settings.enableNotificationOnKill && !hasOtherAlarms) { try { await platform.invokeMethod( 'setNotificationOnKillService', diff --git a/lib/src/ios_alarm.dart b/lib/src/ios_alarm.dart index dde55d27..a35f7773 100644 --- a/lib/src/ios_alarm.dart +++ b/lib/src/ios_alarm.dart @@ -17,30 +17,25 @@ class IOSAlarm { /// Also set periodic timer and listens for app state changes to trigger /// the alarm ring callback at the right time. static Future setAlarm( - int id, - DateTime dateTime, + AlarmSettings settings, void Function()? onRing, - String assetAudio, - bool loopAudio, - bool vibrate, - double? volume, - double fadeDuration, - bool enableNotificationOnKill, ) async { + final id = settings.id; try { - final delay = dateTime.difference(DateTime.now()); + final delay = settings.dateTime.difference(DateTime.now()); final res = await methodChannel.invokeMethod( 'setAlarm', { 'id': id, - 'assetAudio': assetAudio, + 'assetAudio': settings.assetAudioPath, 'delayInSeconds': delay.inSeconds.abs().toDouble(), - 'loopAudio': loopAudio, - 'fadeDuration': fadeDuration >= 0 ? fadeDuration : 0, - 'vibrate': vibrate, - 'volume': volume, - 'notifOnKillEnabled': enableNotificationOnKill, + 'loopAudio': settings.loopAudio, + 'fadeDuration': + settings.fadeDuration >= 0 ? settings.fadeDuration : 0, + 'vibrate': settings.vibrate, + 'volumeMax': settings.volume == null, + 'notifOnKillEnabled': settings.enableNotificationOnKill, 'notifTitleOnAppKill': AlarmStorage.getNotificationOnAppKillTitle(), 'notifDescriptionOnAppKill': @@ -50,7 +45,7 @@ class IOSAlarm { false; alarmPrint( - 'Alarm with id $id scheduled ${res ? 'successfully' : 'failed'} at $dateTime', + 'Alarm with id $id scheduled ${res ? 'successfully' : 'failed'} at ${settings.dateTime}', ); if (!res) return false; @@ -60,7 +55,7 @@ class IOSAlarm { } if (timers[id] != null && timers[id]!.isActive) timers[id]!.cancel(); - timers[id] = periodicTimer(onRing, dateTime, id); + timers[id] = periodicTimer(onRing, settings.dateTime, id); listenAppStateChange( id: id, @@ -75,7 +70,7 @@ class IOSAlarm { onRing?.call(); } else { if (timers[id] != null && timers[id]!.isActive) timers[id]!.cancel(); - timers[id] = periodicTimer(onRing, dateTime, id); + timers[id] = periodicTimer(onRing, settings.dateTime, id); } }, );