Impressia/Vernissage/Widgets/CommentsSection.swift

123 lines
5.6 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
2023-01-05 21:08:19 +01:00
if withDivider {
Rectangle()
.size(width: UIScreen.main.bounds.width, height: 4)
.fill(Color.mainTextColor)
.opacity(0.1)
}
2023-01-03 14:09:22 +01:00
HStack (alignment: .top) {
2023-01-04 20:56:26 +01:00
if let account = status.account {
NavigationLink(destination: UserProfileView(
accountId: account.id,
accountDisplayName: account.displayName,
2023-01-06 13:05:21 +01:00
accountUserName: account.acct)
2023-01-04 20:56:26 +01:00
.environmentObject(applicationState)) {
AsyncImage(url: account.avatar) { image in
image
.resizable()
.clipShape(Circle())
.aspectRatio(contentMode: .fit)
} placeholder: {
Image(systemName: "person.circle")
.resizable()
2023-01-06 13:05:21 +01:00
.foregroundColor(.mainTextColor)
2023-01-04 20:56:26 +01:00
}
.frame(width: 32.0, height: 32.0)
}
2023-01-03 14:09:22 +01:00
}
VStack (alignment: .leading) {
HStack (alignment: .top) {
2023-01-06 13:05:21 +01:00
Text(status.account?.displayName ?? status.account?.acct ?? "")
.foregroundColor(.mainTextColor)
2023-01-03 14:09:22 +01:00
.font(.footnote)
.fontWeight(.bold)
2023-01-04 17:56:01 +01:00
Spacer()
Text(status.createdAt.toRelative(.isoDateTimeMilliSec))
2023-01-06 13:05:21 +01:00
.foregroundColor(.lightGrayColor.opacity(0.5))
2023-01-03 14:09:22 +01:00
.font(.footnote)
}
2023-01-04 17:56:01 +01:00
2023-01-03 14:09:22 +01:00
HTMLFormattedText(status.content, withFontSize: 14, andWidth: contentWidth)
2023-01-06 13:05:21 +01:00
.padding(.top, -16)
2023-01-03 14:09:22 +01:00
.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)
2023-01-06 13:05:21 +01:00
.shadow(color: .mainTextColor.opacity(0.3), radius: 2)
2023-01-04 17:56:01 +01:00
} placeholder: {
Image(systemName: "photo")
.resizable()
.scaledToFit()
.frame(minWidth: 0, maxWidth: .infinity)
.frame(height: status.mediaAttachments.count == 1 ? 200 : 100)
2023-01-06 13:05:21 +01:00
.foregroundColor(.mainTextColor)
2023-01-04 17:56:01 +01:00
.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)
}
}
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)
}
}