HeimView: Backend ist Source-of-Truth fürs Tagesfoto

Bisher: cachedPhotoUrl (UserDefaults) wurde dem Backend-Wert
bevorzugt. Wenn der Cache eine tote URL hielt (gelöschtes Bild),
sah man bis Mitternacht kein Tagesbild — selbst nachdem der
Backend-Cache selbstheilend invalidiert war.

Jetzt: dashboard.randomPhoto.url gewinnt im Background-View, Cache
ist nur Offline-Fallback. In load() wird der Cache bei jeder neuen
Backend-URL aktualisiert.
This commit is contained in:
rene 2026-05-30 19:09:35 +02:00
parent 55b65195af
commit 0ad2cb124b

View file

@ -68,7 +68,7 @@ struct HeimView: View {
private var background: some View {
ZStack {
Color.accentColor.opacity(0.08).ignoresSafeArea()
if let path = cachedPhotoUrl ?? dashboard?.randomPhoto?.url,
if let path = dashboard?.randomPhoto?.url ?? cachedPhotoUrl,
let url = URL(string: "https://banyaro.app\(path)") {
AsyncImage(url: url) { phase in
switch phase {
@ -244,19 +244,20 @@ struct HeimView: View {
private func load() async {
guard let dog = activeDog.activeDog else { return }
// 1. Cached photo URL bevorzugen bleibt stabil über den Tag, identisch
// zur PWA (gleiches Cache-Schema: pro user+dog+datum).
// Auch nil setzen, falls der Cache-Key zum anderen Hund gehört.
// Cache als Fallback, falls Backend offline. Nicht als Vorzug
// sonst klebt eine tote URL (gelöschtes Bild) über den ganzen Tag.
cachedPhotoUrl = photoCacheKey.flatMap { UserDefaults.standard.string(forKey: $0) }
isLoading = true
defer { isLoading = false }
dashboard = try? await APIClient.shared.get("/api/dogs/\(dog.id)/welcome-dashboard")
// 2. Wenn noch nichts gecacht ist und das Backend ein Foto liefert,
// festhalten bis Mitternacht zeigen wir dasselbe Bild.
if cachedPhotoUrl == nil, let fresh = dashboard?.randomPhoto?.url, let key = photoCacheKey {
// Backend ist Source-of-Truth: wenn es ein Foto liefert, ist das
// aktuell und im Server-Cache stabil. Lokal denselben Wert ablegen.
if let fresh = dashboard?.randomPhoto?.url, let key = photoCacheKey {
if fresh != cachedPhotoUrl {
UserDefaults.standard.set(fresh, forKey: key)
}
cachedPhotoUrl = fresh
}
}