Skip to content

Commit

Permalink
Refactor setAlarm methods arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
gdelataillade committed Sep 27, 2023
1 parent 872b74a commit fde73ee
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 51 deletions.
18 changes: 2 additions & 16 deletions lib/alarm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}

Expand Down
30 changes: 13 additions & 17 deletions lib/src/android_alarm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,10 @@ class AndroidAlarm {

/// Creates isolate communication channel and set alarm at given [dateTime].
static Future<bool> 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(
Expand All @@ -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);
}
}
}
Expand All @@ -75,7 +71,7 @@ class AndroidAlarm {
}

final res = await AndroidAlarmManager.oneShotAt(
dateTime,
settings.dateTime,
id,
AndroidAlarm.playAlarm,
alarmClock: true,
Expand All @@ -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',
Expand Down
31 changes: 13 additions & 18 deletions lib/src/ios_alarm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> 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<bool?>(
'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':
Expand All @@ -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;
Expand All @@ -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,
Expand All @@ -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);
}
},
);
Expand Down

0 comments on commit fde73ee

Please sign in to comment.