Vernissage/Vernissage/Views/StatusView/Subviews/CommentBodyView.swift

97 lines
4.2 KiB
Swift
Raw Normal View History

2023-01-10 11:30:30 +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-10 11:30:30 +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 16:59:18 +02:00
import EnvironmentKit
import WidgetsKit
2023-01-10 11:30:30 +01:00
2023-02-21 07:05:06 +01:00
struct CommentBodyView: View {
2023-01-10 11:30:30 +01:00
@EnvironmentObject var applicationState: ApplicationState
2023-01-25 15:39:04 +01:00
@EnvironmentObject var routerPath: RouterPath
2023-01-10 11:30:30 +01:00
@State var statusViewModel: StatusModel
2023-01-10 11:30:30 +01:00
var body: some View {
HStack(alignment: .top) {
2023-01-25 15:39:04 +01:00
UserAvatar(accountAvatar: self.statusViewModel.account.avatar, size: .comment)
.onTapGesture {
routerPath.navigate(to: .userProfile(accountId: self.statusViewModel.account.id,
accountDisplayName: self.statusViewModel.account.displayNameWithoutEmojis,
accountUserName: self.statusViewModel.account.acct))
}
VStack(alignment: .leading, spacing: 0) {
HStack(alignment: .top) {
2023-01-18 18:41:42 +01:00
Text(statusViewModel.account.displayNameWithoutEmojis)
2023-01-10 11:30:30 +01:00
.foregroundColor(.mainTextColor)
.font(.footnote)
.fontWeight(.bold)
2023-01-10 11:30:30 +01:00
Spacer()
2023-04-08 15:36:04 +02:00
if let createdAt = self.statusViewModel.createdAt.toDate(.isoDateTimeMilliSec) {
RelativeTime(date: createdAt)
2023-09-19 19:32:27 +02:00
.foregroundColor(.customGrayColor)
2023-04-08 15:36:04 +02:00
.font(.footnote)
}
2023-01-10 11:30:30 +01:00
}
2023-03-26 09:40:05 +02:00
MarkdownFormattedText(self.statusViewModel.content.asMarkdown)
.font(.footnote)
.environment(\.openURL, OpenURLAction { _ in .handled })
2023-01-23 18:01:27 +01:00
.padding(.top, 4)
2023-01-10 20:38:02 +01:00
if self.statusViewModel.mediaAttachments.count > 0 {
2023-01-10 11:30:30 +01:00
LazyVGrid(
2023-01-10 20:38:02 +01:00
columns: self.statusViewModel.mediaAttachments.count == 1 ? [GridItem(.flexible())]: [GridItem(.flexible()), GridItem(.flexible())],
2023-01-10 11:30:30 +01:00
alignment: .center,
spacing: 4
) {
2023-01-10 20:38:02 +01:00
ForEach(self.statusViewModel.mediaAttachments, id: \.id) { attachment in
2023-01-10 11:30:30 +01:00
AsyncImage(url: attachment.url) { image in
image
.resizable()
.scaledToFill()
.frame(minWidth: 0, maxWidth: .infinity)
2023-01-10 20:38:02 +01:00
.frame(height: self.statusViewModel.mediaAttachments.count == 1 ? 200 : 100)
2023-01-10 11:30:30 +01:00
.cornerRadius(10)
.shadow(color: .mainTextColor.opacity(0.3), radius: 2)
} placeholder: {
Image(systemName: "photo")
.resizable()
.scaledToFit()
.frame(minWidth: 0, maxWidth: .infinity)
2023-01-10 20:38:02 +01:00
.frame(height: self.statusViewModel.mediaAttachments.count == 1 ? 200 : 100)
2023-01-10 11:30:30 +01:00
.foregroundColor(.mainTextColor)
.opacity(0.05)
}
}
}
.padding(.bottom, 8)
}
}
2023-01-25 15:39:04 +01:00
.contentShape(Rectangle())
2023-01-10 11:30:30 +01:00
.onTapGesture {
withAnimation(.linear(duration: 0.3)) {
2023-01-10 20:38:02 +01:00
if self.statusViewModel.id == self.applicationState.showInteractionStatusId {
2023-01-14 08:52:51 +01:00
self.applicationState.showInteractionStatusId = String.empty()
2023-01-10 11:30:30 +01:00
} else {
2023-01-10 20:38:02 +01:00
self.applicationState.showInteractionStatusId = self.statusViewModel.id
2023-01-10 11:30:30 +01:00
}
}
}
}
.padding(8)
2023-01-10 20:38:02 +01:00
.background(self.getSelectedRowColor(statusViewModel: statusViewModel))
2023-01-10 11:30:30 +01:00
}
private func getSelectedRowColor(statusViewModel: StatusModel) -> Color {
2023-01-10 20:38:02 +01:00
return self.applicationState.showInteractionStatusId == statusViewModel.id ? Color.selectedRowColor : Color.systemBackground
2023-01-10 11:30:30 +01:00
}
}