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

219 lines
6.3 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
@EnvironmentObject private var account: CurrentAccount
2022-12-24 14:09:17 +00:00
@EnvironmentObject private var theme: Theme
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 14:09:17 +00:00
.tint(viewModel.isFocused ? theme.tintColor : .gray)
.contentShape(Rectangle())
.onTapGesture {
routeurPath.navigate(to: .statusDetail(id: viewModel.status.reblog?.id ?? viewModel.status.id))
}
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-27 06:51:44 +00:00
if !viewModel.isEmbed {
Task {
await viewModel.loadEmbededStatus()
}
}
2022-12-20 19:33:45 +00:00
}
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 {
HStack(alignment: .top) {
Button {
routeurPath.navigate(to: .accountDetailWithAccount(account: status.account))
} label: {
makeAccountView(status: status)
}.buttonStyle(.plain)
Spacer()
menuButton
}
2022-12-20 08:37:07 +00:00
}
2022-12-27 06:51:44 +00:00
makeStatusContentView(status: status)
}
}
}
private func makeStatusContentView(status: AnyStatus) -> some View {
Group {
Text(status.content.asSafeAttributedString)
.font(.body)
.environment(\.openURL, OpenURLAction { url in
routeurPath.handleStatus(status: status, url: url)
})
embededStatusView
if !status.mediaAttachments.isEmpty {
if viewModel.isEmbed {
Image(systemName: "paperclip")
} else {
StatusMediaPreviewView(attachements: status.mediaAttachments)
.padding(.vertical, 4)
2022-12-16 12:16:48 +00:00
}
2022-12-27 06:51:44 +00:00
}
if let card = status.card, !viewModel.isEmbed {
StatusCardView(card: card)
}
}
.contentShape(Rectangle())
.onTapGesture {
routeurPath.navigate(to: .statusDetail(id: viewModel.status.reblog?.id ?? viewModel.status.id))
}
}
@ViewBuilder
private func makeAccountView(status: AnyStatus, size: AvatarView.Size = .status) -> some View {
HStack(alignment: .center) {
AvatarView(url: status.account.avatar, size: size)
VStack(alignment: .leading, spacing: 0) {
status.account.displayNameWithEmojis
.font(size == .embed ? .footnote : .headline)
.fontWeight(.semibold)
Group {
Text("@\(status.account.acct)") +
Text("") +
Text(status.createdAt.formatted)
}
2022-12-27 06:51:44 +00:00
.font(size == .embed ? .caption : .footnote)
.foregroundColor(.gray)
2022-12-17 12:37:46 +00:00
}
2022-12-16 12:16:48 +00:00
}
}
@ViewBuilder
2022-12-27 06:51:44 +00:00
private var embededStatusView: some View {
if let status = viewModel.embededStatus {
VStack(alignment: .leading) {
makeAccountView(status: status, size: .embed)
StatusRowView(viewModel: .init(status: status, isEmbed: true))
2022-12-16 12:16:48 +00:00
}
2022-12-27 06:51:44 +00:00
.padding(8)
.background(Color.gray.opacity(0.10))
.overlay(
RoundedRectangle(cornerRadius: 4)
.stroke(.gray.opacity(0.35), lineWidth: 1)
)
.padding(.top, 8)
2022-11-21 08:31:32 +00:00
}
}
private var menuButton: some View {
Menu {
contextMenu
} label: {
Image(systemName: "ellipsis")
.frame(width: 30, height: 30)
}
.foregroundColor(.gray)
.contentShape(Rectangle())
}
@ViewBuilder
private var contextMenu: some View {
Button { Task {
if viewModel.isFavourited {
await viewModel.unFavourite()
} else {
await viewModel.favourite()
}
} } label: {
Label(viewModel.isFavourited ? "Unfavorite" : "Favorite", systemImage: "star")
}
2022-12-27 06:51:44 +00:00
Button { Task {
if viewModel.isReblogged {
await viewModel.unReblog()
} else {
await viewModel.reblog()
}
} } label: {
Label(viewModel.isReblogged ? "Unboost" : "Boost", systemImage: "arrow.left.arrow.right.circle")
}
Button {
routeurPath.presentedSheet = .quoteStatusEditor(status: viewModel.status.reblog ?? viewModel.status)
} label: {
Label("Quote this status", systemImage: "quote.bubble")
}
if let url = viewModel.status.reblog?.url ?? viewModel.status.url {
Button { UIApplication.shared.open(url) } label: {
Label("View in Browser", systemImage: "safari")
}
}
if account.account?.id == viewModel.status.account.id {
2022-12-26 07:24:55 +00:00
Button {
routeurPath.presentedSheet = .editStatusEditor(status: viewModel.status)
} label: {
Label("Edit", systemImage: "pencil")
}
Button(role: .destructive) { Task { await viewModel.delete() } } label: {
Label("Delete", systemImage: "trash")
}
}
}
2022-11-21 08:31:32 +00:00
}