Make entitlement reads observable !16

merged merged by cmc on 2026-10-24 06:13 UTC · krz/domain-dig:observable-tier into main

Discussion

cmc

Closes A6, which was a defect rather than a tidy-up.

FeatureAccessService.currentTier read PurchaseService.cachedTier — a static over UserDefaults — so a view body calling hasAccess touched no observable property, SwiftUI recorded no dependency, and the view would not re-render when a purchase landed. The nine let _ = purchaseService.currentTier discards were compensating for that.

Reading PurchaseService.shared.currentTier instead fixes it at the source: observation records any observable property read while a body evaluates, however deep in the call tree, so every existing hasAccess call site updates on its own. The instance property is kept in step with the static everywhere it changes — init, refreshEntitlements, transaction updates, the debug override and the owner grant — so the value is the same, only now it is observed.

Three findings fell out of doing it:

  • Four of the nine discards could never have worked. They were in DomainDigApp's .task, and observation tracks body evaluation, not .task closures.
  • Three views held a @State purchaseService solely to perform the fake read and no longer need the property at all.
  • The duplicated launch block was hiding a staleness bug. .task called refreshEntitlements() and the scenePhase == .active path did not, so a purchase, cancellation or restore made outside the app — App Store, another device, Settings → Subscriptions — was not picked up until the next cold launch. Both paths now call one refreshAfterBecomingActive().

Verified, and what is not

156 tests, 0 failures on iOS 18.6 and iOS 27.0 — which proves nothing broke, not that the views now update on a tier change. That is SwiftUI runtime behaviour the unit suite cannot reach, and exercising it needs a real purchase or the owner grant rather than a launch argument. The mechanism follows from Swift's observation semantics and the read path is now correct by construction, but a live tier change is unverified.