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

geesawra.industries/old-fella

Old people's social media app

0
1
main
301 lines · 13.2 KB Raw History
import SwiftUI

struct ReplyContextPosts: View {
    @AppStorage("hideAvatars") private var hideAvatars = false
    @AppStorage("handlesOnly") private var handlesOnly = false
    private var displayPreferences: DisplayPreferences {
        DisplayPreferences(hideAvatars: hideAvatars, handlesOnly: handlesOnly)
    }
    @Environment(AppModel.self) private var model
    @AppStorage("hideReplyContexts") private var hideReplyContexts = false

    let post: Post
    let posts: [Post]
    var avatarIdentifierPrefix = "replyContextAvatar"
    var alwaysShowContext = false
    let openPost: (String) -> Void
    let openProfile: (String) -> Void
    let openComposer: (String?, String?) -> Void

    var body: some View {
        if hideReplyContexts && !alwaysShowContext, let parentID = post.replyParentURI {
            let parent = posts.first { $0.id == parentID } ?? model.post(id: parentID)
            let safeParent = parent.flatMap { model.canShowTimelineContent($0) ? $0 : nil }
            Button(
                action: { openPost(parentID) },
                label: {
                    Label(
                        safeParent.map { "In reply to \(displayPreferences.name($0.author))" } ?? "In reply to a post",
                        systemImage: "arrow.turn.down.right"
                    )
                    .appFont(.subheadline, weight: .medium)
                    .foregroundStyle(AppPalette.reply)
                }
            )
            .buttonStyle(.plain)
            .accessibilityIdentifier("replyContextLink-\(post.id)")
        } else if (!hideReplyContexts || alwaysShowContext) && !posts.isEmpty {
            Group(subviews: contextRows) { rows in
                ForEach(rows) { row in
                    row.padding(.bottom, row.id == rows.last?.id ? 2 : 0)
                }
            }
        }
    }

    @ViewBuilder
    private var contextRows: some View {
        ForEach(Array(posts.enumerated()), id: \.element.id) { index, contextPost in
            if index > 0 && model.hasMissingReplyContext(between: posts[index - 1], and: contextPost) {
                MissingReplyContextRow { openPost(contextPost.id) }
            }
            if model.canShowTimelineContent(contextPost) {
                ReplyContextRow(
                    post: contextPost,
                    isRoot: contextPost.id == post.replyRootURI,
                    avatarIdentifierPrefix: avatarIdentifierPrefix,
                    openActor: openProfile,
                    openLinkedPost: openPost
                )
                .padding(.bottom, 4)
            }
        }
        if let last = posts.last, model.hasMissingReplyContext(between: last, and: post) {
            MissingReplyContextRow { openPost(post.id) }
                .padding(.bottom, 4)
        }
    }
}

struct QuoteContextRow: View {
    @AppStorage("hideAvatars") private var hideAvatars = false
    @AppStorage("handlesOnly") private var handlesOnly = false
    private var displayPreferences: DisplayPreferences {
        DisplayPreferences(hideAvatars: hideAvatars, handlesOnly: handlesOnly)
    }
    @Environment(AppModel.self) private var model
    @AppStorage("hideQuotedPosts") private var hideQuotedPosts = false
    @State private var revealedQuote = false
    let quote: Post.Quote
    let fallbackPost: Post?
    var respectsHideSetting = true
    var showsAvatar = true
    var topPadding: CGFloat = 0
    var title: String?
    var accessibilityID = "quotedPostLink"
    var openProfile: ((String) -> Void)?
    var openActor: (String) -> Void = { _ in }
    var openLinkedPost: (String) -> Void = { _ in }
    let openPost: () -> Void

