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

geesawra.industries/old-fella

Old people's social media app

0
1
main
303 lines · 16.8 KB Raw History
import SwiftUI

struct NotificationRow: 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 state: NotificationGroupState
    private var group: NotificationGroup { state.group }
    let openRoute: (AppRoute) -> Void
    let openComposer: (String?, String?) -> Void
    let showGroup: (NotificationGroup) -> Void
    @ScaledMetric(relativeTo: .subheadline) private var notificationQuoteIndent = 7

    var body: some View {
        let _: Void = ViewUpdateMetrics.record("NotificationRow-\(group.id)")
        let notification = group.notifications[0]
        VStack(spacing: 0) {
            if group.notifications.count > 1 {
                groupedNotification(group)
            } else {
                VStack(alignment: .leading, spacing: 10) {
                    if notification.reason == .like || notification.reason == .repost,
                        let relatedPost = notification.relatedPost {
                        let post = model.post(id: relatedPost.id) ?? relatedPost
                        HStack(alignment: .top, spacing: 12) {
                            if !displayPreferences.hideAvatars {
                                notificationAvatar(notification.author, notificationID: notification.id)
                            }
                            VStack(alignment: .leading, spacing: 8) {
                                NotificationActivityHeader(
                                    reason: notification.reason, date: notification.indexedAt,
                                    isRead: notification.isRead
                                ) {
                                    HStack(spacing: 4) {
                                        Text(displayPreferences.name(notification.author)).lineLimit(1).truncationMode(
                                            .tail)
                                        Text(notification.reason.message).fixedSize()
                                        Spacer(minLength: 0)
                                    }
                                }
                                QuoteContextRow(
                                    quote: Post.Quote(postID: post.id),
                                    fallbackPost: post,
                                    respectsHideSetting: false,
                                    showsAvatar: false,
                                    title: "Your post",
                                    accessibilityID: "notificationPost-\(notification.id)",
                                    openProfile: { openRoute(.profile($0)) },
                                    openActor: { openRoute(.profile($0)) },
                                    openLinkedPost: { openRoute(.post($0)) },
                                    openPost: { openRoute(.post(post.id)) }
                                )
                                .padding(.leading, notificationQuoteIndent)
                            }
                        }
                    } else if notification.reason == .reply, let relatedPost = notification.relatedPost {
                        let reply = model.post(id: relatedPost.id) ?? relatedPost
                        VStack(alignment: .leading, spacing: 8) {
                            NotificationActivityHeader(
                                reason: notification.reason, date: notification.indexedAt, isRead: notification.isRead
                            ) {
                                HStack(spacing: 4) {
                                    Text(
                                        notification.replyIsToAccount(model.account?.id)
                                            ? "Reply to your post" : "Reply in a thread"
                                    ).fixedSize()
                                    Spacer(minLength: 0)
                                }
                            }
                            ReplyContextPosts(
                                post: reply,
                                posts: notification.parentPost.map { [$0] } ?? [],
                                avatarIdentifierPrefix: "notificationReplyAvatar",
                                alwaysShowContext: true,
                                openPost: { openRoute(.post($0)) },
                                openProfile: { openRoute(.profile($0)) },
                                openComposer: { replyTo, quotePostID in
                                    showComposer(replyTo: replyTo, quotePostID: quotePostID)
                                }
                            )
                            PostRow(
                                post: reply,
                                openPost: { openRoute(.post(reply.id)) },
                                openProfile: { openRoute(.profile(reply.author.id)) },
                                openActor: { openRoute(.profile($0)) },
                                openQuotedPost: { openRoute(.post($0)) },
                                openComposer: { replyTo, quotePostID in
                                    showComposer(replyTo: replyTo, quotePostID: quotePostID)
                                }
                            )
                        }
                    } else if notification.reason == .mention || notification.reason == .quote
                        || notification.reason == .repost, let relatedPost = notification.relatedPost {
                        let post = model.post(id: relatedPost.id) ?? relatedPost
                        VStack(alignment: .leading, spacing: 10) {
                            NotificationActivityHeader(
                                reason: notification.reason, date: notification.indexedAt, isRead: notification.isRead
                            ) {
                                HStack(spacing: 4) {
                                    Text(displayPreferences.name(notification.author)).lineLimit(1).truncationMode(
                                        .tail)
                                    Text(notification.reason.message).fixedSize()
                                    Spacer(minLength: 0)
                                }
                                .accessibilityIdentifier("notificationActivity-\(notification.id)")
                            }
                            PostRow(
                                post: post,
                                openPost: { openRoute(.post(post.id)) },
                                openProfile: { openRoute(.profile(post.author.id)) },
                                openActor: { openRoute(.profile($0)) },
                                openQuotedPost: { openRoute(.post($0)) },
                                openComposer: { replyTo, quotePostID in
                                    showComposer(replyTo: replyTo, quotePostID: quotePostID)
                                }
                            )
                        }
                    } else if notification.reason == .follow {
                        AccountSummaryRow(
                            author: notification.author, bio: notification.author.bio,
                            avatarIdentifier: "notificationAvatar-\(notification.id)",
                            contentIdentifier: "notification-\(notification.id)",
                            openProfile: { openRoute(.profile(notification.author.id)) },
                            header: {
                                NotificationActivityHeader(
                                    reason: notification.reason, date: notification.indexedAt,
                                    isRead: notification.isRead
                                ) {
                                    HStack(spacing: 4) {
                                        Text(displayPreferences.name(notification.author)).lineLimit(1)
                                            .truncationMode(.tail)
                                        Text(notification.reason.message).fixedSize()
                                        Spacer(minLength: 0)
                                    }
                                }
                            }
                        )
                    } else {
                        HStack(alignment: .top, spacing: 12) {
                            if !displayPreferences.hideAvatars {
                                notificationAvatar(notification.author, notificationID: notification.id)
                            }
                            Button {
                                if notification.reason.includesPost, let postID = notification.postID {
                                    openRoute(.post(postID))
                                } else {
                                    openRoute(.profile(notification.author.id))
                                }
                            } label: {
                                HStack(alignment: .top, spacing: 12) {
                                    VStack(alignment: .leading, spacing: 8) {
                                        NotificationActivityHeader(
                                            reason: notification.reason, date: notification.indexedAt,
                                            isRead: notification.isRead
                                        ) {
                                            HStack(spacing: 4) {
                                                Text(displayPreferences.name(notification.author)).lineLimit(1)
                                                    .truncationMode(.tail)
                                                Text(notification.reason.message).fixedSize()
                                                Spacer(minLength: 0)
                                            }
                                        }
                                        if let post = notification.relatedPost {
                                            HStack(alignment: .top, spacing: 8) {
                                                AccountAvatar(
                                                    account: Account(
                                                        handle: post.author.handle, displayName: post.author.name,
                                                        id: post.author.id, avatarURL: post.author.avatarURL),
                                                    size: 30
                                                )
                                                VStack(alignment: .leading, spacing: 3) {
                                                    Text(displayPreferences.name(post.author))
                                                        .appFont(.caption, weight: .semibold)
                                                        .foregroundStyle(.primary)
                                                    Text(post.text.isEmpty ? "Photo post" : post.text)
                                                        .appFont(.subheadline)
                                                        .foregroundStyle(.secondary)
                                                        .lineLimit(3)
                                                        .multilineTextAlignment(.leading)
                                                }
                                                .frame(maxWidth: .infinity, alignment: .leading)
                                            }
                                            .padding(10)
                                            .background(
                                                Color(uiColor: .secondarySystemGroupedBackground),
                                                in: RoundedRectangle(cornerRadius: 10, style: .continuous))
                                        } else if notification.postID != nil {
                                            Text("View post")
                                                .appFont(.caption, weight: .medium)
                                                .foregroundStyle(.tint)
                                        }
                                    }
                                }
                                .contentShape(Rectangle())
                            }
                            .buttonStyle(.plain)
                            .accessibilityHint(
                                notification.reason.includesPost && notification.postID != nil
                                    ? "Opens the related post" : "Opens this account profile"
                            )
                            .accessibilityIdentifier("notification-\(notification.id)")
                        }
                    }
                }
            }
        }
        .modifier(ContentListRowStyle(openPost: backgroundPostAction))
    }

    private var backgroundPostAction: (() -> Void)? {
        guard group.notifications.count == 1,
            let relatedPost = group.notifications[0].relatedPost,
            model.canShowTimelineContent(model.post(id: relatedPost.id) ?? relatedPost)
        else { return nil }
        return { openRoute(.post(relatedPost.id)) }
    }

    private func showComposer(replyTo: String?, quotePostID: String?) {
        openComposer(replyTo, quotePostID)
    }

}

