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

geesawra.industries/old-fella

Old people's social media app

0
1
main
83 lines · 3.5 KB Raw History
import SwiftUI

struct PostAuthorHeader: View {
    @AppStorage("appFont") private var appFont = AppFont.system.rawValue
    @AppStorage("hideAvatars") private var hideAvatars = false
    @AppStorage("handlesOnly") private var handlesOnly = false
    private var displayPreferences: DisplayPreferences {
        DisplayPreferences(hideAvatars: hideAvatars, handlesOnly: handlesOnly)
    }
    @ScaledMetric(relativeTo: .body) private var handleFontSize: CGFloat = 18
    let author: Post.Author
    let postID: String
    var date: Date?
    var threadPosition: ThreadPosition?
    var allowsAvatarImage = true
    var accessibilityID = ""
    let openProfile: () -> Void

    var body: some View {
        HStack(alignment: .center, spacing: 8) {
            Button(action: openProfile) {
                HStack(alignment: .center, spacing: 10) {
                    AuthorAvatar(
                        author: author, size: 44, allowsImage: allowsAvatarImage, ringWidth: ImageBorderStyle.width)
                    VStack(alignment: .leading, spacing: 2) {
                        Text(displayPreferences.name(author))
                            .font(
                                handlesOnly
                                    ? (AppFont(rawValue: appFont) ?? .system).fixedFont(size: handleFontSize)
                                    : (AppFont(rawValue: appFont) ?? .system).font(.body)
                            )
                            .fontWeight(.semibold)
                            .foregroundStyle(.primary)
                            .lineLimit(1)
                            .truncationMode(.tail)
                        if !displayPreferences.handlesOnly {
                            Text("@\(author.handle)")
                                .appFont(.subheadline)
                                .foregroundStyle(.secondary)
                                .lineLimit(1)
                                .truncationMode(.tail)
                        }
                    }
                }
                .frame(minHeight: 44)
                .padding(.vertical, handlesOnly ? 6 : 0)
                .contentShape(Rectangle())
            }
            .buttonStyle(.plain)
            .accessibilityIdentifier(accessibilityID)
            .layoutPriority(1)
            Spacer(minLength: 0)
            if let threadPosition {
                Text("\(threadPosition.index.formatted())/\(threadPosition.count.formatted())")
                    .appFont(.caption, weight: .semibold)
                    .monospacedDigit()
                    .foregroundStyle(AppPalette.accent)
                    .padding(.horizontal, 6)
                    .padding(.vertical, 2)
                    .background(AppPalette.accent.opacity(0.12), in: Capsule())
                    .fixedSize()
                    .accessibilityLabel("Thread post \(threadPosition.index) of \(threadPosition.count)")
                    .accessibilityIdentifier("threadPosition-\(postID)")
            }
            if let date {
                PostTimestamp(date: date)
                    .fixedSize()
                    .accessibilityIdentifier("postTimestamp-\(postID)")
            }
        }
    }
}

#if DEBUG
    #Preview("Post author") {
        VStack(spacing: 20) {
            PostAuthorHeader(
                author: PreviewContent.author, postID: "post-1", date: PreviewContent.date, openProfile: {})
            PostAuthorHeader(author: PreviewContent.author, postID: "post-2", allowsAvatarImage: false, openProfile: {})
        }
        .padding()
    }
#endif