Yandex Music client for Apple TV

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.
This commit is contained in:
2026-09-17 20:38:19 +03:00
commit acd50280bc
70 changed files with 4588 additions and 0 deletions
+104
View File
@@ -0,0 +1,104 @@
import SwiftUI
struct LikesView: View {
@EnvironmentObject private var player: PlayerController
@State private var tracks: [Track] = []
@State private var isLoading = true
var body: some View {
NavigationStack {
content
.musicDestinations()
}
.task { await load() }
}
/// A real `List` rather than a `ScrollView` + `LazyVStack`: on tvOS the list
/// recycles its rows and owns the focus animation, where a lazy stack of a
/// hundred and fifty custom buttons does neither.
private var content: some View {
VStack(alignment: .leading, spacing: 0) {
// Everything on this screen shares the tighter margin.
// Outside the List on purpose: inside a section header SwiftUI
// paints everything as secondary, which greys out the buttons.
header
.padding(.top, 30)
.padding(.bottom, 24)
List {
ForEach(Array(tracks.enumerated()), id: \.offset) { pair in
TrackRow(
track: pair.element,
isCurrent: player.current?.trackID == pair.element.trackID
) {
player.play(
tracks: tracks,
startAt: pair.offset,
source: .collection("Мне нравится")
)
}
}
}
.listStyle(.plain)
.overlay {
if isLoading {
LoadingState(text: "Загружаем вашу коллекцию…")
}
}
}
}
private var header: some View {
VStack(alignment: .leading, spacing: 20) {
HStack(alignment: .firstTextBaseline) {
Text("Мне нравится")
.font(.system(size: 56, weight: .bold))
Spacer()
if !tracks.isEmpty {
Text("\(tracks.count) треков").foregroundStyle(.secondary)
}
}
if !tracks.isEmpty {
HStack(spacing: 20) {
Button {
player.play(tracks: tracks, startAt: 0, source: .collection("Мне нравится"))
} label: {
Label("Слушать", systemImage: "play.fill")
}
Button {
player.play(
tracks: tracks.shuffled(),
startAt: 0,
source: .collection("Мне нравится")
)
} label: {
Label("Вперемешку", systemImage: "shuffle")
}
}
}
}
}
/// The whole library, a hundred at a time, so a big collection starts
/// showing immediately instead of after every batch has arrived.
private func load() async {
guard tracks.isEmpty else { return }
isLoading = true
defer { isLoading = false }
do {
let keys = try await YMClient.shared.likedTrackKeys()
for chunk in stride(from: 0, to: keys.count, by: 100).map({
Array(keys[$0 ..< min($0 + 100, keys.count)])
}) {
let batch = try await YMClient.shared.tracks(keys: chunk)
guard !Task.isCancelled else { return }
tracks.append(contentsOf: batch)
isLoading = false
}
} catch {
log.error("\(error.localizedDescription)")
}
}
}