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) {
|
2023-01-04 20:56:26 +01:00
|
|
|
|
|
|
|
if let account = status.account {
|
|
|
|
NavigationLink(destination: UserProfileView(
|
|
|
|
accountId: account.id,
|
|
|
|
accountDisplayName: account.displayName,
|
|
|
|
accountUserName: account.username)
|
|
|
|
.environmentObject(applicationState)) {
|
|
|
|
AsyncImage(url: account.avatar) { image in
|
|
|
|
image
|
|
|
|
.resizable()
|
|
|
|
.clipShape(Circle())
|
|
|
|
.aspectRatio(contentMode: .fit)
|
|
|
|
} placeholder: {
|
|
|
|
Image(systemName: "person.circle")
|
|
|
|
.resizable()
|
2023-01-05 11:55:20 +01:00
|
|
|
.foregroundColor(Color.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) {
|
|
|
|
Text(status.account?.displayName ?? status.account?.username ?? "")
|
2023-01-05 11:55:20 +01:00
|
|
|
.foregroundColor(Color.mainTextColor)
|
2023-01-03 14:09:22 +01:00
|
|
|
.font(.footnote)
|
|
|
|
.fontWeight(.bold)
|
|
|
|
Text("@\(status.account?.username ?? "")")
|
2023-01-05 11:55:20 +01:00
|
|
|
.foregroundColor(Color.lightGrayColor)
|
2023-01-04 17:56:01 +01:00
|
|
|
.font(.footnote)
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
Text(status.createdAt.toRelative(.isoDateTimeMilliSec))
|
2023-01-05 11:55:20 +01:00
|
|
|
.foregroundColor(Color.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-04 20:56:26 +01:00
|
|
|
.padding(.top, -10)
|
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-05 11:55:20 +01:00
|
|
|
.shadow(color: 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-05 11:55:20 +01:00
|
|
|
.foregroundColor(Color.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)
|
|
|
|
|
|
|
|
if withDivider {
|
|
|
|
Rectangle()
|
|
|
|
.size(width: UIScreen.main.bounds.width, height: 4)
|
2023-01-05 11:55:20 +01:00
|
|
|
.fill(Color.mainTextColor)
|
2023-01-04 17:56:01 +01:00
|
|
|
.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)
|
|
|
|
}
|
|
|
|
}
|