-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathCommonProfileHeader.swift
More file actions
185 lines (176 loc) · 7.52 KB
/
CommonProfileHeader.swift
File metadata and controls
185 lines (176 loc) · 7.52 KB
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
179
180
181
182
183
184
185
import SwiftUI
import KotlinSharedUI
enum CommonProfileHeaderConstants {
static let headerHeight: CGFloat = 200
static let avatarSize: CGFloat = 96
}
struct CommonProfileHeader: View {
let user: UiProfile
let relation: UiState<UiRelation>
let isMe: UiState<KotlinBoolean>
let onFollowClick: (UiRelation) -> Void
var body: some View {
ZStack(alignment: .top) {
if let banner = user.banner, !banner.isEmpty {
Color.clear.overlay {
NetworkImage(data: banner)
.frame(height: CommonProfileHeaderConstants.headerHeight)
}
.frame(height: CommonProfileHeaderConstants.headerHeight)
} else {
Rectangle()
.foregroundColor(.gray)
.frame(height: CommonProfileHeaderConstants.headerHeight)
}
VStack(alignment: .leading) {
HStack {
VStack {
Spacer()
.frame(
height: CommonProfileHeaderConstants.headerHeight -
CommonProfileHeaderConstants.avatarSize / 2
)
AvatarView(data: user.avatar)
.frame(width: CommonProfileHeaderConstants.avatarSize, height: CommonProfileHeaderConstants.avatarSize)
}
Spacer()
VStack {
Spacer()
.frame(height: CommonProfileHeaderConstants.headerHeight)
if case .success(let data) = onEnum(of: isMe), !data.data.boolValue {
switch onEnum(of: relation) {
case .success(let relationState): Button(action: {
onFollowClick(relationState.data)
}, label: {
let text = if relationState.data.blocking {
String(localized: "relation_blocked")
} else if relationState.data.following {
String(localized: "relation_following")
} else if relationState.data.hasPendingFollowRequestFromYou {
String(localized: "relation_requested")
} else {
String(localized: "relation_follow")
}
Text(text)
})
.buttonStyle(.borderless)
case .loading: Button(action: {}, label: {
Text("Button")
})
.buttonStyle(.borderless)
.redacted(reason: .placeholder)
case .error: EmptyView()
}
}
}
}
ListCardView {
VStack(
alignment: .leading,
spacing: 8
) {
RichText(text: user.name)
.font(.headline)
.frame(maxWidth: .infinity, alignment: .leading)
HStack {
Text(user.handle)
.font(.subheadline)
.foregroundColor(.gray)
ForEach(0..<user.mark.count, id: \.self) { index in
let mark = user.mark[index]
switch mark {
case .cat: Image("fa-cat")
case .verified: Image("fa-circle-check")
case .locked: Image("fa-lock")
case .bot: Image("fa-robot")
}
}
}
if let desc = user.description_ {
RichText(text: desc)
}
if let bottomContent = user.bottomContent {
switch onEnum(of: bottomContent) {
case .fields(let data):
FieldsView(fields: data.fields)
case .iconify(let data):
let list = data.items.map { $0.key }
ForEach(Array(data.items.keys), id: \.name) { key in
let value = data.items[key]
Label(
title: {
if let text = value {
RichText(text: text)
.font(.body)
}
},
icon: {
switch key {
case .location: Image("fa-location-dot")
case .url: Image("fa-globe")
case .verify: Image("fa-circle-check")
}
}
)
}
}
}
MatrixView(followCount: user.matrices.followsCountHumanized, fansCount: user.matrices.fansCountHumanized)
}
.padding()
}
}
.padding([.horizontal])
}
}
}
struct MatrixView: View {
let followCount: String
let fansCount: String
var body: some View {
HStack {
Text(followCount)
Text("matrix_following")
Divider()
Text(fansCount)
Text("matrix_followers")
}
.font(.caption)
.foregroundStyle(.secondary)
}
}
struct FieldsView: View {
let fields: [String: UiRichText]
var body: some View {
if fields.count > 0 {
VStack(alignment: .leading, spacing: 8) {
let keys = fields.map {
$0.key
}
ForEach(0..<keys.count, id: \.self) { index in
let key = keys[index]
Text(key)
.font(.caption)
.foregroundStyle(.secondary)
if let richText = fields[key] {
RichText(text: richText)
.font(.body)
}
if index != keys.count - 1 {
Divider()
}
}
.padding(.horizontal)
}
.padding(.vertical)
#if os(iOS)
.background(Color(UIColor.secondarySystemBackground))
#else
.background(Color(NSColor.windowBackgroundColor))
#endif
.clipShape(RoundedRectangle(cornerRadius: 8))
} else {
EmptyView()
}
}
}