Skip to content

Commit

Permalink
Fix and improve isRinging method
Browse files Browse the repository at this point in the history
  • Loading branch information
gdelataillade committed Oct 25, 2024
1 parent effce37 commit 6edab3b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ class AlarmPlugin : FlutterPlugin, MethodChannel.MethodCallHandler {
"isRinging" -> {
val id = call.argument<Int>("id")
val ringingAlarmIds = AlarmService.ringingAlarmIds
val isRinging = ringingAlarmIds.contains(id)
val isRinging = if (id == null) {
ringingAlarmIds.isNotEmpty()
} else {
ringingAlarmIds.contains(id)
}
result.success(isRinging)
}
"setWarningNotificationOnKill" -> {
Expand Down
20 changes: 6 additions & 14 deletions ios/Classes/SwiftAlarmPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ public class SwiftAlarmPlugin: NSObject, FlutterPlugin {
return
}
self.stopAlarm(id: id, cancelNotif: true, result: result)
case "audioCurrentTime":
guard let args = call.arguments as? [String: Any], let id = args["id"] as? Int else {
result(FlutterError(code: "NATIVE_ERR", message: "[SwiftAlarmPlugin] Error: id parameter is missing or invalid for audioCurrentTime", details: nil))
return
case "isRinging":
let id = call.arguments as? Int
if id == nil {
result(self.isAnyAlarmRinging())
} else {
result(self.alarms[id!]?.audioPlayer?.isPlaying ?? false)
}
self.audioCurrentTime(id: id, result: result)
case "setWarningNotificationOnKill":
guard let args = call.arguments as? [String: Any] else {
result(FlutterError(code: "NATIVE_ERR", message: "[SwiftAlarmPlugin] Error: Arguments are not in the expected format for setWarningNotificationOnKill", details: nil))
Expand Down Expand Up @@ -382,15 +383,6 @@ public class SwiftAlarmPlugin: NSObject, FlutterPlugin {
}
}

private func audioCurrentTime(id: Int, result: FlutterResult) {
if let audioPlayer = self.alarms[id]?.audioPlayer {
let time = Double(audioPlayer.currentTime)
result(time)
} else {
result(0.0)
}
}

private func backgroundFetch() {
self.mixOtherAudios()

Expand Down
9 changes: 6 additions & 3 deletions lib/alarm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,12 @@ class Alarm {
}

/// Whether the alarm is ringing.
static Future<bool> isRinging(int id) async => iOS
? await IOSAlarm.checkIfRinging(id)
: await AndroidAlarm.isRinging(id);
///
/// If no `id` is provided, it checks if any alarm is ringing.
/// If an `id` is provided, it checks if the specific alarm with that `id`
/// is ringing.
static Future<bool> isRinging([int? id]) async =>
iOS ? await IOSAlarm.isRinging(id) : await AndroidAlarm.isRinging(id);

/// Whether an alarm is set.
static bool hasAlarm() => AlarmStorage.hasAlarm();
Expand Down
7 changes: 4 additions & 3 deletions lib/src/android_alarm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,12 @@ class AndroidAlarm {
}
}

/// Checks if the alarm with given [id] is ringing.
static Future<bool> isRinging(int id) async {
/// Checks whether an alarm or any alarm (if id is null) is ringing.
static Future<bool> isRinging([int? id]) async {
try {
final res =
await methodChannel.invokeMethod('isRinging', {'id': id}) as bool;
await methodChannel.invokeMethod<bool>('isRinging', {'id': id}) ??
false;
return res;
} catch (e) {
alarmPrint('Failed to check if alarm is ringing: $e');
Expand Down
32 changes: 17 additions & 15 deletions lib/src/ios_alarm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';

import 'package:alarm/alarm.dart';
import 'package:alarm/utils/alarm_exception.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_fgbg/flutter_fgbg.dart';

Expand Down Expand Up @@ -62,9 +63,9 @@ class IOSAlarm {
onForeground: () async {
if (fgbgSubscriptions[id] == null) return;

final isRinging = await checkIfRinging(id);
final alarmIsRinging = await isRinging(id);

if (isRinging) {
if (alarmIsRinging) {
disposeAlarm(id);
Alarm.ringStream.add(settings);
} else {
Expand Down Expand Up @@ -108,19 +109,20 @@ class IOSAlarm {
.toList();
}

/// Checks whether alarm is ringing by getting the native audio player's
/// current time at two different moments. If the two values are different,
/// it means the alarm is ringing and then returns `true`.
static Future<bool> checkIfRinging(int id) async {
final pos1 = await methodChannel
.invokeMethod<double?>('audioCurrentTime', {'id': id}) ??
0.0;
await Future.delayed(const Duration(milliseconds: 100), () {});
final pos2 = await methodChannel
.invokeMethod<double?>('audioCurrentTime', {'id': id}) ??
0.0;

return pos2 > pos1;
/// Checks whether an alarm or any alarm (if id is null) is ringing.
static Future<bool> isRinging([int? id]) async {
try {
final res = await methodChannel.invokeMethod<bool>(
'isRinging',
{'id': id},
) ??
false;

return res;
} catch (e) {
debugPrint('Error checking if alarm is ringing: $e');
return false;
}
}

/// Listens when app goes foreground so we can check if alarm is ringing.
Expand Down

0 comments on commit 6edab3b

Please sign in to comment.