Impressia/Vernissage/Widgets/CommentsSection.swift

173 lines
8.6 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
import MastodonSwift
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-08 15:43:55 +01:00
var onNewStatus: ((_ context: Status) -> Void)?
2023-01-06 18:16:08 +01:00
2023-01-03 14:09:22 +01:00
private let contentWidth = Int(UIScreen.main.bounds.width) - 50
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 {
ForEach(context.descendants, id: \.id) { status 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
HStack (alignment: .top) {
2023-01-04 17:56:01 +01:00
2023-01-08 11:32:39 +01:00
if let account = status.account {
NavigationLink(destination: UserProfileView(
accountId: account.id,
accountDisplayName: account.displayName,
accountUserName: account.acct)
.environmentObject(applicationState)) {
AsyncImage(url: account.avatar) { image in
2023-01-04 17:56:01 +01:00
image
.resizable()
2023-01-08 11:32:39 +01:00
.clipShape(Circle())
.aspectRatio(contentMode: .fit)
2023-01-04 17:56:01 +01:00
} placeholder: {
2023-01-08 11:32:39 +01:00
Image(systemName: "person.circle")
2023-01-04 17:56:01 +01:00
.resizable()
2023-01-06 13:05:21 +01:00
.foregroundColor(.mainTextColor)
2023-01-04 17:56:01 +01:00
}
2023-01-08 11:32:39 +01:00
.frame(width: 32.0, height: 32.0)
2023-01-04 17:56:01 +01:00
}
2023-01-08 11:32:39 +01:00
}
VStack (alignment: .leading, spacing: 0) {
HStack (alignment: .top) {
Text(self.getUserName(status: status))
.foregroundColor(.mainTextColor)
.font(.footnote)
.fontWeight(.bold)
Spacer()
Text(status.createdAt.toRelative(.isoDateTimeMilliSec))
.foregroundColor(.lightGrayColor)
.font(.footnote)
2023-01-04 17:56:01 +01:00
}
2023-01-08 11:32:39 +01:00
HTMLFormattedText(status.content, withFontSize: 14, andWidth: contentWidth)
.padding(.top, -4)
.padding(.leading, -4)
2023-01-08 15:27:16 +01:00
if status.mediaAttachments.count > 0 {
LazyVGrid(
columns: status.mediaAttachments.count == 1 ? [GridItem(.flexible())]: [GridItem(.flexible()), GridItem(.flexible())],
alignment: .center,
spacing: 4
) {
ForEach(status.mediaAttachments, id: \.id) { attachment in
AsyncImage(url: attachment.url) { image in
image
.resizable()
.scaledToFill()
.frame(minWidth: 0, maxWidth: .infinity)
.frame(height: status.mediaAttachments.count == 1 ? 200 : 100)
.cornerRadius(10)
.shadow(color: .mainTextColor.opacity(0.3), radius: 2)
} placeholder: {
Image(systemName: "photo")
.resizable()
.scaledToFit()
.frame(minWidth: 0, maxWidth: .infinity)
.frame(height: status.mediaAttachments.count == 1 ? 200 : 100)
.foregroundColor(.mainTextColor)
.opacity(0.05)
}
}
}
.padding(.bottom, 8)
}
2023-01-04 17:56:01 +01:00
}
2023-01-08 11:32:39 +01:00
.onTapGesture {
withAnimation(.linear(duration: 0.3)) {
if status.id == self.applicationState.showInteractionStatusId {
self.applicationState.showInteractionStatusId = ""
} else {
self.applicationState.showInteractionStatusId = status.id
}
2023-01-07 18:43:44 +01:00
}
}
}
2023-01-08 11:32:39 +01:00
.padding(8)
.background(self.getSelectedRowColor(status: status))
if self.applicationState.showInteractionStatusId == status.id {
VStack (alignment: .leading, spacing: 0) {
InteractionRow(statusId: status.id,
repliesCount: status.repliesCount,
reblogged: status.reblogged,
reblogsCount: status.reblogsCount,
favourited: status.favourited,
favouritesCount: status.favouritesCount,
bookmarked: status.bookmarked) {
2023-01-08 15:43:55 +01:00
self.onNewStatus?(status)
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))
}
CommentsSection(statusId: status.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 getUserName(status: Status) -> String {
return status.account?.displayName ?? status.account?.acct ?? status.account?.username ?? ""
}
private func getInteractionRowTextColor() -> Color {
return self.colorScheme == .dark ? Color.black : Color.white
}
private func getSelectedRowColor(status: Status) -> Color {
return self.applicationState.showInteractionStatusId == status.id ? Color.selectedRowColor : Color.systemBackground
}
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
}
}