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

95 lines
2.3 KiB
Swift
Raw Normal View History

2022-12-25 06:43:02 +00:00
import NukeUI
2023-01-17 10:36:01 +00:00
import Shimmer
import SwiftUI
2022-12-19 11:28:55 +00:00
public struct AvatarView: View {
@Environment(\.redactionReasons) private var reasons
@EnvironmentObject private var theme: Theme
public enum Size {
2022-12-29 06:02:10 +00:00
case account, status, embed, badge, boost
2023-01-17 10:36:01 +00:00
public 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)
2022-12-29 06:02:10 +00:00
case .boost:
return .init(width: 12, height: 12)
}
}
2023-01-17 10:36:01 +00:00
var cornerRadius: CGFloat {
switch self {
2022-12-29 06:02:10 +00:00
case .badge, .boost:
return size.width / 2
default:
return 4
}
}
}
2023-01-17 10:36:01 +00:00
2022-12-19 11:28:55 +00:00
public let url: URL
public let size: Size
2023-01-17 10:36:01 +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
}
2023-01-17 10:36:01 +00:00
2022-12-19 11:28:55 +00:00
public var body: some View {
Group {
if reasons == .placeholder {
RoundedRectangle(cornerRadius: size.cornerRadius)
.fill(.gray)
.frame(width: size.size.width, height: size.size.height)
2023-01-17 10:36:01 +00:00
} else {
LazyImage(url: url) { state in
if let image = state.image {
image
.resizingMode(.aspectFit)
} else if state.isLoading {
placeholderView
.shimmering()
} else {
placeholderView
}
2022-12-19 11:28:55 +00:00
}
2023-01-17 10:36:01 +00:00
.processors([.resize(size: size.size), .roundedCorners(radius: size.cornerRadius)])
.frame(width: size.size.width, height: size.size.height)
}
}
.clipShape(clipShape)
.overlay(
clipShape.stroke(Color.primary.opacity(0.25), lineWidth: 1)
)
}
private var clipShape: some Shape {
switch theme.avatarShape {
case .circle:
return AnyShape(Circle())
case .rounded:
return AnyShape(RoundedRectangle(cornerRadius: size.cornerRadius))
2022-12-19 11:28:55 +00:00
}
}
2023-01-17 10:36:01 +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
}