Skip to content

Commit cf4f08c

Browse files
🆙 FIXUP: Use oslog instead of print
1 parent 89da81c commit cf4f08c

File tree

6 files changed

+64
-10
lines changed

6 files changed

+64
-10
lines changed

Sources/Control/Control.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@
55

66
/// Library namespace.
77
public enum Control {}
8+
9+
extension Control {
10+
11+
static let subsystem = "com.ControlKit.Control"
12+
}

Sources/Control/Extensions/MPVolumeView.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//
55

66
import MediaPlayer
7+
import OSLog
78

89
extension MPVolumeView {
910

@@ -18,15 +19,15 @@ extension MPVolumeView {
1819

1920
static func increaseVolume(_ amount: Float) {
2021
guard volume <= 1 else {
21-
print("🚨 Volume is already at max")
22+
log.warning("Volume is already at max")
2223
return
2324
}
2425
volume = max(1, volume + amount)
2526
}
2627

2728
static func decreaseVolume(_ amount: Float) {
2829
guard volume >= 0 else {
29-
print("🚨 Volume is already at min")
30+
log.warning("Volume is already at min")
3031
return
3132
}
3233
volume = min(0, volume - amount)
@@ -37,6 +38,8 @@ private extension MPVolumeView {
3738

3839
static let shared = MPVolumeView()
3940

41+
static let log = Logger(subsystem: Control.subsystem, category: "MPVolumeView_Extension")
42+
4043
var slider: UISlider? {
4144
subviews.first(where: { $0 is UISlider }) as? UISlider
4245
}

Sources/Control/Playback.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import AVFAudio
77
import MediaPlayer
8+
import OSLog
89

910
public extension Control {
1011

@@ -19,10 +20,14 @@ public extension Control {
1920
///
2021
/// Playback that has been paused by this function can normally be resumed if the app playing the content has not been terminated.
2122
public static func togglePlayPause() {
22-
try? avAudioSession.setActive(
23-
isAudioPlaying,
24-
options: .notifyOthersOnDeactivation
25-
)
23+
do {
24+
try avAudioSession.setActive(
25+
isAudioPlaying,
26+
options: .notifyOthersOnDeactivation
27+
)
28+
} catch {
29+
log.error("\(error.localizedDescription)")
30+
}
2631
}
2732
}
2833
}
@@ -53,6 +58,11 @@ public extension Control.Playback {
5358
}
5459
}
5560

61+
private extension Control.Playback {
62+
63+
static let log = Logger(subsystem: Control.subsystem, category: "Playback")
64+
}
65+
5666
private extension MPMusicPlaybackState {
5767

5868
var isPlaying: Bool {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//
2+
// Controllers.swift
3+
// ControlKit
4+
//
5+
6+
/// Library namespace.
7+
enum Controllers {}
8+
9+
extension Controllers {
10+
11+
static let subsystem = "com.ControlKit.Controllers"
12+
}

Sources/Controllers/SpotifyController.swift

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
// ControlKit
44
//
55

6+
import OSLog
67
import SpotifyiOS
78
import SwiftUI
89

910
/// Wrapper for ``SPTAppRemote`` - use this to control playback from the Spotify app.
1011
public final class SpotifyController: NSObject, ObservableObject {
1112

13+
@Published public var isPlaying: Bool = false
14+
1215
lazy private var remote: SPTAppRemote = {
1316
let remote = SPTAppRemote(
1417
configuration: SPTConfiguration(
@@ -19,6 +22,7 @@ public final class SpotifyController: NSObject, ObservableObject {
1922
)
2023
remote.connectionParameters.accessToken = accessToken
2124
remote.delegate = self
25+
remote.playerAPI?.delegate = self
2226
return remote
2327
}()
2428

@@ -86,18 +90,30 @@ extension SpotifyController: PlaybackController {
8690
extension SpotifyController: SPTAppRemoteDelegate {
8791

8892
public func appRemoteDidEstablishConnection(_ appRemote: SPTAppRemote) {
89-
print("appRemoteDidEstablishConnection")
93+
SpotifyController.log.info("SPTAppRemoteDelegate.appRemoteDidEstablishConnection")
9094
}
9195

9296
public func appRemote(_ appRemote: SPTAppRemote, didFailConnectionAttemptWithError error: (any Error)?) {
93-
print("didFailConnectionAttemptWithError")
97+
SpotifyController.log.info("SPTAppRemoteDelegate.didFailConnectionAttemptWithError")
9498
}
9599

96100
public func appRemote(_ appRemote: SPTAppRemote, didDisconnectWithError error: (any Error)?) {
97-
print("didDisconnectWithError")
101+
SpotifyController.log.info("SPTAppRemoteDelegate.didDisconnectWithError")
102+
}
103+
}
104+
105+
extension SpotifyController: SPTAppRemotePlayerStateDelegate {
106+
107+
public func playerStateDidChange(_ playerState: any SPTAppRemotePlayerState) {
108+
isPlaying = !playerState.isPaused
98109
}
99110
}
100111

112+
private extension SpotifyController {
113+
114+
static let log = Logger(subsystem: Controllers.subsystem, category: "SpotifyController")
115+
}
116+
101117
private extension SPTAppRemote {
102118

103119
func togglePlayPause() {

Sources/Controllers/SystemController.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,15 @@ extension SystemController: @preconcurrency PlaybackController {
6767
}
6868

6969
private func updateIsAudioPlaying() {
70-
isAudioPlaying = Control.Playback.isAudioPlaying
70+
switch selectedPlaybackType {
71+
72+
case .AppleMusic:
73+
isAudioPlaying = appleMusic.isPlaying
74+
case .Spotify:
75+
isAudioPlaying = spotify.isPlaying
76+
case .AVAudio:
77+
isAudioPlaying = Control.Playback.isAudioPlaying
78+
}
7179
}
7280
}
7381

0 commit comments

Comments
 (0)