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

geesawra.industries/old-fella

Old people's social media app

0
1
main
122 lines · 4.8 KB Raw History
import SwiftUI

#if DEBUG
    import UIKit
#endif

struct PostMedia: View {
    var imageURLs: [URL] = []
    var imageThumbnailURLs: [URL: URL] = [:]
    var pendingImages: [ImageAttachment] = []
    var gifURLs: [URL] = []
    var inlineGIFVideoURLs: [URL] = []
    var videoURLs: [URL] = []
    var descriptions: [URL: String] = [:]
    var isContext = false
    var decision: ModerationPolicy.Decision?
    var accessibilityID = ""
    @State private var mediaOverride: Bool?

    private var hasMedia: Bool {
        !imageURLs.isEmpty || !pendingImages.isEmpty || !gifURLs.isEmpty || !inlineGIFVideoURLs.isEmpty
            || !videoURLs.isEmpty
    }

    var body: some View {
        Group {
            if hasMedia {
                let media = decision?.contentMedia
                let hidden =
                    (media?.noOverride == true && media?.blur == true) || (mediaOverride ?? (media?.blur == true))
                if hidden {
                    if media?.noOverride == true {
                        mediaButtonLabel("Media warning", systemImage: "exclamationmark.triangle")
                    } else {
                        Button {
                            mediaOverride = false
                        } label: {
                            mediaButtonLabel("Show media", systemImage: "exclamationmark.triangle")
                        }
                        .buttonStyle(.plain)
                        .accessibilityIdentifier("showMedia-\(accessibilityID)")
                        .accessibilityHint("Media warning")
                    }
                } else {
                    mediaContent
                    if decision?.hasAdultMediaLabel == true {
                        Button {
                            mediaOverride = true
                        } label: {
                            mediaButtonLabel("Hide media", systemImage: "eye.slash")
                        }
                        .buttonStyle(.plain)
                        .accessibilityIdentifier("hideMedia-\(accessibilityID)")
                    }
                }
            }
        }
        .onChange(of: decision) { _, _ in mediaOverride = nil }
        .onChange(of: accessibilityID) { _, _ in mediaOverride = nil }
    }

    private func mediaButtonLabel(_ title: String, systemImage: String) -> some View {
        Label(title, systemImage: systemImage)
            .appFont(.subheadline, weight: .medium)
            .foregroundStyle(.primary)
            .frame(maxWidth: .infinity, minHeight: 48)
            .background {
                RoundedRectangle(cornerRadius: 12, style: .continuous)
                    .fill(Color(uiColor: .secondarySystemGroupedBackground))
                    .overlay {
                        RoundedRectangle(cornerRadius: 12, style: .continuous)
                            .fill(AppPalette.mediaWarningBackground)
                    }
            }
            .contentShape(RoundedRectangle(cornerRadius: 12, style: .continuous))
    }

    @ViewBuilder private var mediaContent: some View {
        if imageURLs.isEmpty && !pendingImages.isEmpty {
            PendingPostImages(images: pendingImages)
        } else if !imageURLs.isEmpty {
            InlineImages(
                urls: imageURLs, thumbnailURLs: imageThumbnailURLs, descriptions: descriptions,
                isReplyContext: isContext, requiresGIFReveal: isContext)
        }
        if !gifURLs.isEmpty || !inlineGIFVideoURLs.isEmpty {
            GIFMedia(
                imageURLs: gifURLs, videoURLs: inlineGIFVideoURLs, descriptions: descriptions, isReplyContext: isContext
            )
        }
        if !videoURLs.isEmpty {
            VideoLinks(urls: videoURLs)
        }
    }
}

#if DEBUG
    #Preview("Post media") {
        let renderer = UIGraphicsImageRenderer(size: CGSize(width: 480, height: 300))
        let image = renderer.image { context in
            UIColor.systemTeal.setFill()
            context.fill(CGRect(x: 0, y: 0, width: 480, height: 300))
            UIColor.systemYellow.setFill()
            context.cgContext.fillEllipse(in: CGRect(x: 320, y: 40, width: 90, height: 90))
        }
        let attachment = ImageAttachment(
            data: image.pngData()!, altText: "A yellow sun against a teal sky", mimeType: "image/png")
        return ScrollView {
            VStack(alignment: .leading, spacing: 20) {
                PostMedia(pendingImages: [attachment])
                PostMedia(
                    pendingImages: [
                        attachment,
                        ImageAttachment(
                            data: attachment.data, altText: attachment.altText, mimeType: attachment.mimeType)
                    ], isContext: true)
                PostMedia(videoURLs: [URL(fileURLWithPath: "/preview/video.mp4")])
            }
            .padding()
        }
    }
#endif