A native player bar, and the Glass look it sits in (#2)
The first native surface, and the one that settles how the rest will look. Glass.swift is not a port of the stylesheet. ipx's Glass is already an approximation of what UIKit gives away -- panels are a 70% colour under blur(24px) saturate(180%) with an inset top highlight, which is a material, and the palette names Apple's own colours in its own comments. So where the CSS had to write #409cff in dark and #0055aa in light, because no single blue clears AA over the wash, this asks for the system blue and gets both. Only the wash keeps its hex values: there the colour is the design and there is no system equivalent to ask for. PlayerBarView draws what Playback knows, and Playback is the same object the lock screen and the remote commands drive, so all of them agree without anything being kept in step by hand. Back 15 and forward 30 match the page and the lock screen, so every way of skipping moves by the same amount. Seeking waits for the finger to lift rather than sending a stream of seeks at a player still answering the last one. The page's own bar is hidden rather than left to sit under ours. Its play buttons still work -- they post to the host either way -- but two bars for one player would be two sets of buttons disagreeing about what is playing, and only one of them is what CarPlay will be driving. One test had to be rewritten rather than the code: it asserted that one of the fixture's two episodes had a file, which stops being true the moment the event test asks the daemon to scan. The daemon is shared and keeps what earlier tests did to it, so the assertion now asks what the downloaded filter means instead of how many things match it today. Thirteen pass, twice over the same accumulated state. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,11 +1,22 @@
|
||||
import AVFoundation
|
||||
import Foundation
|
||||
import MediaPlayer
|
||||
import UIKit
|
||||
|
||||
/// The host's player: the one CarPlay and a car's own buttons can reach, and the one that keeps
|
||||
/// going when the screen locks. Knows nothing about the web view -- the bridge drives it, and so
|
||||
/// will a CarPlay scene.
|
||||
final class Playback: NSObject {
|
||||
@MainActor
|
||||
final class Playback: NSObject, ObservableObject {
|
||||
/// What a view needs to draw the bar. The closures below stay as they are: the page is not
|
||||
/// a SwiftUI view and the bridge still speaks to it by callback.
|
||||
@Published private(set) var nowPlaying: Item?
|
||||
@Published private(set) var elapsed: Double = 0
|
||||
@Published private(set) var playing = false
|
||||
@Published private(set) var artwork: UIImage?
|
||||
/// The length once it is known, falling back to what the feed claimed.
|
||||
@Published private(set) var length: Double?
|
||||
|
||||
/// What is loaded, enough to keep the car's screen right and to save a position without
|
||||
/// asking anyone.
|
||||
struct Item {
|
||||
@@ -68,6 +79,16 @@ final class Playback: NSObject {
|
||||
self.item = item
|
||||
self.markedRead = false
|
||||
self.lastSaved = 0
|
||||
self.nowPlaying = item
|
||||
self.elapsed = 0
|
||||
self.length = item.duration.map(Double.init)
|
||||
self.artwork = nil
|
||||
if let art = item.artwork {
|
||||
Task { [weak self] in
|
||||
let image = await self?.api.image(art)
|
||||
await MainActor.run { self?.artwork = image }
|
||||
}
|
||||
}
|
||||
|
||||
// The session cookie is how ipx knows who is asking, and an AVURLAsset does not read the
|
||||
// shared store on its own.
|
||||
@@ -115,12 +136,14 @@ final class Playback: NSObject {
|
||||
guard let player else { return }
|
||||
try? AVAudioSession.sharedInstance().setActive(true)
|
||||
player.play()
|
||||
playing = true
|
||||
onState?(true)
|
||||
updateNowPlaying()
|
||||
}
|
||||
|
||||
func pause() {
|
||||
player?.pause()
|
||||
playing = false
|
||||
onState?(false)
|
||||
savePosition()
|
||||
updateNowPlaying()
|
||||
@@ -152,6 +175,11 @@ final class Playback: NSObject {
|
||||
player?.pause()
|
||||
player = nil
|
||||
item = nil
|
||||
nowPlaying = nil
|
||||
artwork = nil
|
||||
elapsed = 0
|
||||
length = nil
|
||||
playing = false
|
||||
MPNowPlayingInfoCenter.default().nowPlayingInfo = nil
|
||||
try? AVAudioSession.sharedInstance().setActive(false, options: .notifyOthersOnDeactivation)
|
||||
if notify { onState?(false) }
|
||||
@@ -174,6 +202,9 @@ final class Playback: NSObject {
|
||||
|
||||
private func tick(_ secs: Double) {
|
||||
guard secs.isFinite else { return }
|
||||
elapsed = secs
|
||||
if let d = duration { length = d }
|
||||
playing = isPlaying
|
||||
onTime?(secs, duration)
|
||||
updateElapsed(secs)
|
||||
if abs(secs - lastSaved) > 10 { savePosition() }
|
||||
@@ -185,6 +216,7 @@ final class Playback: NSObject {
|
||||
@objc private func didEnd() {
|
||||
savePosition()
|
||||
markRead()
|
||||
playing = false
|
||||
onState?(false)
|
||||
onEnded?()
|
||||
updateNowPlaying()
|
||||
|
||||
Reference in New Issue
Block a user