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

278 lines
8.4 KiB
Swift
Raw Normal View History

2022-12-19 11:28:55 +00:00
import DesignSystem
2023-01-17 10:36:01 +00:00
import EmojiText
import Env
import Models
2022-12-19 14:51:25 +00:00
import Network
2022-12-30 18:31:17 +00:00
import Shimmer
2023-01-17 10:36:01 +00:00
import SwiftUI
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 preferences: UserPreferences
@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
@EnvironmentObject private var routerPath: RouterPath
2022-12-20 19:33:45 +00:00
@StateObject var viewModel: StatusRowViewModel
2023-01-17 10:36:01 +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
}
2023-01-17 10:36:01 +00:00
2022-12-18 19:30:19 +00:00
public var body: some View {
2023-01-03 11:24:15 +00:00
if viewModel.isFiltered, let filter = viewModel.filter {
switch filter.filter.filterAction {
case .warn:
makeFilterView(filter: filter.filter)
case .hide:
EmptyView()
2022-12-24 09:14:47 +00:00
}
2023-01-03 11:24:15 +00:00
} else {
HStack(alignment: .top, spacing: .statusColumnsSpacing) {
if !viewModel.isCompact,
theme.avatarPosition == .leading,
2023-01-17 10:36:01 +00:00
let status: AnyStatus = viewModel.status.reblog ?? viewModel.status
{
2023-01-03 11:24:15 +00:00
Button {
routerPath.navigate(to: .accountDetailWithAccount(account: status.account))
2023-01-03 11:24:15 +00:00
} label: {
AvatarView(url: status.account.avatar, size: .status)
}
}
2023-01-03 11:24:15 +00:00
VStack(alignment: .leading) {
if !viewModel.isCompact {
reblogView
2023-01-05 17:54:18 +00:00
replyView
2023-01-03 11:24:15 +00:00
}
statusView
2023-01-17 06:54:59 +00:00
if viewModel.showActions && !viewModel.isRemote, theme.statusActionsDisplay != .none {
2023-01-03 11:24:15 +00:00
StatusActionsView(viewModel: viewModel)
.padding(.top, 8)
2023-01-03 11:24:15 +00:00
.tint(viewModel.isFocused ? theme.tintColor : .gray)
.contentShape(Rectangle())
.onTapGesture {
viewModel.navigateToDetail(routerPath: routerPath)
2023-01-03 11:24:15 +00:00
}
}
}
2022-12-23 16:50:51 +00:00
}
2023-01-03 11:24:15 +00:00
.onAppear {
viewModel.client = client
if !viewModel.isCompact, viewModel.embeddedStatus == nil {
2023-01-03 11:24:15 +00:00
Task {
await viewModel.loadEmbeddedStatus()
2023-01-03 11:24:15 +00:00
}
2022-12-27 06:51:44 +00:00
}
if preferences.serverPreferences?.autoExpandSpoilers == true {
viewModel.displaySpoiler = false
}
2022-12-27 06:51:44 +00:00
}
2023-01-03 11:24:15 +00:00
.contextMenu {
2023-01-04 17:37:58 +00:00
StatusRowContextMenu(viewModel: viewModel)
2023-01-03 11:24:15 +00:00
}
.background {
Color.clear
.contentShape(Rectangle())
.onTapGesture {
viewModel.navigateToDetail(routerPath: routerPath)
}
}
2022-12-20 19:33:45 +00:00
}
2023-01-03 11:24:15 +00:00
}
2023-01-17 10:36:01 +00:00
2023-01-03 11:24:15 +00:00
private func makeFilterView(filter: Filter) -> some View {
HStack {
Text("Filtered by: \(filter.title)")
Button {
withAnimation {
viewModel.isFiltered = false
}
} label: {
Text("Show anyway")
}
2022-12-27 08:11:12 +00:00
}
2022-12-16 12:16:48 +00:00
}
2023-01-17 10:36:01 +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) {
2023-01-17 10:36:01 +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)
if viewModel.status.account.username != account.account?.username {
EmojiTextApp(viewModel.status.account.safeDisplayName.asMarkdown, emojis: viewModel.status.account.emojis)
Text("boosted")
} else {
Text("You 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 {
if viewModel.isRemote, let url = viewModel.status.account.url {
Task {
await routerPath.navigateToAccountFrom(url: url)
}
} else {
routerPath.navigate(to: .accountDetailWithAccount(account: viewModel.status.account))
}
2022-12-21 16:39:48 +00:00
}
2022-11-29 10:46:02 +00:00
}
}
2023-01-17 10:36:01 +00:00
2022-12-24 06:32:20 +00:00
@ViewBuilder
var replyView: some View {
if let accountId = viewModel.status.inReplyToAccountId,
2023-01-17 10:36:01 +00:00
let mention = viewModel.status.mentions.first(where: { $0.id == accountId })
{
2022-12-28 09:45:05 +00:00
HStack(spacing: 2) {
2023-01-17 10:36:01 +00:00
Image(systemName: "arrowshape.turn.up.left.fill")
2022-12-28 09:45:05 +00:00
Text("Replied to")
Text(mention.username)
}
.font(.footnote)
.foregroundColor(.gray)
.fontWeight(.semibold)
.onTapGesture {
if viewModel.isRemote {
Task {
await routerPath.navigateToAccountFrom(url: mention.url)
}
} else {
routerPath.navigate(to: .accountDetail(id: mention.id))
}
2022-12-28 09:45:05 +00:00
}
2022-12-24 06:32:20 +00:00
}
}
2023-01-17 10:36:01 +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 {
if viewModel.isRemote, let url = status.account.url {
Task {
await routerPath.navigateToAccountFrom(url: url)
}
} else {
routerPath.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
}
makeStatusContentView(status: status)
.contentShape(Rectangle())
.onTapGesture {
viewModel.navigateToDetail(routerPath: routerPath)
}
}
2022-12-27 06:51:44 +00:00
}
}
2023-01-17 10:36:01 +00:00
2022-12-27 06:51:44 +00:00
private func makeStatusContentView(status: AnyStatus) -> some View {
Group {
2023-01-09 20:13:00 +00:00
if !status.spoilerText.isEmpty {
EmojiTextApp(status.spoilerText.asMarkdown, emojis: status.emojis)
2022-12-28 10:41:56 +00:00
.font(.body)
Button {
withAnimation {
viewModel.displaySpoiler.toggle()
2022-12-28 10:41:56 +00:00
}
} label: {
2023-01-17 10:36:01 +00:00
Text(viewModel.displaySpoiler ? "Show more" : "Show less")
2022-12-28 10:41:56 +00:00
}
.buttonStyle(.bordered)
}
if !viewModel.displaySpoiler {
HStack {
EmojiTextApp(status.content.asMarkdown, emojis: status.emojis)
.font(.body)
.environment(\.openURL, OpenURLAction { url in
routerPath.handleStatus(status: status, url: url)
})
Spacer()
}
2023-01-17 10:36:01 +00:00
2022-12-30 18:31:17 +00:00
if !reasons.contains(.placeholder) {
if !viewModel.isCompact, !viewModel.isEmbedLoading, let embed = viewModel.embeddedStatus {
StatusEmbeddedView(status: embed)
2022-12-30 18:31:17 +00:00
} else if viewModel.isEmbedLoading, !viewModel.isCompact {
StatusEmbeddedView(status: .placeholder())
2022-12-30 18:31:17 +00:00
.redacted(reason: .placeholder)
.shimmering()
}
2022-12-28 10:41:56 +00:00
}
2023-01-17 10:36:01 +00:00
2022-12-28 10:41:56 +00:00
if let poll = status.poll {
StatusPollView(poll: poll)
}
2023-01-17 10:36:01 +00:00
2022-12-28 10:41:56 +00:00
if !status.mediaAttachments.isEmpty {
if theme.statusDisplayStyle == .compact {
HStack {
StatusMediaPreviewView(attachments: status.mediaAttachments,
sensitive: status.sensitive,
isNotifications: viewModel.isCompact)
Spacer()
}
.padding(.vertical, 4)
} else {
StatusMediaPreviewView(attachments: status.mediaAttachments,
sensitive: status.sensitive,
isNotifications: viewModel.isCompact)
2023-01-17 10:36:01 +00:00
.padding(.vertical, 4)
}
2022-12-28 10:41:56 +00:00
}
if let card = status.card,
viewModel.embeddedStatus?.url != status.card?.url,
status.mediaAttachments.isEmpty,
!viewModel.isEmbedLoading,
2023-01-17 10:36:01 +00:00
theme.statusDisplayStyle == .large
{
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
}
}
}
2023-01-17 10:36:01 +00:00
2022-12-27 06:51:44 +00:00
@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) {
EmojiTextApp(status.account.safeDisplayName.asMarkdown, emojis: status.account.emojis)
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)") +
2023-01-17 10:36:01 +00:00
Text("") +
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
}
}
2023-01-17 10:36:01 +00:00
private var menuButton: some View {
Menu {
2023-01-04 17:37:58 +00:00
StatusRowContextMenu(viewModel: viewModel)
} label: {
Image(systemName: "ellipsis")
.frame(width: 30, height: 30)
}
.foregroundColor(.gray)
.contentShape(Rectangle())
}
2022-11-21 08:31:32 +00:00
}