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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import SwiftUI
struct ProfileRevealKey: Equatable {
let actorID: String
let decision: ModerationPolicy.Decision?
let contentRevision: UUID
}
struct ProfileReportSubject: Identifiable {
let id = UUID()
let subject: ReportSubject
}
struct ProfilePostLoadKey: Hashable {
let actorID: String
let hasLoaded: Bool
}
#if DEBUG
#Preview("Profile") {
PreviewHost { _ in
NavigationStack {
ProfileView(
actorID: PreviewContent.author.id, openPost: { _ in }, openProfile: { _ in },
openConnections: { _ in }, openComposer: { _, _ in })
}
}
}
#endif
struct ProfileView: View {
@AppStorage("hideAvatars") var hideAvatars = false
@AppStorage("handlesOnly") var handlesOnly = false
var displayPreferences: DisplayPreferences {
DisplayPreferences(hideAvatars: hideAvatars, handlesOnly: handlesOnly)
}
@Environment(AppModel.self) var model
@Environment(\.accessibilityReduceMotion) var reduceMotion
@State private var showingSettings = false
@State private var isPullRefreshing = false
@State var reportSubject: ProfileReportSubject?
@State var accountAction: String?
@State var showingAccountConfirmation = false
@State var showingAccountError = false
@State var accountStateLoaded = false
@State var revealedProfile: ProfileRevealKey?
@State var selectedAvatar: ImageViewerRequest?
let actorID: String
let openPost: (String) -> Void
let openProfile: (String) -> Void
let openConnections: (ProfileConnectionKind) -> Void
let openComposer: (String?, String?) -> Void
var profile: Profile? { model.profile(actorID: actorID) }
var author: Post.Author? { profile?.author }
var authorPosts: [Post] { model.posts(for: actorID) }
var isFollowing: Bool { model.followingActorIDs.contains(actorID) || profile?.isFollowing == true }
var revealKey: ProfileRevealKey {
ProfileRevealKey(
actorID: actorID, decision: author.flatMap { model.contentDecision(author: $0) },
contentRevision: model.contentRevision)
}
var profileVisible: Bool {
guard let author, let decision = model.contentDecision(author: author), decision.action != .hide else {
return false
}
let presentation = decision.ui(.profileView)
return !presentation.blur || (revealedProfile == revealKey && !presentation.noOverride)
}
var postLoadKey: ProfilePostLoadKey {
ProfilePostLoadKey(actorID: actorID, hasLoaded: model.authorPosts[actorID] != nil)
}
var isLoadingProfileDetails: Bool {
if model.contentPreferences == nil {
return model.contentPreferencesError == nil
}
return profile == nil && model.profileErrors[actorID] == nil
}
var isLoadingProfilePosts: Bool {
profileVisible && !postLoadKey.hasLoaded && model.profileErrors[actorID] == nil
}
}
extension ProfileView {
var profileList: some View {
Group {
if !isPullRefreshing && (isLoadingProfileDetails || isLoadingProfilePosts) {
profileRows
.safeAreaInset(edge: .top, spacing: 0) {
ProgressView()
.tint(AppPalette.accent)
.frame(maxWidth: .infinity)
.frame(height: 60)
.accessibilityIdentifier("initialProfileRefreshControl")
.accessibilityLabel(isLoadingProfileDetails ? "Loading profile" : "Loading profile posts")
}
} else {
profileRows
.refreshable {
guard !model.loadingProfileIDs.contains(actorID) else { return }
isPullRefreshing = true
defer { isPullRefreshing = false }
await model.loadProfile(actorID: actorID, forceRefresh: true)
}
.background {
ScrollRefreshControl(isRefreshing: false, isActive: false)
.allowsHitTesting(false)
.accessibilityHidden(true)
}
}
}
}
private var profileRows: some View {
List {
if isLoadingProfileDetails {
profilePostsPlaceholder(label: "Loading profile", identifier: "profileDetailsLoading", rows: 2)
.listRowInsets(EdgeInsets())
.listRowSeparator(.hidden)
} else {
VStack(spacing: 0) {
if model.contentPreferences != nil, profile == nil, let error = model.profileErrors[actorID] {
ContentUnavailableView {
Label("Could not load profile", systemImage: "exclamationmark.triangle")
} description: {
Text(error)
} actions: {
Button("Retry") { Task { await model.loadProfile(actorID: actorID) } }
.buttonStyle(.borderedProminent)
.accessibilityIdentifier("retryProfileButton")
}
} else if profileVisible {
profileHeader
.padding(EdgeInsets(top: 20, leading: 16, bottom: 18, trailing: 16))
.transition(.opacity)
} else {
Text(model.contentPreferencesError ?? "Profile hidden by content preferences")
.padding()
if let author, let decision = model.contentDecision(author: author), decision.action != .hide,
decision.ui(.profileView).blur, !decision.ui(.profileView).noOverride {
Button("Show profile") { revealedProfile = revealKey }
}
if model.contentPreferencesError != nil {
Button("Retry") { Task { await model.reloadContentPreferences() } }
Link("Manage preferences in Bluesky", destination: BlueskyLinks.moderation)
}
}
PostDivider()
}
.animation(reduceMotion ? nil : .easeInOut(duration: 0.25), value: profileVisible)
.listRowInsets(EdgeInsets())
.listRowSeparator(.hidden)
}
Group {
if !profileVisible {
EmptyView()
} else if model.authorPosts[actorID] == nil, let error = model.profileErrors[actorID] {
ContentUnavailableView {
Label("Could not load profile posts", systemImage: "exclamationmark.triangle")
} description: {
Text(error)
} actions: {
Button("Retry") { Task { await model.loadProfile(actorID: actorID) } }
.buttonStyle(.borderedProminent)
.accessibilityIdentifier("retryProfileButton")
}
.listRowSeparator(.hidden)
.listRowInsets(EdgeInsets(top: 12, leading: 16, bottom: 12, trailing: 16))
} else if model.authorPosts[actorID] == nil {
profilePostsPlaceholder(label: "Loading profile posts", identifier: "profilePostsLoading", rows: 2)
.listRowSeparator(.hidden)
.listRowInsets(EdgeInsets(top: 12, leading: 16, bottom: 12, trailing: 16))
} else if model.isHidden(author?.id) {
ContentUnavailableView(
"Posts hidden", systemImage: "eye.slash",
description: Text("Unmute and unblock this account to see its posts.")
)
.listRowSeparator(.hidden)
} else if authorPosts.isEmpty && model.profileCursors[actorID] == nil {
ContentUnavailableView(
"No posts yet", systemImage: "text.bubble",
description: Text("Posts from this profile will appear here.")
)
.accessibilityIdentifier("profilePostsEmpty")
.listRowSeparator(.hidden)
.listRowInsets(EdgeInsets(top: 12, leading: 16, bottom: 12, trailing: 16))
} else {
ForEach(authorPosts) { post in
PostListItem(
post: post,
repostedBy: model.profileRepostedBy[actorID]?[post.id],
openPost: openPost,
openProfile: openProfile,
openComposer: openComposer
)
.transition(.opacity)
.listRowSeparator(.hidden)
.listRowInsets(EdgeInsets())
}
}
}
.animation(reduceMotion ? nil : .easeInOut(duration: 0.25), value: postLoadKey)
if profileVisible && model.profileCursors[actorID] != nil {
VStack(spacing: 8) {
if model.loadingMoreProfileIDs.contains(actorID) {
profilePostsPlaceholder(
label: "Loading more posts", identifier: "profilePostsMoreLoading", rows: 1)
} else {
Button("Load more posts") {
Task { await model.loadNextProfilePage(actorID: actorID) }
}
.buttonStyle(.bordered)
.accessibilityIdentifier("loadMoreProfilePostsButton")
}
}
.frame(maxWidth: .infinity)
.listRowSeparator(.hidden)
.listRowInsets(EdgeInsets(top: 12, leading: 16, bottom: 18, trailing: 16))
}
}
.listStyle(.plain)
.accessibilityIdentifier("profilePostList")
}
var body: some View {
profileList
.appNavigationTitle("Profile")
.toolbar {
if let author, author.id != model.account?.id {
ToolbarItem(placement: .topBarTrailing) {
Menu {
if accountStateLoaded, let state = model.moderationStates[author.id] {
Button(state.isMuted ? "Unmute account" : "Mute account") {
accountAction = state.isMuted ? "Unmute" : "Mute"
showingAccountConfirmation = true
}
.disabled(
model.moderationBusy.contains(author.id)
|| model.uncertainModerationIDs[author.id]?.contains(.mute) == true)
Button(
state.isBlocked ? "Unblock account" : "Block account",
role: state.isBlocked ? nil : .destructive
) {
accountAction = state.isBlocked ? "Unblock" : "Block"
showingAccountConfirmation = true
}
.disabled(
model.moderationBusy.contains(author.id)
|| model.uncertainModerationIDs[author.id]?.contains(.block) == true)
} else {
Button("Load account controls") {
Task {
model.clearModerationError()
accountStateLoaded = await model.loadModeration(actorID: author.id)
showingAccountError = model.moderationError != nil
}
}
.disabled(model.moderationBusy.contains(author.id))
}
Button("Report account") {
reportSubject = ProfileReportSubject(subject: .account(author.id))
}
} label: {
Image(systemName: "ellipsis")
}
.accessibilityLabel("Account controls")
}
}
ToolbarItem(placement: .topBarTrailing) {
ShareLink(item: URL(string: "https://bsky.app/profile/\(author?.handle ?? actorID)")!) {
Image(systemName: "square.and.arrow.up")
}
.accessibilityLabel("Share profile")
}
if UIDevice.current.userInterfaceIdiom == .phone, actorID == model.account?.id {
ToolbarItem(placement: .topBarTrailing) {
Button("Settings", systemImage: "gearshape") { showingSettings = true }
.labelStyle(.iconOnly)
.accessibilityIdentifier("profileSettingsButton")
}
}
}
.sheet(item: $reportSubject) { PostReportView(subject: $0.subject) }
.sheet(isPresented: $showingSettings) { SettingsView(showsDoneButton: true) }
.alert("Confirm action", isPresented: $showingAccountConfirmation) {
if let action = accountAction {
Button(action, role: .destructive) {
Task {
model.clearModerationError()
await model.setModeration(actorID: actorID, block: action == "Block" || action == "Unblock")
showingAccountError = model.moderationError != nil
}
}
}
Button("Cancel", role: .cancel) {}
} message: {
Text("\(accountAction ?? "Change") this account?")
}
.alert("Account action", isPresented: $showingAccountError) {
Button("OK") { model.clearModerationError() }
} message: {
Text(model.moderationError ?? "Could not load account controls. Try again.")
}
.fullScreenCover(item: $selectedAvatar) { request in ImageViewer(request: request) }
.onChange(of: displayPreferences.hideAvatars) { _, hidden in
if hidden { selectedAvatar = nil }
}
.onChange(of: revealKey) { _, _ in
revealedProfile = nil
selectedAvatar = nil
}
.task(id: actorID) {
accountStateLoaded = false
if model.contentPreferences != nil { await model.loadProfile(actorID: actorID) }
if actorID != model.account?.id {
accountStateLoaded = await model.loadModeration(actorID: actorID)
}
}
.onChange(of: model.contentRevision) { _, _ in Task { await model.loadProfile(actorID: actorID) } }
}
func profilePostsPlaceholder(label: String, identifier: String, rows: Int) -> some View {
PostLoadingPlaceholder(label: label, count: rows)
.accessibilityIdentifier(identifier)
}
}