Impressia/Vernissage/Widgets/StatusView/CommentsSection.swift

77 lines
2.8 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
@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-10 20:38:02 +01:00
var onNewStatus: ((_ context: StatusViewModel) -> Void)?
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-03 14:09:22 +01:00
if let context = context {
2023-01-10 20:38:02 +01:00
ForEach(context.descendants.toStatusViewModel(), id: \.id) { statusViewModel in
2023-01-08 11:32:39 +01:00
VStack(alignment: .leading, spacing: 0) {
if withDivider {
Divider()
.foregroundColor(.mainTextColor)
.padding(0)
2023-01-03 14:09:22 +01:00
}
2023-01-08 11:32:39 +01:00
2023-01-10 20:38:02 +01:00
CommentBody(statusViewModel: statusViewModel)
2023-01-08 11:32:39 +01:00
2023-01-10 20:38:02 +01:00
if self.applicationState.showInteractionStatusId == statusViewModel.id {
2023-01-08 11:32:39 +01:00
VStack (alignment: .leading, spacing: 0) {
2023-01-10 20:38:02 +01:00
InteractionRow(statusViewModel: statusViewModel) {
self.onNewStatus?(statusViewModel)
2023-01-08 11:32:39 +01:00
}
.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-10 20:38:02 +01:00
CommentsSection(statusId: statusViewModel.id, withDivider: false) { context in
2023-01-08 15:43:55 +01:00
self.onNewStatus?(context)
2023-01-07 18:43:44 +01:00
}
2023-01-06 18:16:08 +01:00
}
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)")
}
}
}
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
}
struct CommentsSection_Previews: PreviewProvider {
static var previews: some View {
2023-01-08 15:43:55 +01:00
CommentsSection(statusId: "", withDivider: true)
2023-01-03 14:09:22 +01:00
}
}