tvOS 17+ SwiftUI client for the unofficial Yandex Music API: rotor stations with feedback, library, search, artist and album pages, synced lyrics, and system Now Playing integration.
113 lines
3.8 KiB
Swift
113 lines
3.8 KiB
Swift
import Foundation
|
|
|
|
/// Rotor stations are an endless chain: the server hands out a batch, the
|
|
/// client reports what happened to each track, and asks for more by naming the
|
|
/// last one it played.
|
|
extension PlayerController {
|
|
private static let clientName = "appletv-yamusek"
|
|
|
|
private static func resumeKey(_ station: String) -> String {
|
|
"rotorResume.\(station)"
|
|
}
|
|
|
|
func startRotor(station: String, name: String) {
|
|
isLoading = true
|
|
Task {
|
|
// Announce the start before asking for tracks, and continue from
|
|
// where this station left off; otherwise it replays its opening.
|
|
await YMClient.shared.rotorFeedback(
|
|
station: station,
|
|
type: "radioStarted",
|
|
from: Self.clientName
|
|
)
|
|
|
|
let resume = UserDefaults.standard.string(forKey: Self.resumeKey(station))
|
|
guard let result = try? await YMClient.shared.rotorTracks(station: station, queue: resume) else {
|
|
stopForEmptyStation()
|
|
return
|
|
}
|
|
|
|
var tracks = result.sequence.compactMap(\.track)
|
|
if let resume, tracks.count > 1, tracks.first?.trackID == resume {
|
|
tracks.removeFirst()
|
|
}
|
|
guard !tracks.isEmpty else {
|
|
stopForEmptyStation()
|
|
return
|
|
}
|
|
|
|
rotorBatchID = result.batchId
|
|
setQueue(tracks, source: .rotor(station: station, name: name))
|
|
}
|
|
}
|
|
|
|
/// Saved on start rather than on finish: a few seconds of listening is
|
|
/// enough for the station to move on next time.
|
|
func reportRotorTrackStarted() {
|
|
guard case let .rotor(station, _) = source, let track = current else { return }
|
|
let id = track.trackID
|
|
let batch = rotorBatchID
|
|
UserDefaults.standard.set(id, forKey: Self.resumeKey(station))
|
|
|
|
Task {
|
|
await YMClient.shared.rotorFeedback(
|
|
station: station,
|
|
type: "trackStarted",
|
|
trackID: id,
|
|
batchID: batch
|
|
)
|
|
}
|
|
}
|
|
|
|
func reportTrackEnded(skipped: Bool) {
|
|
guard case let .rotor(station, _) = source, let track = current else { return }
|
|
let id = track.trackID
|
|
let batch = rotorBatchID
|
|
let played = clock.currentTime
|
|
|
|
Task {
|
|
await YMClient.shared.rotorFeedback(
|
|
station: station,
|
|
type: skipped ? "skip" : "trackFinished",
|
|
trackID: id,
|
|
batchID: batch,
|
|
totalPlayedSeconds: played
|
|
)
|
|
}
|
|
}
|
|
|
|
/// Fetches the next batch while a couple of tracks are still queued.
|
|
func topUpRotorIfNeeded() {
|
|
guard case let .rotor(station, _) = source, queue.count - index <= 2 else { return }
|
|
let last = queue.last?.trackID
|
|
|
|
Task {
|
|
guard let result = try? await YMClient.shared.rotorTracks(station: station, queue: last) else { return }
|
|
rotorBatchID = result.batchId ?? rotorBatchID
|
|
appendToQueue(result.sequence.compactMap(\.track))
|
|
}
|
|
}
|
|
|
|
/// The queue ran dry; pull more and keep going.
|
|
func extendRotor(station: String, name: String) {
|
|
isLoading = true
|
|
let last = queue.last?.trackID
|
|
|
|
Task {
|
|
guard let result = try? await YMClient.shared.rotorTracks(station: station, queue: last) else {
|
|
stopForEmptyStation()
|
|
return
|
|
}
|
|
rotorBatchID = result.batchId ?? rotorBatchID
|
|
|
|
let before = queue.count
|
|
appendToQueue(result.sequence.compactMap(\.track))
|
|
guard queue.count > before else {
|
|
stopForEmptyStation()
|
|
return
|
|
}
|
|
startPlaying(at: before)
|
|
}
|
|
}
|
|
}
|