~ knotless Search/
↑↓ move↵ open⇧↵ all results

geesawra.industries/old-fella

Old people's social media app

0
1
main
137 lines · 6.3 KB Raw History
import SwiftUI

extension ProfileView {
    var profileHeader: some View {
        VStack(alignment: .leading, spacing: 16) {
            HStack(alignment: .center, spacing: 14) {
                if !displayPreferences.hideAvatars { profileAvatar }
                VStack(alignment: .leading, spacing: 4) {
                    Text(author.map { displayPreferences.name($0) } ?? "Profile")
                        .appFont(.title2, weight: .bold)
                        .accessibilityIdentifier("profileDisplayName")
                    if !displayPreferences.handlesOnly {
                        Text("@\(author?.handle ?? actorID)")
                            .appFont(.subheadline)
                            .foregroundStyle(.secondary)
                    }
                    if profile?.isFollowedBy == true, actorID != model.account?.id {
                        Text("Follows you")
                            .appFont(.caption)
                            .foregroundStyle(.secondary)
                            .accessibilityIdentifier("profileFollowsYou")
                    }
                }
                Spacer(minLength: 4)
                if author?.id != model.account?.id {
                    if model.uncertainFollowIDs.contains(actorID) {
                        Text("Follow status unknown")
                            .appFont(.footnote)
                            .foregroundStyle(.secondary)
                    } else if isFollowing {
                        Button {
                            Task { await model.toggleFollow(actorID: actorID) }
                        } label: {
                            Text("Following").fontWeight(.semibold)
                        }
                        .buttonStyle(.bordered)
                        .disabled(model.activeFollowIDs.contains(actorID) || model.uncertainFollowIDs.contains(actorID))
                        .accessibilityIdentifier("followButton")
                    } else {
                        Button {
                            Task { await model.toggleFollow(actorID: actorID) }
                        } label: {
                            Text("Follow").fontWeight(.semibold)
                        }
                        .buttonStyle(.borderedProminent)
                        .disabled(model.activeFollowIDs.contains(actorID) || model.uncertainFollowIDs.contains(actorID))
                        .accessibilityIdentifier("followButton")
                    }
                }
            }

            if let profile {
                if !profile.bio.isEmpty {
                    Text(profile.bio)
                        .fixedSize(horizontal: false, vertical: true)
                }
                let links = profile.links.filter { link in
                    guard let decision = model.contentDecision(author: profile.author, labels: link.moderationLabels)
                    else { return false }
                    return decision.action != .hide && !decision.ui(.contentView).blur
                }
                if !links.isEmpty || profile.website != nil {
                    ProfileLinksLayout {
                        ForEach(links) { link in
                            Link(destination: link.url) {
                                Label(link.title, systemImage: "link")
                            }
                            .accessibilityIdentifier("profileLink-\(link.id)")
                        }
                        if let website = profile.website {
                            Link(destination: website) {
                                Label(website.host() ?? website.absoluteString, systemImage: "link")
                            }
                            .accessibilityIdentifier("profileWebsiteLink")
                        }
                    }
                    .frame(maxWidth: .infinity, alignment: .leading)
                    .multilineTextAlignment(.leading)
                    .buttonStyle(.bordered)
                    .buttonBorderShape(.capsule)
                    .appFont(.subheadline)
                    .padding(.vertical, 4)
                    .externalLinkBrowser { url in
                        PostTextFacets.openInternalURL(url, openActor: openProfile, openPost: openPost)
                    }
                }
                HStack(spacing: 16) {
                    connectionButton(profile.followingCount, kind: .following)
                    connectionButton(profile.followerCount, kind: .followers)
                }
                .accessibilityElement(children: .contain)
                .accessibilityIdentifier("profileCounts")
                if let location = profile.location {
                    Label(location, systemImage: "mappin.and.ellipse")
                        .appFont(.subheadline)
                        .foregroundStyle(.secondary)
                }
            }
        }
        .frame(maxWidth: .infinity, alignment: .leading)
    }

    @ViewBuilder
    var profileAvatar: some View {
        if let author, model.contentDecision(author: author)?.ui(.avatar).blur == false, let url = author.avatarURL {
            Button {
                selectedAvatar = ImageViewerRequest(
                    urls: [AvatarImageURL.fullSize(url)], descriptions: [:], index: 0, requiresGIFReveal: false)
            } label: {
                AccountAvatar(
                    account: Account(handle: author.handle, displayName: author.name, id: author.id, avatarURL: url),
                    size: 72, isDecorative: true)
            }
            .buttonStyle(.plain)
            .accessibilityLabel("View profile photo")
            .accessibilityIdentifier("profileAvatarButton")
        } else {
            AccountAvatar(account: nil, size: 72, isDecorative: false)
                .accessibilityLabel("Profile photo")
        }
    }

    func connectionButton(_ count: Int, kind: ProfileConnectionKind) -> some View {
        Button {
            openConnections(kind)
        } label: {
            HStack(spacing: 4) {
                Text(count.formatted()).fontWeight(.semibold).foregroundStyle(.primary)
                Text(kind.title).foregroundStyle(.secondary)
            }
            .appFont(.subheadline)
            .contentShape(Rectangle())
        }
        .buttonStyle(.plain)
        .accessibilityIdentifier("profile-\(kind.rawValue)")
    }
}