Skip to content

Commit

Permalink
Add volume enforcement for iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
gdelataillade committed Nov 1, 2024
1 parent aed55f3 commit 4f781cd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
5 changes: 4 additions & 1 deletion ios/Classes/AlarmConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@ class AlarmConfiguration {
let loopAudio: Bool
let fadeDuration: Double
let volume: Float?
var volumeEnforced: Bool
var volumeEnforcementTimer: Timer?
var triggerTime: Date?
var audioPlayer: AVAudioPlayer?
var timer: Timer?
var task: DispatchWorkItem?

init(id: Int, assetAudio: String, vibrationsEnabled: Bool, loopAudio: Bool, fadeDuration: Double, volume: Float?) {
init(id: Int, assetAudio: String, vibrationsEnabled: Bool, loopAudio: Bool, fadeDuration: Double, volume: Float?, volumeEnforced: Bool) {
self.id = id
self.assetAudio = assetAudio
self.vibrationsEnabled = vibrationsEnabled
self.loopAudio = loopAudio
self.fadeDuration = fadeDuration
self.volume = volume
self.volumeEnforced = volumeEnforced
}
}
14 changes: 13 additions & 1 deletion ios/Classes/SwiftAlarmPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ public class SwiftAlarmPlugin: NSObject, FlutterPlugin {
vibrationsEnabled: alarmSettings.vibrate,
loopAudio: alarmSettings.loopAudio,
fadeDuration: alarmSettings.fadeDuration,
volume: volumeFloat
volume: volumeFloat,
volumeEnforced: alarmSettings.volumeEnforced
)

self.alarms[id] = alarmConfig
Expand Down Expand Up @@ -290,6 +291,16 @@ public class SwiftAlarmPlugin: NSObject, FlutterPlugin {
} else {
audioPlayer.volume = 1.0
}

if alarm.volumeEnforced {
alarm.volumeEnforcementTimer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { [weak self] timer in
guard let self = self else { return }
let currentSystemVolume = self.getSystemVolume()
if abs(currentSystemVolume - targetSystemVolume) > 0.01 {
self.setVolume(volume: targetSystemVolume, enable: false)
}
}
}
}

private func getSystemVolume() -> Float {
Expand Down Expand Up @@ -341,6 +352,7 @@ public class SwiftAlarmPlugin: NSObject, FlutterPlugin {
alarm.timer?.invalidate()
alarm.task?.cancel()
alarm.audioPlayer?.stop()
alarm.volumeEnforcementTimer?.invalidate()
self.alarms.removeValue(forKey: id)
}

Expand Down
14 changes: 9 additions & 5 deletions ios/Classes/models/AlarmSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct AlarmSettings: Codable {
let warningNotificationOnKill: Bool
let androidFullScreenIntent: Bool
let notificationSettings: NotificationSettings
let volumeEnforced: Bool

static func fromJson(json: [String: Any]) -> AlarmSettings? {
guard let id = json["id"] as? Int,
Expand All @@ -29,10 +30,11 @@ struct AlarmSettings: Codable {
let maxValidMicroseconds: Int64 = 9223372036854775 // Corresponding to year 2262
let safeDateTimeMicros = min(dateTimeMicros, maxValidMicroseconds)

let dateTime = Date(timeIntervalSince1970: TimeInterval(safeDateTimeMicros) / 1_000_000)
let volume = json["volume"] as? Double
let dateTime: Date = Date(timeIntervalSince1970: TimeInterval(safeDateTimeMicros) / 1_000_000)
let volume: Double? = json["volume"] as? Double
let notificationSettings = NotificationSettings.fromJson(json: notificationSettingsDict)

let volumeEnforced: Bool = json["volumeEnforced"] as? Bool ?? false

return AlarmSettings(
id: id,
dateTime: dateTime,
Expand All @@ -43,7 +45,8 @@ struct AlarmSettings: Codable {
fadeDuration: fadeDuration,
warningNotificationOnKill: warningNotificationOnKill,
androidFullScreenIntent: androidFullScreenIntent,
notificationSettings: notificationSettings
notificationSettings: notificationSettings,
volumeEnforced: volumeEnforced
)
}

Expand All @@ -63,10 +66,11 @@ struct AlarmSettings: Codable {
"loopAudio": alarmSettings.loopAudio,
"vibrate": alarmSettings.vibrate,
"volume": alarmSettings.volume,
"volumeEnforced": alarmSettings.volumeEnforced,
"fadeDuration": alarmSettings.fadeDuration,
"warningNotificationOnKill": alarmSettings.warningNotificationOnKill,
"androidFullScreenIntent": alarmSettings.androidFullScreenIntent,
"notificationSettings": NotificationSettings.toJson(notificationSettings: alarmSettings.notificationSettings)
]
}
}
}

0 comments on commit 4f781cd

Please sign in to comment.