extension NotificationRow {
    private func groupedNotification(_ group: NotificationGroup) -> some View {
        let notifications = group.notifications
        let first = notifications[0]
        return HStack(alignment: .top, spacing: 12) {
            if !displayPreferences.hideAvatars {
                groupedNotificationAvatar(group)
            }
            VStack(alignment: .leading, spacing: 8) {
                NotificationActivityHeader(
                    reason: first.reason, date: first.indexedAt, isRead: !notifications.contains(where: { !$0.isRead })
                ) {
                    Button {
                        showGroup(group)
                    } label: {
                        Text(first.reason == .like ? "Many liked this post" : "Many reposted this post")
                            .lineLimit(1)
                            .frame(maxWidth: .infinity, alignment: .leading)
                    }
                    .buttonStyle(.plain)
                }
                if let relatedPost = first.relatedPost {
                    let post = model.post(id: relatedPost.id) ?? relatedPost
                    QuoteContextRow(
                        quote: Post.Quote(postID: post.id), fallbackPost: post, respectsHideSetting: false,
                        showsAvatar: false, title: "Your post", accessibilityID: "notificationPost-\(first.id)",
                        openProfile: { openRoute(.profile($0)) }, openActor: { openRoute(.profile($0)) },
                        openLinkedPost: { openRoute(.post($0)) }, openPost: { openRoute(.post(post.id)) }
                    )
                    .padding(.leading, notificationQuoteIndent)
                } else if let postID = first.postID {
                    Button("View post") { openRoute(.post(postID)) }
                }
            }
        }
    }

