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

76 lines
1.8 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-25 06:43:02 +00:00
import NukeUI
2022-12-19 11:28:55 +00:00
public struct AvatarView: View {
public enum Size {
2022-12-27 06:51:44 +00:00
case account, status, embed, badge
var size: CGSize {
switch self {
case .account:
return .init(width: 80, height: 80)
case .status:
return .init(width: 40, height: 40)
2022-12-27 06:51:44 +00:00
case .embed:
return .init(width: 34, height: 34)
case .badge:
return .init(width: 28, height: 28)
}
}
var cornerRadius: CGFloat {
switch self {
case .badge:
return size.width / 2
default:
return 4
}
}
}
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 = .status) {
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.cornerRadius)
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 {
2022-12-25 06:43:02 +00:00
LazyImage(url: url) { state in
if let image = state.image {
image
.resizingMode(.aspectFit)
} else if state.isLoading {
2022-12-23 17:47:19 +00:00
placeholderView
.shimmering()
2022-12-25 06:43:02 +00:00
} else {
2022-12-23 17:47:19 +00:00
placeholderView
2022-12-19 11:28:55 +00:00
}
}
2022-12-26 19:16:21 +00:00
.processors([.resize(size: size.size), .roundedCorners(radius: size.cornerRadius)])
2022-12-25 06:43:02 +00:00
.frame(width: size.size.width, height: size.size.height)
2022-12-19 11:28:55 +00:00
}
}
2022-12-23 17:47:19 +00:00
@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)
}
}
2022-12-19 11:28:55 +00:00
}