1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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))
}
}
}