// FACES BROUGHT DOWN, AS A SEAM (Android 6d83189695; owner via the coordinator, 24 Nov 2026: people's display // pictures no longer showed in chats that came through WhatsApp). A face draws only what this phone holds // (`RecordPicture`, `read.mediaPath`) and redraws when bytes land (the media doorbell). Nothing brought a person's, // a contact's or a group's photo down, so every face stayed initials. This is the one step that does: after the chat // list loads, the photos of its rows' faces are asked of the core (`media.ensure`, which answers from the phone when // they are here) one at a time in the background, never in Low Data Mode, each once until a refusal's wait runs out. #if os(iOS) import CottonKit import Foundation @MainActor final class ChatFaces { static let shared = ChatFaces() private struct Wait { var tries: Int; var until: ContinuousClock.Instant } private var waits: [String: Wait] = [:] private var out: Set = [] /// Keys the core already answered from this phone or fetched: not asked again while the app runs. private var landed: Set = [] private var running: Task? private struct Ask: Encodable, Sendable { let key: String } private struct Landed: Decodable, Sendable { let path: String; let fetched: Bool? } /// The photos behind `keys`, brought down one at a time behind whatever is already running; none in Low Data Mode. func bring(_ keys: [String], core: any CottonCalling, constrained: Bool = ChatLowData.shared.constrained) { guard !constrained else { return } let wanted = keys.filter { !$0.isEmpty && !landed.contains($0) } guard !wanted.isEmpty else { return } let before = running running = Task { await before?.value for key in wanted { _ = await self.bring(key, core: core) } } } /// One photo: whether the core fetched it just now (false when it was here already, refused, or waiting). @discardableResult func bring(_ key: String, core: any CottonCalling) async -> Bool { guard !key.isEmpty, !out.contains(key), !landed.contains(key) else { return false } if let wait = waits[key], ContinuousClock.now < wait.until { return false } out.insert(key) defer { out.remove(key) } do { let answer = try await core.call(CottonMethod.mediaEnsure, Ask(key: key), as: Landed.self) waits[key] = nil landed.insert(key) return answer.fetched == true } catch { let tries = (waits[key]?.tries ?? 0) + 1 let backoff = ChatPictureTries.backoff waits[key] = Wait(tries: tries, until: .now + backoff[min(tries, backoff.count) - 1]) return false } } /// The last pass, for a check that waits on it. func settled() async { await running?.value } } #endif