Optimize avatar view (#950)

* Reduce AvatarPlaceholderView body calls

* Resize avatars
This commit is contained in:
Alex Grebenyuk 2023-02-19 12:34:16 -05:00 committed by GitHub
parent 76be2b5671
commit da289dbc1f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -56,19 +56,19 @@ public struct AvatarView: View {
.fill(.gray)
.frame(width: size.size.width, height: size.size.height)
} else {
if isInCaptureMode, let image = Nuke.ImagePipeline.shared.cache.cachedImage(for: .init(url: url))?.image {
if isInCaptureMode, let url = url, let image = Nuke.ImagePipeline.shared.cache.cachedImage(for: makeImageRequest(for: url))?.image {
Image(uiImage: image)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: size.size.width, height: size.size.height)
} else {
LazyImage(url: url) { state in
LazyImage(request: url.map(makeImageRequest)) { state in
if let image = state.image {
image
.resizable()
.aspectRatio(contentMode: .fit)
} else {
placeholderView
AvatarPlaceholderView(size: size)
}
}
.animation(nil)
@ -82,6 +82,10 @@ public struct AvatarView: View {
)
}
private func makeImageRequest(for url: URL) -> ImageRequest {
ImageRequest(url: url, processors: [.resize(size: size.size)])
}
private var clipShape: some Shape {
switch theme.avatarShape {
case .circle:
@ -90,17 +94,20 @@ public struct AvatarView: View {
return AnyShape(RoundedRectangle(cornerRadius: size.cornerRadius))
}
}
@ViewBuilder
private var placeholderView: some View {
if size == .badge {
Circle()
.fill(.gray)
.frame(width: size.size.width, height: size.size.height)
} else {
RoundedRectangle(cornerRadius: size.cornerRadius)
.fill(.gray)
.frame(width: size.size.width, height: size.size.height)
}
}
}
private struct AvatarPlaceholderView: View {
let size: AvatarView.Size
var body: some View {
if size == .badge {
Circle()
.fill(.gray)
.frame(width: size.size.width, height: size.size.height)
} else {
RoundedRectangle(cornerRadius: size.cornerRadius)
.fill(.gray)
.frame(width: size.size.width, height: size.size.height)
}
}
}