Vernissage/Vernissage/Views/StatusView/Subviews/CommentsSectionView.swift

80 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.
2023-03-28 10:35:38 +02:00
// Licensed under the Apache License 2.0.
2023-01-03 14:09:22 +01:00
//
import SwiftUI
2023-02-19 10:32:38 +01:00
import PixelfedKit
2023-04-07 14:20:12 +02:00
import ClientKit
2023-04-07 14:38:50 +02:00
import ServicesKit
2023-04-07 16:59:18 +02:00
import EnvironmentKit
import WidgetsKit
2023-01-03 14:09:22 +01:00
2023-02-21 07:05:06 +01:00
struct CommentsSectionView: 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
@State public var statusId: String
@State private var commentViewModels: [CommentModel]?
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-02-21 07:05:06 +01:00
CommentBodyView(statusViewModel: commentViewModel.status)
2023-01-14 08:52:51 +01:00
if self.applicationState.showInteractionStatusId == commentViewModel.status.id {
VStack(alignment: .leading, spacing: 0) {
InteractionRow(statusModel: commentViewModel.status)
2023-01-23 18:01:27 +01:00
.foregroundColor(self.getInteractionRowTextColor())
.padding(.horizontal, 16)
.padding(.vertical, 8)
2023-01-07 18:43:44 +01:00
}
2023-09-19 19:32:27 +02:00
.background(Color.customGrayColor.opacity(0.5))
2023-01-08 11:32:39 +01:00
.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 {
2023-03-13 13:53:36 +01:00
ErrorService.shared.handle(error, message: "status.error.loadingCommentsFailed", showToastr: !Task.isCancelled)
}
}
2023-01-03 14:09:22 +01:00
}