The badge's and CarPlay's reads (/api/feeds, the entry lists) and the player's writes (position, flags) now go as application/x-protobuf, the messages generated from crates/wire/proto by ios/scripts/proto.sh into Sources/Wire, committed so the build needs no protoc. SwiftProtobuf is pinned to the generator's version, 1.41.0, whose code refuses another. A read whose answer is not protobuf counts as no answer: a server from before #465 answers JSON whatever it is asked, and JSON can decode as a message of nothing, which would have set the badge to zero. The outbox keeps its stored key, `json`, for the body, and marks the new writes protobuf, so places saved offline by an older build are still sent, as JSON. Built for the simulator with Xcode 27.2; not run against a server. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
90 lines
3.9 KiB
Swift
90 lines
3.9 KiB
Swift
import BackgroundTasks
|
|
import UIKit
|
|
import UserNotifications
|
|
|
|
/// The number on the icon: All Subscriptions' unread count, as the page's badge shows it (#421).
|
|
///
|
|
/// Set when the app comes forward, after the player marks an item read, and by a background
|
|
/// refresh while it is closed. iOS decides when that runs (a few times a day for an app that is
|
|
/// opened often), so the number can be hours behind; push would make it exact and needs a sender
|
|
/// in ipx.
|
|
enum Badge {
|
|
static let taskId = "net.sdf1.ipx.refresh"
|
|
|
|
/// web/src/feeds.ts `unreadInAll`: a feed counts unless it, or the OPML it came from, is
|
|
/// hidden from All Subscriptions.
|
|
static func unread(inAll rows: [Ipx_FeedRow]) -> Int {
|
|
let hidden = Set(rows.filter(\.hideFromAll).map(\.id))
|
|
return rows.reduce(0) { n, f in
|
|
if f.hideFromAll || (f.hasGroup && hidden.contains(f.group)) { return n }
|
|
return n + max(0, Int(f.unread))
|
|
}
|
|
}
|
|
|
|
/// Asks once, for the badge alone: no alerts, no sounds.
|
|
static func requestPermission() {
|
|
// `-ipx.noBadgePrompt YES` on the launch arguments: a simulator run that drives the page
|
|
// has nobody to tap Allow, and the dialog covers it.
|
|
if UserDefaults.standard.bool(forKey: "ipx.noBadgePrompt") { return }
|
|
UNUserNotificationCenter.current().requestAuthorization(options: [.badge]) { granted, _ in
|
|
// Each launch, not once: Apple can change the token, and says to ask every time.
|
|
guard granted else { return }
|
|
Task { @MainActor in UIApplication.shared.registerForRemoteNotifications() }
|
|
}
|
|
}
|
|
|
|
/// The page's own count, sent as its list changes (#421).
|
|
@MainActor static func set(_ n: Int) {
|
|
lastSet = Date()
|
|
WidgetFeed.update { $0.unread = max(0, n) }
|
|
Task { try? await UNUserNotificationCenter.current().setBadgeCount(max(0, n)) }
|
|
}
|
|
|
|
/// When the icon was last set from a count, by either way.
|
|
@MainActor private static var lastSet = Date.distantPast
|
|
|
|
/// While an episode plays with the screen locked the app is awake, unlike the page: so new
|
|
/// items reach the icon every quarter of an hour, which background refresh does not promise.
|
|
@MainActor static func refreshIfStale() {
|
|
guard Date().timeIntervalSince(lastSet) > 15 * 60 else { return }
|
|
lastSet = Date()
|
|
Task { await refresh() }
|
|
}
|
|
|
|
/// Asks ipx and sets the icon. Left as it is when ipx cannot be reached or does not answer
|
|
/// with a feed list: an old number is better than a wrong one.
|
|
@discardableResult
|
|
static func refresh() async -> Bool {
|
|
guard let n = await fetch() else { return false }
|
|
await MainActor.run { set(n) }
|
|
return true
|
|
}
|
|
|
|
/// All Subscriptions' unread count from ipx, or nil when it cannot be had: an answer that is
|
|
/// not a feed list leaves the badge as it was rather than turning it to zero.
|
|
static func fetch() async -> Int? {
|
|
guard let feeds = await API.get(Ipx_FeedRows.self, "/api/feeds") else { return nil }
|
|
return unread(inAll: feeds.items)
|
|
}
|
|
|
|
// MARK: - while the app is closed
|
|
|
|
/// Before the app finishes launching, which is when iOS wants the handler registered.
|
|
static func registerBackgroundRefresh() {
|
|
BGTaskScheduler.shared.register(forTaskWithIdentifier: taskId, using: nil) { task in
|
|
// The next one first, so a run that fails or is cut short does not end the chain.
|
|
scheduleBackgroundRefresh()
|
|
let work = Task {
|
|
task.setTaskCompleted(success: await refresh())
|
|
}
|
|
task.expirationHandler = { work.cancel() }
|
|
}
|
|
}
|
|
|
|
static func scheduleBackgroundRefresh() {
|
|
let req = BGAppRefreshTaskRequest(identifier: taskId)
|
|
req.earliestBeginDate = Date(timeIntervalSinceNow: 30 * 60)
|
|
try? BGTaskScheduler.shared.submit(req)
|
|
}
|
|
}
|