import SwiftUI struct AccountAvatar: View { @Environment(AppModel.self) private var model @Environment(\.accessibilityReduceMotion) private var reduceMotion @Environment(\.colorScheme) private var colorScheme @AppStorage("hideAvatars") private var hideAvatars = false @State private var avatar: AvatarStore.Entry? @State private var imageURL: URL? let account: Account? let size: CGFloat var isDecorative = true var ringWidth: CGFloat = 1.5 var showsWhenHidden = false var showsDuringSessionRestore = false var body: some View { if !hideAvatars || showsWhenHidden { avatarContent } } private var avatarContent: some View { let isWaitingForAccount = showsDuringSessionRestore && account == nil && model.isRestoringSession let signedInAvatar = model.signedInAvatar.flatMap { $0.matches(account) ? $0 : nil } let remoteURL = signedInAvatar == nil ? account?.avatarURL : nil let remoteAvatar = remoteURL.flatMap { url in (imageURL == url ? avatar : nil) ?? model.avatars.cachedAvatar(for: url) } let displayedAvatar = isWaitingForAccount ? model.signedInAvatar?.launchEntry : signedInAvatar?.entry ?? remoteAvatar return Group { if let displayedAvatar { Image(uiImage: displayedAvatar.image).resizable().scaledToFill() .transition(.opacity) } else { avatarPlaceholder } } .frame(width: size, height: size) .clipShape(Circle()) .overlay { if let colors = displayedAvatar?.ringColors { Circle() .strokeBorder( Color(uiColor: (colorScheme == .dark ? colors.dark : colors.light).color), lineWidth: ringWidth ) .allowsHitTesting(false) .accessibilityHidden(true) } } .opacity(isWaitingForAccount && displayedAvatar == nil ? 0 : 1) .accessibilityHidden(isDecorative) .animation(reduceMotion ? nil : .easeInOut(duration: 0.25), value: signedInAvatar?.entry.map(ObjectIdentifier.init)) .task(id: remoteURL) { guard let url = remoteURL else { avatar = nil imageURL = nil return } avatar = model.avatars.cachedAvatar(for: url) imageURL = url let loaded = await model.avatars.avatar(for: url) if !Task.isCancelled, let loaded { withAnimation(reduceMotion ? nil : .easeInOut(duration: 0.25)) { imageURL = url avatar = loaded } } } } private var avatarPlaceholder: some View { let isDark = colorScheme == .dark return Circle() .fill(Color(.sRGB, white: isDark ? 0.27 : 0.82)) .overlay { Image(systemName: "person.fill") .symbolRenderingMode(.monochrome) .font(.system(size: size * 0.48)) .foregroundStyle(Color(.sRGB, white: isDark ? 0.72 : 0.33)) } } }