Files
ipx/ios/Shared/Snapshot.swift
Ray Slakinski f844cf349a iOS: Siri and Shortcuts, and a widget (#427, #428)
Siri: App Intents with phrases that need no setup: Resume, Pause,
Skip forward, Skip back, Set a sleep timer, How many unread. The
playback ones are AudioPlaybackIntents, run in the app and allowed to
start audio from the background. Playback is now one shared instance
(Playback.shared) so Siri and the widget reach it with no page loaded.
It remembers the last episode and its place (LastPlayed), so Resume
works with the app closed; the page, opened afterwards, does not know
about an episode started that way.

Widget: small and medium on the home screen, circular and rectangular
on the lock screen. It shows the unread count and the last episode,
with a Resume or Pause button (PlayPauseIntent, shared with Siri). It
draws what the app writes to the App Group group.net.sdf1.ipx. The app
asks for a redraw on play, pause, a new episode or a new count, not on
every position, since iOS rations redraws. Shared/ builds into both
targets, with WIDGET set in the widget's, where the player does not
exist.

ipx's push now carries content-available, which wakes the closed app
for a moment to give the widget the count the icon just got (#433).

Tested: unit tests for the snapshot and the remembered place, 19 in
all; in the simulator, playing an episode wrote the snapshot to the
App Group. Not testable there: Siri, and the widget drawn.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 19:33:04 -05:00

31 lines
1.1 KiB
Swift

import Foundation
/// What the widget shows (#428): the unread count and the episode you were last on. Written by
/// the app into the App Group the two share, since the widget has no sign-in and no player of its
/// own; it shows what the app last knew.
struct Snapshot: Codable, Equatable {
var unread: Int?
var title: String?
var feedTitle: String?
var position: Double?
var duration: Double?
var playing = false
static let group = "group.net.sdf1.ipx"
private static let key = "ipx.snapshot"
static func load(from defaults: UserDefaults? = UserDefaults(suiteName: group)) -> Snapshot {
defaults?.data(forKey: key).flatMap { try? JSONDecoder().decode(Snapshot.self, from: $0) } ?? Snapshot()
}
func save(to defaults: UserDefaults? = UserDefaults(suiteName: group)) {
if let data = try? JSONEncoder().encode(self) { defaults?.set(data, forKey: Self.key) }
}
/// How far through, 0 to 1, when both are known.
var progress: Double? {
guard let position, let duration, duration > 0 else { return nil }
return min(1, max(0, position / duration))
}
}