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-06 18:16:08 +01:00
|
|
|
var onNewStatus: (_ context: Status) -> Void?
|
|
|
|
|
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)
|
2023-01-07 18:43:44 +01:00
|
|
|
.opacity(0.2)
|
2023-01-05 21:08:19 +01:00
|
|
|
}
|
|
|
|
|
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 18:16:08 +01:00
|
|
|
Text(status.account?.displayName ?? status.account?.acct ?? status.account?.username ?? "")
|
2023-01-06 13:05:21 +01:00
|
|
|
.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 18:16:08 +01:00
|
|
|
.foregroundColor(.lightGrayColor)
|
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 18:16:08 +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-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
|
|
|
}
|
2023-01-07 18:43:44 +01:00
|
|
|
.onTapGesture {
|
|
|
|
withAnimation(.linear(duration: 0.3)) {
|
|
|
|
if status.id == self.applicationState.showInteractionStatusId {
|
|
|
|
self.applicationState.showInteractionStatusId = ""
|
|
|
|
} else {
|
|
|
|
self.applicationState.showInteractionStatusId = status.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-03 14:09:22 +01:00
|
|
|
}
|
|
|
|
.padding(.horizontal, 8)
|
2023-01-04 17:56:01 +01:00
|
|
|
.padding(.bottom, 8)
|
2023-01-07 18:43:44 +01:00
|
|
|
|
|
|
|
if self.applicationState.showInteractionStatusId == status.id {
|
|
|
|
VStack (alignment: .leading) {
|
|
|
|
InteractionRow(statusId: status.id,
|
|
|
|
repliesCount: status.repliesCount,
|
|
|
|
reblogged: status.reblogged,
|
|
|
|
reblogsCount: status.reblogsCount,
|
|
|
|
favourited: status.favourited,
|
|
|
|
favouritesCount: status.favouritesCount,
|
|
|
|
bookmarked: status.bookmarked) {
|
|
|
|
onNewStatus(status)
|
|
|
|
}
|
|
|
|
.padding(.horizontal, 16)
|
|
|
|
.padding(.vertical, 8)
|
|
|
|
}
|
|
|
|
.background(Color.mainTextColor.opacity(0.08))
|
|
|
|
.transition(AnyTransition.move(edge: .top).combined(with: .opacity))
|
|
|
|
}
|
2023-01-04 17:56:01 +01:00
|
|
|
|
2023-01-06 18:16:08 +01:00
|
|
|
CommentsSection(statusId: status.id, withDivider: false) { context in
|
|
|
|
onNewStatus(context)
|
|
|
|
}
|
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 {
|
2023-01-06 18:16:08 +01:00
|
|
|
CommentsSection(statusId: "", withDivider: true) { context in }
|
2023-01-03 14:09:22 +01:00
|
|
|
}
|
|
|
|
}
|