Tusker/Tusker/Screens/Explore/TrendingLinkCardView.swift

97 lines
3.0 KiB
Swift

//
// TrendingLinkCardView.swift
// Tusker
//
// Created by Shadowfacts on 11/8/23.
// Copyright © 2023 Shadowfacts. All rights reserved.
//
#if os(visionOS)
import SwiftUI
import Pachyderm
import WebURLFoundationExtras
import HTMLStreamer
struct TrendingLinkCardView: View {
let card: Card
private var imageURL: URL? {
if let image = card.image {
URL(image)
} else {
nil
}
}
private var descriptionText: String {
var converter = TextConverter(configuration: .init(insertNewlines: false))
return converter.convert(html: card.description)
}
var body: some View {
VStack(alignment: .leading, spacing: 8) {
AsyncImage(url: imageURL, content: { image in
image
.resizable()
}, placeholder: {
Rectangle()
.fill(.tertiary)
})
.aspectRatio(4/3, contentMode: .fill)
.overlay(alignment: .bottom) {
Text(card.title.trimmingCharacters(in: .whitespacesAndNewlines))
.font(.headline)
.lineLimit(2)
.padding(4)
.background(.regularMaterial)
.padding(-4)
}
.padding(-4)
Text(descriptionText)
.font(.callout)
.lineLimit(3, reservesSpace: true)
HStack(alignment: .bottom, spacing: 4) {
VStack(alignment: .leading, spacing: 4) {
if let providerName = card.providerName {
Text(providerName.trimmingCharacters(in: .whitespacesAndNewlines))
.font(.caption2)
.lineLimit(1)
}
let sorted = card.history!.sorted(by: { $0.day < $1.day })
let lastTwo = sorted[(sorted.count - 2)...]
let accounts = lastTwo.map(\.accounts).reduce(0, +)
let uses = lastTwo.map(\.uses).reduce(0, +)
// U+2009 THIN SPACE
Text("\(accounts.formatted())\u{2009}\(Image(systemName: "person")), \(uses.formatted())\u{2009}\(Image(systemName: "square.text.square"))")
.font(.caption2)
}
if let history = card.history {
CardHistoryView(history: history)
}
}
}
.padding(4)
.glassBackgroundEffect(in: RoundedRectangle(cornerRadius: 12.5))
}
}
private struct CardHistoryView: UIViewRepresentable {
typealias UIViewType = TrendHistoryView
let history: [History]
func makeUIView(context: Context) -> TrendHistoryView {
TrendHistoryView()
}
func updateUIView(_ uiView: TrendHistoryView, context: Context) {
uiView.setHistory(history)
}
}
//#Preview {
// TrendingLinkCardView()
//}
#endif