-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPBAvatar.swift
178 lines (159 loc) · 4.33 KB
/
PBAvatar.swift
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//
// Playbook Swift Design System
//
// Copyright © 2024 Power Home Remodeling Group
// This software is distributed under the ISC License
//
// PBAvatar.swift
//
import SwiftUI
public struct PBAvatar: View {
var image: Image?
var name: String?
var size: Size
var status: PBOnlineStatus.Status?
var wrapped: Bool
var isActive: Bool
@Environment(\.colorScheme) var colorScheme
public init(
image: Image? = nil,
name: String? = nil,
size: Size = .medium,
status: PBOnlineStatus.Status? = nil,
wrapped: Bool = false,
isActive: Bool = true
) {
self.image = image
self.name = name
self.size = size
self.status = status
self.wrapped = wrapped
self.isActive = isActive
}
public var body: some View {
ZStack {
Group {
if let image = image {
image
.resizable()
.aspectRatio(contentMode: .fit)
} else if let initials = initials {
Text(initials)
.pbFont(.monogram(size.fontSize), color: .white)
}
}
.foregroundColor(.white)
.grayscale(isActive ? 0 : 1)
.frame(width: size.diameter, height: size.diameter)
.background(Color.status(.neutral))
.clipShape(Circle())
if wrapped {
Circle()
.strokeBorder(avatarBorderColor, lineWidth: 1)
.frame(width: size.diameter + 1, height: size.diameter + 1)
}
if let status = self.status {
PBOnlineStatus(status: status, size: avatarStatusSize, variant: .border)
.grayscale(isActive ? 0 : 1)
.offset(
x: (size.diameter/2 - size.diameter/9) * size.statusXModifier,
y: (size.diameter/2 - size.diameter/6) * size.statusYModifier
)
}
}
}
}
public extension PBAvatar {
var initials: String? {
guard let name = name else { return nil }
let names = name.split(separator: " ")
if let firstNameInitial = String(names.first ?? " ").first {
if names.count == 1 {
return "\(firstNameInitial.uppercased())"
} else if let lastNameInitial = String(names.last ?? " ").first {
return "\(firstNameInitial.uppercased())\(lastNameInitial.uppercased())"
}
}
return nil
}
enum Size: CaseIterable, Hashable {
public static var allCases: [PBAvatar.Size] = []
case xxSmall
case xSmall
case small
case medium
case large
case xLarge
case defaultStacked
case defaultStackedIndicator
case smallStacked
case smallStackedIndicator
case custom(_ size: CGFloat)
var diameter: CGFloat {
switch self {
case .xxSmall: return 20
case .xSmall: return 28
case .small: return 38
case .medium: return 60
case .large: return 80
case .xLarge: return 100
case .defaultStacked: return 34
case .defaultStackedIndicator: return 18
case .smallStacked: return 22
case .smallStackedIndicator: return 16
case .custom(let size): return size
}
}
var fontSize: CGFloat {
return diameter * 0.38
}
var statusXModifier: CGFloat {
switch self {
case .xxSmall: return 1.3
case .xSmall: return 1.2
case .small: return 0.95
case .medium: return 1.05
case .large: return 1.12
case .xLarge: return 1.16
default: return 0
}
}
var statusYModifier: CGFloat {
switch self {
case .xxSmall: return -0.8
case .xSmall: return -1
case .small: return -1.1
case .medium: return 1
case .large: return 0.78
case .xLarge: return 0.68
default: return 0
}
}
var avatarCases: [Size] {
[.xLarge, .large, .medium, .small, .xSmall, .xxSmall]
}
}
var avatarStatusSize: PBOnlineStatus.Size {
switch size {
case .xxSmall, .xSmall, .smallStacked, .smallStackedIndicator, .defaultStacked, .defaultStackedIndicator: return .small
case .small, .medium: return .medium
case .large, .xLarge: return .large
case .custom: return .medium
}
}
var avatarBorderColor: Color {
switch colorScheme {
case .light: return Color.BorderColor.borderColor(.light)
case .dark: return Color.BorderColor.borderColor(.dark)
default:
return Color.white
}
}
}
#Preview {
PBAvatar(
image: Image("andrew"),
size: .xLarge,
status: .online
)
}