An API client for ipx, and its event stream (#1)
Everything native needs this first, and it is worth having whatever happens to the rest: a CarPlay browse tree is built from exactly these calls. API covers what a client reads and writes -- feeds, entries with the server's own paging, filtering, sorting and search, read and pinned, position, read-all, download, delete, fetch -- and APIModels is what ipx sends, with the server's field names kept so a type here can be checked against web.rs without translating first. It sends nothing of its own to authenticate: ipx decides who is asking by cookie and CookieBridge keeps HTTPCookieStorage in step, which is the same property that lets a plain <audio src> work. Events reads /api/events with URLSession's byte stream, so there is no new dependency. An event this build has never heard of decodes as .other rather than throwing, because a new one on the server must not stop the stream. It reconnects with a backoff: a deploy closes the stream cleanly and should be picked straight back up, a daemon that is down should not be hammered. Library is gone, folded into API. Two clients writing positions was one too many, and the page's forwarded beacon no longer carries a path: it asks the host to save, and the host uses its own clock, which is the only one not stale when the web view has been frozen in the background. The tests run against the browser suite's fixture daemon rather than canned JSON, because the shapes being decoded are the server's and a fixture would only prove it matches itself. Thirteen pass, including a live scan reported over SSE and the playback round trip, which still works after the move. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -28,7 +28,7 @@ final class Playback: NSObject {
|
||||
var onError: ((String) -> Void)?
|
||||
|
||||
private let cookies: CookieBridge
|
||||
private let library: Library
|
||||
private let api: API
|
||||
private var player: AVPlayer?
|
||||
private var item: Item?
|
||||
private var timeObserver: Any?
|
||||
@@ -43,9 +43,9 @@ final class Playback: NSObject {
|
||||
return d
|
||||
}
|
||||
|
||||
init(cookies: CookieBridge, library: Library) {
|
||||
init(cookies: CookieBridge, api: API) {
|
||||
self.cookies = cookies
|
||||
self.library = library
|
||||
self.api = api
|
||||
super.init()
|
||||
configureSession()
|
||||
wireRemoteCommands()
|
||||
@@ -90,8 +90,11 @@ final class Playback: NSObject {
|
||||
if let d = self.duration { self.onMeta?(d) }
|
||||
case .failed:
|
||||
let msg = pi.error?.localizedDescription ?? "the file could not be played"
|
||||
self.library.check { good in
|
||||
DispatchQueue.main.async { self.onError?(good ? msg : "sign in again") }
|
||||
Task {
|
||||
// Tell one from the other: a file that will not play, and a session that
|
||||
// went while it was playing. Only the second has anything to do about it.
|
||||
let signedIn = (try? await self.api.me()) != nil
|
||||
await MainActor.run { self.onError?(signedIn ? msg : "Sign in again") }
|
||||
}
|
||||
default: break
|
||||
}
|
||||
@@ -154,19 +157,17 @@ final class Playback: NSObject {
|
||||
if notify { onState?(false) }
|
||||
}
|
||||
|
||||
/// The page asking us to save, because its own beacon is ours now. It sends the path rather
|
||||
/// than a time: the time it has may be minutes old if it has been frozen in the background.
|
||||
func savePosition(path: String? = nil) {
|
||||
/// Where we are, saved. The page asks for this too, because its own beacon is ours now: it
|
||||
/// sends no time with the request, since the time a backgrounded web view holds may be
|
||||
/// minutes old. This clock is the only one worth writing.
|
||||
func savePosition() {
|
||||
guard let item, let player, player.currentItem?.status == .readyToPlay else { return }
|
||||
let secs = Int(player.currentTime().seconds.rounded())
|
||||
// A file under a second rounds to nought, and "nought seconds in" is not worth writing.
|
||||
guard secs > 0 else { return }
|
||||
lastSaved = Double(secs)
|
||||
let dur = duration.map { Int($0.rounded()) }
|
||||
if let path {
|
||||
library.savePosition(path: path, secs: secs, duration: dur)
|
||||
} else {
|
||||
library.savePosition(feedId: item.feedId, guid: item.guid, secs: secs, duration: dur)
|
||||
}
|
||||
Task { try? await api.setPosition(secs: secs, duration: dur, feed: item.feedId, guid: item.guid) }
|
||||
}
|
||||
|
||||
// MARK: - as it plays
|
||||
@@ -192,7 +193,7 @@ final class Playback: NSObject {
|
||||
private func markRead() {
|
||||
guard !markedRead, let item else { return }
|
||||
markedRead = true
|
||||
library.markRead(feedId: item.feedId, guid: item.guid)
|
||||
Task { try? await api.setRead(true, feed: item.feedId, guid: item.guid) }
|
||||
}
|
||||
|
||||
// MARK: - the car and the lock screen
|
||||
@@ -240,9 +241,9 @@ final class Playback: NSObject {
|
||||
centre.nowPlayingInfo = info
|
||||
|
||||
if let art = item.artwork {
|
||||
library.artwork(art) { image in
|
||||
guard let image else { return }
|
||||
DispatchQueue.main.async {
|
||||
Task {
|
||||
guard let image = await api.image(art) else { return }
|
||||
await MainActor.run {
|
||||
// Read back rather than reusing `info`: by the time the picture arrives the
|
||||
// elapsed time in it is stale, and writing it would jerk the car's progress bar.
|
||||
var latest = centre.nowPlayingInfo ?? [:]
|
||||
|
||||
Reference in New Issue
Block a user