Impressia/Vernissage/Widgets/CommentsSection.swift

126 lines
5.7 KiB
Swift
Raw Normal View History

2023-01-03 14:09:22 +01:00
//
// https://mczachurski.dev
// Copyright © 2023 Marcin Czachurski and the repository contributors.
// Licensed under the MIT License.
//
import SwiftUI
import MastodonSwift
struct CommentsSection: View {
@EnvironmentObject var applicationState: ApplicationState
@State public var statusId: String
@State public var withDivider = true
2023-01-04 17:56:01 +01:00
@State private var context: Context?
2023-01-03 14:09:22 +01:00
private let contentWidth = Int(UIScreen.main.bounds.width) - 50
var body: some View {
VStack {
if let context = context {
ForEach(context.descendants, id: \.id) { status in
HStack (alignment: .top) {
AsyncImage(url: status.account?.avatar) { image in
image
.resizable()
.clipShape(Circle())
.aspectRatio(contentMode: .fit)
} placeholder: {
Image(systemName: "person.circle")
.resizable()
2023-01-04 17:56:01 +01:00
.foregroundColor(Color("MainTextColor"))
2023-01-03 14:09:22 +01:00
}
.frame(width: 32.0, height: 32.0)
VStack (alignment: .leading) {
HStack (alignment: .top) {
Text(status.account?.displayName ?? status.account?.username ?? "")
2023-01-04 17:56:01 +01:00
.foregroundColor(Color("DisplayNameColor"))
2023-01-03 14:09:22 +01:00
.font(.footnote)
.fontWeight(.bold)
Text("@\(status.account?.username ?? "")")
2023-01-04 17:56:01 +01:00
.foregroundColor(Color("LightGrayColor"))
.font(.footnote)
Spacer()
Text(status.createdAt.toRelative(.isoDateTimeMilliSec))
.foregroundColor(Color("LightGrayColor").opacity(0.5))
2023-01-03 14:09:22 +01:00
.font(.footnote)
2023-01-04 17:56:01 +01:00
/*
Image(systemName: "message")
.foregroundColor(Color.accentColor)
Image(systemName: "hand.thumbsup")
.foregroundColor(Color.accentColor)
*/
2023-01-03 14:09:22 +01:00
}
.padding(.bottom, -10)
2023-01-04 17:56:01 +01:00
2023-01-03 14:09:22 +01:00
HTMLFormattedText(status.content, withFontSize: 14, andWidth: contentWidth)
.padding(.leading, -4)
2023-01-04 17:56:01 +01:00
if status.mediaAttachments.count > 0 {
LazyVGrid(columns: Array(repeating: .init(.flexible()), count: status.mediaAttachments.count == 1 ? 1 : 2), alignment: .center, spacing: 4) {
ForEach(status.mediaAttachments, id: \.id) { attachment in
AsyncImage(url: status.mediaAttachments[0].url) { image in
image
.resizable()
.scaledToFill()
.frame(minWidth: 0, maxWidth: .infinity)
.frame(height: status.mediaAttachments.count == 1 ? 200 : 100)
.cornerRadius(10)
.shadow(color: Color("MainTextColor").opacity(0.3), radius: 2)
} placeholder: {
Image(systemName: "photo")
.resizable()
.scaledToFit()
.frame(minWidth: 0, maxWidth: .infinity)
.frame(height: status.mediaAttachments.count == 1 ? 200 : 100)
.foregroundColor(Color("MainTextColor"))
.opacity(0.05)
}
}
}
.padding(.bottom, 8)
}
2023-01-03 14:09:22 +01:00
}
}
.padding(.horizontal, 8)
2023-01-04 17:56:01 +01:00
.padding(.bottom, 8)
2023-01-03 14:09:22 +01:00
CommentsSection(statusId: status.id, withDivider: false)
if withDivider {
Rectangle()
.size(width: UIScreen.main.bounds.width, height: 4)
2023-01-04 17:56:01 +01:00
.fill(Color("MainTextColor"))
.opacity(0.1)
2023-01-03 14:09:22 +01:00
}
}
}
2023-01-04 17:56:01 +01:00
}
.task {
2023-01-03 14:09:22 +01:00
do {
if let accountData = applicationState.accountData {
2023-01-04 17:56:01 +01:00
self.context = try await TimelineService.shared.getComments(
for: statusId,
and: accountData)
2023-01-03 14:09:22 +01:00
}
} catch {
print("Error \(error.localizedDescription)")
}
}
}
}
struct CommentsSection_Previews: PreviewProvider {
static var previews: some View {
CommentsSection(statusId: "", withDivider: true)
}
}