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) } }