Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
255 lines
8.0 KiB
Swift
255 lines
8.0 KiB
Swift
// CrossSound for Apple Watch — a remote for every CrossSound device on your
|
|
// account, and a player of its own (through Bluetooth headphones).
|
|
//
|
|
// The watch can't sign in to SoundCloud (there is no web view on watchOS), so
|
|
// it uses the iPhone's login, handed over by WatchLink. Everything below the
|
|
// screens is CrossSoundKit, the code the phone runs: the API, the queue, the
|
|
// FairPlay player and the Firebase remote. See WatchModel.
|
|
|
|
import CrossSoundKit
|
|
import RedWhiteUI
|
|
import SwiftUI
|
|
|
|
@main
|
|
struct CrossSoundWatchApp: App {
|
|
@State private var model = WatchModel()
|
|
@Environment(\.scenePhase) private var scenePhase
|
|
|
|
var body: some Scene {
|
|
WindowGroup {
|
|
WatchRoot()
|
|
.crossSoundWatchLook()
|
|
// Above the look: the look reads the palette .rwTheme plants.
|
|
.rwTheme(model.appearance)
|
|
.environment(model)
|
|
.task { await model.boot() }
|
|
// The phone's theme, whenever it sends one.
|
|
.onChange(of: WatchLink.shared.themeJSON, initial: true) { _, json in
|
|
model.applyTheme(json)
|
|
}
|
|
}
|
|
.onChange(of: scenePhase) { _, phase in model.scenePhaseChanged(phase) }
|
|
}
|
|
}
|
|
|
|
/// Every push, as one value: the stack registers one destination switch.
|
|
enum WatchRoute: Hashable {
|
|
case player
|
|
case queue
|
|
case device(String)
|
|
case likes
|
|
case playlists
|
|
case playlist(SCPlaylist)
|
|
case stations
|
|
case history
|
|
case feed
|
|
case search
|
|
case log
|
|
}
|
|
|
|
struct WatchRoot: View {
|
|
@Environment(WatchModel.self) private var model
|
|
@Environment(\.rw) private var p
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
Group {
|
|
switch model.phase {
|
|
case .loading:
|
|
ProgressView()
|
|
case .signedOut:
|
|
SignedOutScreen()
|
|
case .ready:
|
|
HomeScreen()
|
|
}
|
|
}
|
|
.navigationDestination(for: WatchRoute.self) { route in
|
|
destination(route)
|
|
.containerBackground(p.bg.color, for: .navigation)
|
|
.toolbarForegroundStyle(p.red.color, for: .navigationBar)
|
|
}
|
|
.containerBackground(p.bg.color, for: .navigation)
|
|
// Titles in the theme's red; watchOS draws them gray otherwise (.tint
|
|
// doesn't reach them). Per page: each one styles its own bar.
|
|
.toolbarForegroundStyle(p.red.color, for: .navigationBar)
|
|
}
|
|
.overlay(alignment: .bottom) {
|
|
if let notice = model.notice {
|
|
Text(notice)
|
|
.rw(.labelMedium)
|
|
.multilineTextAlignment(.center)
|
|
.padding(8)
|
|
.frame(maxWidth: .infinity)
|
|
.background(p.bg.color)
|
|
.rwBorder(.accent, width: 2)
|
|
.padding(.horizontal, 4)
|
|
.transition(.move(edge: .bottom).combined(with: .opacity))
|
|
}
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
private func destination(_ route: WatchRoute) -> some View {
|
|
switch route {
|
|
case .player: PlayerScreen()
|
|
case .queue: QueueScreen()
|
|
case .device(let id): DeviceScreen(deviceId: id)
|
|
case .likes:
|
|
TrackListScreen(title: "Likes") { api in try await api.getLikedTracks(limit: 200) }
|
|
case .history:
|
|
TrackListScreen(title: "History") { api in try await api.getHistory(limit: 100) }
|
|
case .feed:
|
|
TrackListScreen(title: "Feed") { api in try await api.getStream(limit: 50) }
|
|
case .playlists:
|
|
PlaylistListScreen(title: "Playlists") { api in
|
|
// Yours first, then the ones you liked (minus any you own).
|
|
let mine = try await api.getMyPlaylists(limit: 100)
|
|
let liked = (try? await api.getLikedPlaylists(limit: 100)) ?? []
|
|
let ids = Set(mine.map(\.id))
|
|
return mine + liked.filter { !ids.contains($0.id) }
|
|
}
|
|
case .stations:
|
|
PlaylistListScreen(title: "Stations") { api in
|
|
try await api.getLikedStationsPage(limit: 100).playlists
|
|
}
|
|
case .playlist(let playlist):
|
|
PlaylistScreen(playlist: playlist)
|
|
case .search:
|
|
SearchScreen()
|
|
case .log:
|
|
LogScreen()
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Home
|
|
|
|
struct HomeScreen: View {
|
|
@Environment(WatchModel.self) private var model
|
|
@State private var confirmingSignOut = false
|
|
|
|
var body: some View {
|
|
ScrollView {
|
|
LazyVStack(alignment: .leading, spacing: 2) {
|
|
if let track = model.queue.currentTrack {
|
|
NavigationLink(value: WatchRoute.player) {
|
|
TrackRow(
|
|
title: track.title, artist: track.user.username,
|
|
artworkUrl: track.artworkUrlSmall ?? track.artworkUrl,
|
|
isCurrent: model.queue.isPlaying)
|
|
}
|
|
}
|
|
|
|
devices
|
|
|
|
SectionLabel("Library")
|
|
NavigationLink(value: WatchRoute.search) { LinkRow(icon: "magnifyingglass", title: "Search") }
|
|
NavigationLink(value: WatchRoute.likes) { LinkRow(icon: "heart", title: "Likes") }
|
|
NavigationLink(value: WatchRoute.playlists) {
|
|
LinkRow(icon: "music.note.list", title: "Playlists")
|
|
}
|
|
NavigationLink(value: WatchRoute.stations) {
|
|
LinkRow(icon: "dot.radiowaves.left.and.right", title: "Stations")
|
|
}
|
|
NavigationLink(value: WatchRoute.feed) { LinkRow(icon: "rectangle.stack", title: "Feed") }
|
|
NavigationLink(value: WatchRoute.history) {
|
|
LinkRow(icon: "clock.arrow.circlepath", title: "History")
|
|
}
|
|
|
|
SectionLabel("Account")
|
|
if let me = model.me {
|
|
Text("Signed in as \(me.username)")
|
|
.rw(.labelMedium)
|
|
.padding(.vertical, 4)
|
|
}
|
|
Button {
|
|
confirmingSignOut = true
|
|
} label: {
|
|
LinkRow(icon: "rectangle.portrait.and.arrow.right", title: "Sign out")
|
|
}
|
|
|
|
SectionLabel("Help")
|
|
NavigationLink(value: WatchRoute.log) { LinkRow(icon: "text.alignleft", title: "Log") }
|
|
}
|
|
.padding(.horizontal, 4)
|
|
}
|
|
.navigationTitle("CrossSound")
|
|
.confirmationDialog(
|
|
"Sign out on this watch?", isPresented: $confirmingSignOut, titleVisibility: .visible
|
|
) {
|
|
Button("Sign out", role: .destructive) {
|
|
Task { await model.signOut() }
|
|
}
|
|
Button("Cancel", role: .cancel) {}
|
|
} message: {
|
|
Text("Your iPhone stays signed in.")
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var devices: some View {
|
|
let devices = model.remote.devices.filter(\.online)
|
|
SectionLabel("Devices")
|
|
if devices.isEmpty {
|
|
Text(model.remote.started ? "No other devices online" : "Connecting…")
|
|
.rw(.labelMedium)
|
|
.padding(.vertical, 6)
|
|
}
|
|
ForEach(devices) { device in
|
|
NavigationLink(value: WatchRoute.device(device.id)) {
|
|
DeviceRow(device: device)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Signed out
|
|
|
|
/// Signing in happens on the iPhone (there's no web view on a watch): its
|
|
/// Devices screen sends the login over, and after that the watch runs on its
|
|
/// own — no phone needed nearby.
|
|
struct SignedOutScreen: View {
|
|
@Environment(WatchModel.self) private var model
|
|
@State private var asking = false
|
|
|
|
var body: some View {
|
|
let hasPhoneApp = WatchLink.shared.companionInstalled
|
|
ScrollView {
|
|
VStack(spacing: 10) {
|
|
Image(systemName: "iphone")
|
|
.font(.system(size: 30, weight: .semibold))
|
|
Text(hasPhoneApp ? "Sign in on your iPhone" : "Get CrossSound on your iPhone")
|
|
.rw(.titleMedium)
|
|
.multilineTextAlignment(.center)
|
|
Text(
|
|
hasPhoneApp
|
|
? "Tap Sign in with your iPhone nearby, or in CrossSound on your iPhone open "
|
|
+ "Devices and tap Sign in on Apple Watch. After that the watch works on its own."
|
|
: "The watch signs in once with your iPhone's SoundCloud login, then works on its own."
|
|
)
|
|
.rw(.labelMedium)
|
|
.multilineTextAlignment(.center)
|
|
Button {
|
|
asking = true
|
|
Task {
|
|
await model.signIn(userInitiated: true)
|
|
asking = false
|
|
}
|
|
} label: {
|
|
if asking {
|
|
ProgressView()
|
|
} else {
|
|
Text("Sign in").rw(.titleSmall)
|
|
}
|
|
}
|
|
.padding(.top, 4)
|
|
NavigationLink(value: WatchRoute.log) {
|
|
Text("Log").rw(.labelMedium)
|
|
}
|
|
}
|
|
.padding(.horizontal, 6)
|
|
}
|
|
.navigationTitle("CrossSound")
|
|
}
|
|
}
|