Impressia/Vernissage/Widgets/StatusView/CommentsSection.swift

77 lines
2.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
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-02-03 15:16:30 +01:00
@EnvironmentObject var client: Client
2023-01-03 14:09:22 +01:00
2023-01-23 18:01:27 +01:00
@State public var statusId: String
@State private var commentViewModels: [CommentModel]?
2023-01-14 08:52:51 +01:00
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
}
.onChange(of: self.applicationState.newComment) { _ in
self.commentViewModels = nil
Task {
await self.loadComments()
2023-01-03 14:09:22 +01:00
}
}
.task {
await self.loadComments()
}
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
}
private func loadComments() async {
do {
self.commentViewModels = try await self.client.statuses?.comments(to: statusId) ?? []
} catch {
ErrorService.shared.handle(error, message: "Comments cannot be downloaded.", showToastr: !Task.isCancelled)
}
}
2023-01-03 14:09:22 +01:00
}