From 0ad2cb124bf20620302f7b6f6b3139e6826ada39 Mon Sep 17 00:00:00 2001 From: rene Date: Sat, 30 May 2026 19:09:35 +0200 Subject: [PATCH] =?UTF-8?q?HeimView:=20Backend=20ist=20Source-of-Truth=20f?= =?UTF-8?q?=C3=BCrs=20Tagesfoto?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- BanYaroGo/Views/HeimView.swift | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/BanYaroGo/Views/HeimView.swift b/BanYaroGo/Views/HeimView.swift index db2de2f..b1f553c 100644 --- a/BanYaroGo/Views/HeimView.swift +++ b/BanYaroGo/Views/HeimView.swift @@ -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 { - UserDefaults.standard.set(fresh, forKey: key) + // 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 } }