IceCubesApp/Packages/DesignSystem/Sources/DesignSystem/Views/AvatarView.swift

61 lines
1.6 KiB
Swift
Raw Normal View History

2022-12-19 11:28:55 +00:00
import SwiftUI
2022-12-22 18:00:23 +00:00
import Shimmer
2022-12-19 11:28:55 +00:00
public struct AvatarView: View {
public enum Size {
case profile, badge
var size: CGSize {
switch self {
case .profile:
return .init(width: 40, height: 40)
case .badge:
return .init(width: 28, height: 28)
}
}
}
2022-12-19 11:28:55 +00:00
@Environment(\.redactionReasons) private var reasons
public let url: URL
public let size: Size
2022-12-19 11:28:55 +00:00
public init(url: URL, size: Size = .profile) {
2022-12-19 11:28:55 +00:00
self.url = url
self.size = size
2022-12-19 11:28:55 +00:00
}
public var body: some View {
if reasons == .placeholder {
RoundedRectangle(cornerRadius: size == .profile ? 4 : size.size.width / 2)
2022-12-19 11:28:55 +00:00
.fill(.gray)
.frame(maxWidth: size.size.width, maxHeight: size.size.height)
2022-12-19 11:28:55 +00:00
} else {
AsyncImage(url: url) { phase in
switch phase {
case .empty:
if size == .badge {
Circle()
.fill(.gray)
2022-12-22 18:00:23 +00:00
.frame(width: size.size.width, height: size.size.height)
.shimmering()
} else {
2022-12-22 18:00:23 +00:00
RoundedRectangle(cornerRadius: size == .profile ? 4 : size.size.width / 2)
.fill(.gray)
.frame(width: size.size.width, height: size.size.height)
.shimmering()
}
case let .success(image):
2022-12-19 11:28:55 +00:00
image.resizable()
.aspectRatio(contentMode: .fit)
.cornerRadius(size == .profile ? 4 : size.size.width / 2)
.frame(maxWidth: size.size.width, maxHeight: size.size.height)
case .failure:
EmptyView()
@unknown default:
EmptyView()
2022-12-19 11:28:55 +00:00
}
}
2022-12-19 11:28:55 +00:00
}
}
}