IceCubesApp/Packages/Status/Sources/Status/Row/StatusRowView.swift

122 lines
3.4 KiB
Swift
Raw Normal View History

2022-11-21 08:31:32 +00:00
import SwiftUI
2022-11-29 08:28:17 +00:00
import Models
2022-12-22 09:53:36 +00:00
import Env
2022-12-19 11:28:55 +00:00
import DesignSystem
2022-12-19 14:51:25 +00:00
import Network
2022-11-21 08:31:32 +00:00
2022-12-18 19:30:19 +00:00
public struct StatusRowView: View {
2022-12-17 12:37:46 +00:00
@Environment(\.redactionReasons) private var reasons
2022-12-19 14:51:25 +00:00
@EnvironmentObject private var client: Client
2022-11-29 10:46:02 +00:00
@EnvironmentObject private var routeurPath: RouterPath
2022-12-20 19:33:45 +00:00
@StateObject var viewModel: StatusRowViewModel
2022-11-29 10:46:02 +00:00
2022-12-20 19:33:45 +00:00
public init(viewModel: StatusRowViewModel) {
_viewModel = StateObject(wrappedValue: viewModel)
2022-12-18 19:30:19 +00:00
}
2022-12-20 19:33:45 +00:00
2022-12-18 19:30:19 +00:00
public var body: some View {
2022-11-21 08:31:32 +00:00
VStack(alignment: .leading) {
2022-12-24 09:14:47 +00:00
if !viewModel.isEmbed {
reblogView
replyView
}
2022-12-16 12:16:48 +00:00
statusView
2022-12-23 16:50:51 +00:00
if !viewModel.isEmbed {
StatusActionsView(viewModel: viewModel)
.padding(.vertical, 8)
2022-12-24 12:41:25 +00:00
.tint(viewModel.isFocused ? .brand : .gray)
2022-12-23 16:50:51 +00:00
}
2022-12-16 12:16:48 +00:00
}
2022-12-20 19:33:45 +00:00
.onAppear {
viewModel.client = client
}
2022-12-16 12:16:48 +00:00
}
@ViewBuilder
private var reblogView: some View {
2022-12-20 19:33:45 +00:00
if viewModel.status.reblog != nil {
2022-12-16 12:16:48 +00:00
HStack(spacing: 2) {
2022-12-24 06:32:20 +00:00
Image(systemName:"arrow.left.arrow.right.circle.fill")
2022-12-21 16:39:48 +00:00
viewModel.status.account.displayNameWithEmojis
Text("boosted")
2022-11-29 10:46:02 +00:00
}
2022-12-16 12:16:48 +00:00
.font(.footnote)
.foregroundColor(.gray)
.fontWeight(.semibold)
2022-12-21 16:39:48 +00:00
.onTapGesture {
routeurPath.navigate(to: .accountDetailWithAccount(account: viewModel.status.account))
}
2022-11-29 10:46:02 +00:00
}
}
2022-12-24 06:32:20 +00:00
@ViewBuilder
var replyView: some View {
if let accountId = viewModel.status.inReplyToAccountId,
let mention = viewModel.status.mentions.first(where: { $0.id == accountId}) {
HStack(spacing: 2) {
Image(systemName:"arrowshape.turn.up.left.fill")
Text("Replied to")
Text(mention.username)
}
.font(.footnote)
.foregroundColor(.gray)
.fontWeight(.semibold)
.onTapGesture {
routeurPath.navigate(to: .accountDetail(id: mention.id))
}
}
}
2022-12-16 12:16:48 +00:00
private var statusView: some View {
2022-12-20 08:37:07 +00:00
VStack(alignment: .leading, spacing: 8) {
2022-12-20 19:33:45 +00:00
if let status: AnyStatus = viewModel.status.reblog ?? viewModel.status {
if !viewModel.isEmbed {
2022-12-20 08:37:07 +00:00
Button {
routeurPath.navigate(to: .accountDetailWithAccount(account: status.account))
} label: {
makeAccountView(status: status)
}.buttonStyle(.plain)
}
Text(status.content.asSafeAttributedString)
.font(.body)
.onTapGesture {
routeurPath.navigate(to: .statusDetail(id: status.id))
}
.environment(\.openURL, OpenURLAction { url in
routeurPath.handleStatus(status: status, url: url)
})
if !status.mediaAttachments.isEmpty {
2022-12-23 16:50:51 +00:00
if viewModel.isEmbed {
Image(systemName: "paperclip")
} else {
StatusMediaPreviewView(attachements: status.mediaAttachments)
.padding(.vertical, 4)
}
2022-12-16 12:16:48 +00:00
}
2022-12-23 16:50:51 +00:00
if let card = status.card, !viewModel.isEmbed {
StatusCardView(card: card)
}
2022-12-17 12:37:46 +00:00
}
2022-12-16 12:16:48 +00:00
}
}
@ViewBuilder
private func makeAccountView(status: AnyStatus) -> some View {
AvatarView(url: status.account.avatar, size: .status)
2022-12-21 16:39:48 +00:00
VStack(alignment: .leading, spacing: 0) {
status.account.displayNameWithEmojis
2022-12-20 08:37:07 +00:00
.font(.subheadline)
.fontWeight(.semibold)
Group {
Text("@\(status.account.acct)") +
Text("") +
2022-12-17 12:37:46 +00:00
Text(status.createdAt.formatted)
2022-12-16 12:16:48 +00:00
}
2022-12-20 08:37:07 +00:00
.font(.footnote)
.foregroundColor(.gray)
2022-11-21 08:31:32 +00:00
}
}
}