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
|
2023-01-10 08:04:25 +01:00
|
|
|
import MastodonKit
|
2023-01-03 14:09:22 +01:00
|
|
|
|
|
|
|
struct CommentsSection: View {
|
2023-01-08 11:32:39 +01:00
|
|
|
@Environment(\.colorScheme) var colorScheme
|
2023-01-03 14:09:22 +01:00
|
|
|
@EnvironmentObject var applicationState: ApplicationState
|
|
|
|
|
2023-01-23 18:01:27 +01:00
|
|
|
@State public var statusId: String
|
2023-01-14 08:52:51 +01:00
|
|
|
@State private var commentViewModels: [CommentViewModel]?
|
|
|
|
|
2023-01-03 14:09:22 +01:00
|
|
|
var body: some View {
|
2023-01-08 11:32:39 +01:00
|
|
|
VStack(alignment: .leading, spacing: 0) {
|
2023-01-14 08:52:51 +01:00
|
|
|
if let commentViewModels {
|
|
|
|
ForEach(commentViewModels, id: \.status.id) { commentViewModel in
|
2023-01-08 11:32:39 +01:00
|
|
|
VStack(alignment: .leading, spacing: 0) {
|
|
|
|
|
2023-01-14 08:52:51 +01:00
|
|
|
if commentViewModel.showDivider {
|
2023-01-08 11:32:39 +01:00
|
|
|
Divider()
|
2023-01-14 08:52:51 +01:00
|
|
|
.frame(height: 1)
|
|
|
|
.overlay(Color.placeholderText.opacity(0.3))
|
2023-01-08 11:32:39 +01:00
|
|
|
.padding(0)
|
2023-01-03 14:09:22 +01:00
|
|
|
}
|
2023-01-08 11:32:39 +01:00
|
|
|
|
2023-01-14 08:52:51 +01:00
|
|
|
CommentBody(statusViewModel: commentViewModel.status)
|
2023-01-08 11:32:39 +01:00
|
|
|
|
2023-01-14 08:52:51 +01:00
|
|
|
if self.applicationState.showInteractionStatusId == commentViewModel.status.id {
|
2023-01-08 11:32:39 +01:00
|
|
|
VStack (alignment: .leading, spacing: 0) {
|
2023-01-23 18:01:27 +01:00
|
|
|
InteractionRow(statusViewModel: commentViewModel.status)
|
|
|
|
.foregroundColor(self.getInteractionRowTextColor())
|
|
|
|
.padding(.horizontal, 16)
|
|
|
|
.padding(.vertical, 8)
|
2023-01-07 18:43:44 +01:00
|
|
|
}
|
2023-01-08 11:32:39 +01:00
|
|
|
.background(Color.lightGrayColor.opacity(0.5))
|
|
|
|
.transition(AnyTransition.move(edge: .top).combined(with: .opacity))
|
|
|
|
}
|
2023-01-06 18:16:08 +01:00
|
|
|
}
|
2023-01-03 14:09:22 +01:00
|
|
|
}
|
2023-01-14 08:52:51 +01:00
|
|
|
} else {
|
|
|
|
HStack {
|
|
|
|
Spacer()
|
|
|
|
LoadingIndicator()
|
|
|
|
Spacer()
|
|
|
|
}
|
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-28 21:09:31 +01:00
|
|
|
self.commentViewModels = try await StatusService.shared.comments(to: statusId, for: accountData)
|
2023-01-03 14:09:22 +01:00
|
|
|
}
|
|
|
|
} catch {
|
2023-01-20 13:47:38 +01:00
|
|
|
ErrorService.shared.handle(error, message: "Comments cannot be downloaded.", showToastr: !Task.isCancelled)
|
2023-01-03 14:09:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-08 11:32:39 +01:00
|
|
|
|
|
|
|
private func getInteractionRowTextColor() -> Color {
|
|
|
|
return self.colorScheme == .dark ? Color.black : Color.white
|
|
|
|
}
|
2023-01-03 14:09:22 +01:00
|
|
|
}
|
|
|
|
|