    private func groupedNotificationAvatar(_ group: NotificationGroup) -> some View {
        let notifications = group.notifications
        let first = notifications[0]
        return Button {
            showGroup(group)
        } label: {
            ZStack {
                Circle().fill(Color(uiColor: .secondarySystemFill))
                    .frame(width: 30, height: 30)
                ForEach(Array(notifications.prefix(3).enumerated()), id: \.element.id) { index, notification in
                    AuthorAvatar(author: notification.author, size: 20)
                        .shadow(color: .black.opacity(0.2), radius: 1.5, x: 0, y: 1)
                        .position(
                            x: notifications.count == 2
                                ? (index == 0 ? 12 : 32) : (index == 0 ? 22 : index == 1 ? 12 : 32),
                            y: notifications.count == 2 ? 22 : (index == 0 ? 12 : 32)
                        )
                }
            }
            .frame(width: 44, height: 44)
            .contentShape(Rectangle())
        }
        .buttonStyle(.plain)
        .accessibilityLabel(
            first.reason == .like ? "View people who liked this post" : "View people who reposted this post")
    }

    private func notificationAvatar(_ author: Post.Author, notificationID: String) -> some View {
        Button {
            openRoute(.profile(author.id))
        } label: {
            AuthorAvatar(author: author, size: 42)
                .frame(minWidth: 44, minHeight: 44)
                .contentShape(Rectangle())
        }
        .buttonStyle(.plain)
        .accessibilityLabel("View \(displayPreferences.name(author))'s profile")
        .accessibilityIdentifier("notificationAvatar-\(notificationID)")
    }
}