    var body: some View {
        Group {
            if model.isQuoteHidden(quote, fallbackPost: fallbackPost) {
                EmptyView()
            } else if let decision = model.quoteContentDecision(quote, fallbackPost: fallbackPost),
                decision.ui(.contentList).blur && !revealedQuote {
                VStack(alignment: .leading, spacing: 8) {
                    Text("Content warning")
                    if !decision.reasons.contains(where: {
                        if case .label(_, _, let noOverride) = $0 { return noOverride }
                        return false
                    }) {
                        Button("Show post") { revealedQuote = true }
                            .accessibilityIdentifier("revealQuotedPost-\(quote.postID)")
                    }
                }
            } else if respectsHideSetting && hideQuotedPosts {
                Button(
                    action: openPost,
                    label: {
                        HStack(spacing: 6) {
                            Image(systemName: "quote.bubble")
                            Text("See post")
                            Image(systemName: "chevron.right")
                                .font(.caption.weight(.semibold))
                        }
                        .appFont(.subheadline, weight: .medium)
                        .foregroundStyle(.tint)
                        .frame(minHeight: 44)
                        .contentShape(Rectangle())
                    }
                )
                .buttonStyle(.plain)
                .accessibilityLabel("See post")
                .accessibilityIdentifier("quotedPostLink")
            } else {
                quotedPost
                    .padding(.top, topPadding)
            }
        }
        .onChange(of: quote) { _, _ in revealedQuote = false }
        .onChange(of: fallbackPost) { _, _ in revealedQuote = false }
        .onChange(of: model.quoteContentDecision(quote, fallbackPost: fallbackPost)) { _, _ in revealedQuote = false }
        .onChange(of: model.contentRevision) { _, _ in revealedQuote = false }
    }

    private func profileAction(for author: Post.Author?) -> (() -> Void)? {
        guard let author else { return nil }
        return { (openProfile ?? openActor)(author.id) }
    }

    private var quotedPost: some View {
        let author = quote.author ?? fallbackPost?.author
        let usesFallback = quote.author == nil
        let text = quote.text ?? fallbackPost?.text ?? ""
        let facets = quote.text == nil ? (fallbackPost?.facets ?? []) : quote.facets
        return ContextCard(
            kind: .quote,
            title: title ?? author.map { "Quoted \(displayPreferences.name($0))" } ?? "View quoted post",
            author: author,
            date: quote.createdAt ?? fallbackPost?.createdAt,
            showsAvatar: showsAvatar,
            headerIdentifier: accessibilityID,
            avatarIdentifier: "notificationPostAvatar-\(quote.postID)",
            openProfile: profileAction(for: author),
            openPost: openPost,
            content: {
                PostText(
                    text: text,
                    facets: facets,
                    preparedText: model.preparedPostText.renderedText(
                        id: quote.postID, text: text, facets: facets, embeddedQuote: quote.text != nil),
                    font: .subheadline,
                    accessibilityID: "quotedPostText-\(quote.postID)",
                    openPost: openPost,
                    openActor: openActor,
                    openLinkedPost: openLinkedPost
                )
                PostLinks(urls: usesFallback ? (fallbackPost?.links ?? []) : quote.links, openPost: openLinkedPost)
                PostMedia(
                    imageURLs: usesFallback ? (fallbackPost?.imageURLs ?? []) : quote.imageURLs,
                    imageThumbnailURLs: usesFallback
                        ? (fallbackPost?.imageThumbnailURLs ?? [:]) : quote.imageThumbnailURLs,
                    gifURLs: usesFallback ? (fallbackPost?.gifURLs ?? []) : quote.gifURLs,
                    inlineGIFVideoURLs: usesFallback
                        ? (fallbackPost?.inlineGIFVideoURLs ?? []) : quote.inlineGIFVideoURLs,
                    videoURLs: usesFallback ? (fallbackPost?.videoURLs ?? []) : quote.videoURLs,
                    descriptions: usesFallback ? (fallbackPost?.imageDescriptions ?? [:]) : quote.imageDescriptions,
                    isContext: true,
                    decision: model.quoteContentDecision(quote, fallbackPost: fallbackPost),
                    accessibilityID: "quote-\(quote.postID)"
                )
            })
    }
}

struct MissingReplyContextRow: View {
    let openPost: () -> Void

