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

244 lines
7.2 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-12-30 18:31:17 +00:00
import Shimmer
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 {
HStack(alignment: .top, spacing: .statusColumnsSpacing) {
if !viewModel.isCompact,
theme.avatarPosition == .leading,
let status: AnyStatus = viewModel.status.reblog ?? viewModel.status {
Button {
routeurPath.navigate(to: .accountDetailWithAccount(account: status.account))
} label: {
AvatarView(url: status.account.avatar, size: .status)
}
2022-12-24 09:14:47 +00:00
}
VStack(alignment: .leading) {
if !viewModel.isCompact {
reblogView
replyView
}
statusView
if !viewModel.isCompact {
StatusActionsView(viewModel: viewModel)
.padding(.vertical, 8)
.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-30 18:31:17 +00:00
if !viewModel.isCompact, viewModel.embededStatus == nil {
2022-12-27 06:51:44 +00:00
Task {
await viewModel.loadEmbededStatus()
}
}
2022-12-20 19:33:45 +00:00
}
2022-12-27 08:11:12 +00:00
.contextMenu {
contextMenu
}
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-29 06:02:10 +00:00
AvatarView(url: viewModel.status.account.avatar, size: .boost)
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}) {
2022-12-28 09:45:05 +00:00
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-24 06:32:20 +00:00
}
}
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 {
2022-12-29 16:22:07 +00:00
if !viewModel.isCompact {
HStack(alignment: .top) {
Button {
routeurPath.navigate(to: .accountDetailWithAccount(account: status.account))
} label: {
2022-12-27 12:38:10 +00:00
accountView(status: status)
}.buttonStyle(.plain)
Spacer()
menuButton
}
2022-12-20 08:37:07 +00:00
}
2022-12-28 10:41:56 +00:00
makeStatusContentView(status: status) }
2022-12-27 06:51:44 +00:00
}
}
private func makeStatusContentView(status: AnyStatus) -> some View {
Group {
2022-12-28 10:41:56 +00:00
if !viewModel.status.spoilerText.isEmpty {
Text(status.spoilerText)
.font(.body)
}
if viewModel.displaySpoiler {
Button {
withAnimation {
viewModel.displaySpoiler = false
}
} label: {
Text("Show more")
}
.buttonStyle(.bordered)
} else {
2022-12-28 09:45:05 +00:00
Text(status.content.asSafeAttributedString)
.font(.body)
.environment(\.openURL, OpenURLAction { url in
routeurPath.handleStatus(status: status, url: url)
})
2022-12-28 10:41:56 +00:00
2022-12-30 18:31:17 +00:00
if !reasons.contains(.placeholder) {
if !viewModel.isCompact, !viewModel.isEmbedLoading, let embed = viewModel.embededStatus {
StatusEmbededView(status: embed)
} else if viewModel.isEmbedLoading, !viewModel.isCompact {
StatusEmbededView(status: .placeholder())
.redacted(reason: .placeholder)
.shimmering()
}
2022-12-28 10:41:56 +00:00
}
if let poll = status.poll {
StatusPollView(poll: poll)
}
if !status.mediaAttachments.isEmpty {
2022-12-29 16:22:07 +00:00
StatusMediaPreviewView(attachements: status.mediaAttachments, isCompact: viewModel.isCompact)
.padding(.vertical, 4)
2022-12-28 10:41:56 +00:00
}
2022-12-30 18:31:17 +00:00
if let card = status.card, viewModel.embededStatus?.url != status.card?.url, !viewModel.isEmbedLoading {
2022-12-28 10:41:56 +00:00
StatusCardView(card: card)
2022-12-16 12:16:48 +00:00
}
2022-12-27 06:51:44 +00:00
}
}
.contentShape(Rectangle())
.onTapGesture {
routeurPath.navigate(to: .statusDetail(id: viewModel.status.reblog?.id ?? viewModel.status.id))
}
}
@ViewBuilder
2022-12-27 12:38:10 +00:00
private func accountView(status: AnyStatus) -> some View {
2022-12-27 06:51:44 +00:00
HStack(alignment: .center) {
if theme.avatarPosition == .top {
AvatarView(url: status.account.avatar, size: .status)
}
2022-12-27 06:51:44 +00:00
VStack(alignment: .leading, spacing: 0) {
status.account.displayNameWithEmojis
2022-12-27 12:38:10 +00:00
.font(.headline)
2022-12-27 06:51:44 +00:00
.fontWeight(.semibold)
Group {
Text("@\(status.account.acct)") +
Text("") +
2022-12-27 13:20:00 +00:00
Text(status.createdAt.formatted) +
Text("") +
Text(Image(systemName: viewModel.status.visibility.iconName))
}
2022-12-27 12:38:10 +00:00
.font(.footnote)
2022-12-27 06:51:44 +00:00
.foregroundColor(.gray)
2022-12-17 12:37:46 +00:00
}
2022-12-16 12:16:48 +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 {
2022-12-27 12:38:10 +00:00
routeurPath.presentedSheet = .quoteStatusEditor(status: viewModel.status)
2022-12-27 06:51:44 +00:00
} label: {
2022-12-27 12:38:10 +00:00
Label("Quote this post", systemImage: "quote.bubble")
2022-12-27 06:51:44 +00:00
}
2022-12-28 09:45:05 +00:00
if let url = viewModel.status.reblog?.url ?? viewModel.status.url {
Button { UIApplication.shared.open(url) } label: {
Label("View in Browser", systemImage: "safari")
}
}
2022-12-28 09:45:05 +00:00
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
}