Skip to content

Commit

Permalink
Replace commands based on diff to avoid iOS 15 issues
Browse files Browse the repository at this point in the history
  • Loading branch information
dcvz committed Sep 24, 2021
1 parent 4e79087 commit 9d2d259
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
19 changes: 18 additions & 1 deletion SwiftAudioEx/Classes/RemoteCommandController/RemoteCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public struct FeedbackCommand: RemoteCommandProtocol {
}
}

public enum RemoteCommand {
public enum RemoteCommand: CustomStringConvertible {

case play

Expand All @@ -128,6 +128,23 @@ public enum RemoteCommand {
case dislike(isActive: Bool, localizedTitle: String, localizedShortTitle: String)

case bookmark(isActive: Bool, localizedTitle: String, localizedShortTitle: String)

public var description: String {
switch self {
case .play: return "play"
case .pause: return "pause"
case .stop: return "stop"
case .togglePlayPause: return "togglePlayPause"
case .next: return "nextTrack"
case .previous: return "previousTrack"
case .changePlaybackPosition: return "changePlaybackPosition"
case .skipForward(_): return "skipForward"
case .skipBackward(_): return "skipBackward"
case .like(_, _, _): return "like"
case .dislike(_, _, _): return "dislike"
case .bookmark(_, _, _): return "bookmark"
}
}

/**
All values in an array for convenience.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public class RemoteCommandController {
weak var audioPlayer: AudioPlayer?

var commandTargetPointers: [String: Any] = [:]

private var enabledCommands: [RemoteCommand] = []

/**
Create a new RemoteCommandController.

Expand All @@ -30,10 +31,18 @@ public class RemoteCommandController {
}

internal func enable(commands: [RemoteCommand]) {
self.disable(commands: RemoteCommand.all())
let commandsToDisable = enabledCommands.filter { command in
!commands.contains(where: { $0.description == command.description })
}

self.enabledCommands = commands
commands.forEach { (command) in
self.enable(command: command)
}

commandsToDisable.forEach { (command) in
self.disable(command: command)
}
}

internal func disable(commands: [RemoteCommand]) {
Expand All @@ -44,6 +53,7 @@ public class RemoteCommandController {

private func enableCommand<Command: RemoteCommandProtocol>(_ command: Command) {
center[keyPath: command.commandKeyPath].isEnabled = true
center[keyPath: command.commandKeyPath].removeTarget(commandTargetPointers[command.id])
commandTargetPointers[command.id] = center[keyPath: command.commandKeyPath].addTarget(handler: self[keyPath: command.handlerKeyPath])
}

Expand Down

0 comments on commit 9d2d259

Please sign in to comment.