    var body: some View {
        ContextCard(
            kind: .root, title: "", showsHeader: false, openPost: openPost,
            content: {
                Button(
                    action: openPost,
                    label: {
                        HStack(spacing: 10) {
                            Image(systemName: "ellipsis")
                                .font(.caption.weight(.bold))
                                .frame(width: 24, height: 24)
                                .background(.quaternary, in: Circle())
                                .accessibilityHidden(true)
                            Text("More posts in this thread")
                                .appFont(.caption, weight: .medium)
                            Spacer(minLength: 0)
                        }
                        .foregroundStyle(.secondary)
                        .frame(minHeight: 44)
                        .contentShape(Rectangle())
                    }
                )
                .buttonStyle(.plain)
                .accessibilityHint("Opens the conversation")
                .accessibilityIdentifier("missingReplyContext")
            }
        )
        .padding(.vertical, 3)
    }
}

struct ReplyContextRow: View {
    @AppStorage("hideAvatars") private var hideAvatars = false
    @AppStorage("handlesOnly") private var handlesOnly = false
    private var displayPreferences: DisplayPreferences {
        DisplayPreferences(hideAvatars: hideAvatars, handlesOnly: handlesOnly)
    }
    @Environment(AppModel.self) private var model
    let post: Post
    let isRoot: Bool
    var avatarIdentifierPrefix = "replyContextAvatar"
    let openActor: (String) -> Void
    let openLinkedPost: (String) -> Void

    var body: some View {
        ContextCard(
            kind: isRoot ? .root : .reply,
            title: "\(isRoot ? "Thread started by" : "Reply by") \(displayPreferences.name(post.author))",
            author: post.author,
            date: post.createdAt,
            avatarIdentifier: "\(avatarIdentifierPrefix)-\(post.id)",
            openProfile: { openActor(post.author.id) },
            openPost: { openLinkedPost(post.id) },
            content: {
                PostText(
                    text: post.text,
                    facets: post.facets,
                    preparedText: model.preparedPostText.renderedText(
                        id: post.id, text: post.text, facets: post.facets),
                    font: .subheadline,
                    accessibilityID: "replyContext-\(post.id)",
                    openPost: { openLinkedPost(post.id) },
                    openActor: openActor,
                    openLinkedPost: openLinkedPost
                )
                PostLinks(urls: post.links, openPost: openLinkedPost)
                PostMedia(
                    imageURLs: post.imageURLs, imageThumbnailURLs: post.imageThumbnailURLs,
                    gifURLs: post.gifURLs, inlineGIFVideoURLs: post.inlineGIFVideoURLs,
                    videoURLs: post.videoURLs, descriptions: post.imageDescriptions, isContext: true,
                    decision: model.contentDecision(for: post), accessibilityID: "reply-\(post.id)")
                if let quote = post.quote {
                    QuoteContextRow(
                        quote: quote, fallbackPost: model.post(id: quote.postID), topPadding: 2, openActor: openActor,
                        openLinkedPost: openLinkedPost
                    ) {
                        openLinkedPost(quote.postID)
                    }
                }
            }
        )
        .accessibilityElement(children: .contain)
        .accessibilityIdentifier("replyPostContext-\(post.id)")
    }
}

#if DEBUG
    #Preview("Post context") {
        PreviewHost { _ in
            ScrollView {
                VStack(alignment: .leading, spacing: 20) {
                    ReplyContextPosts(
                        post: PreviewContent.post, posts: [PreviewContent.post], alwaysShowContext: true,
                        openPost: { _ in }, openProfile: { _ in }, openComposer: { _, _ in })
                    MissingReplyContextRow(openPost: {})
                    ReplyContextRow(
                        post: PreviewContent.post, isRoot: true, openActor: { _ in }, openLinkedPost: { _ in })
                    ReplyContextRow(
                        post: PreviewContent.post, isRoot: false, openActor: { _ in }, openLinkedPost: { _ in })
                    QuoteContextRow(
                        quote: Post.Quote(
                            postID: "quoted", author: PreviewContent.author, text: "A quiet place to read."),
                        fallbackPost: nil, respectsHideSetting: false, openPost: {})
                    QuoteContextRow(
                        quote: Post.Quote(postID: "fallback"), fallbackPost: PreviewContent.post,
                        respectsHideSetting: false, showsAvatar: false, openPost: {})
                }
                .padding()
            }
        }
    }
#endif