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

410 lines
12 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
var contextMenu: some View {
StatusRowContextMenu(viewModel: viewModel)
}
2023-01-27 19:36:40 +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 {
VStack(alignment: .leading) {
if !viewModel.isCompact, theme.avatarPosition == .leading {
reblogView
replyView
}
HStack(alignment: .top, spacing: .statusColumnsSpacing) {
if !viewModel.isCompact,
theme.avatarPosition == .leading,
let status: AnyStatus = viewModel.status.reblog ?? viewModel.status
{
Button {
routerPath.navigate(to: .accountDetailWithAccount(account: status.account))
} label: {
AvatarView(url: status.account.avatar, size: .status)
}
2023-01-03 11:24:15 +00:00
}
VStack(alignment: .leading) {
if !viewModel.isCompact, theme.avatarPosition == .top {
reblogView
replyView
}
statusView
if viewModel.showActions && !viewModel.isRemote, theme.statusActionsDisplay != .none {
StatusActionsView(viewModel: viewModel)
.padding(.top, 8)
.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 {
2023-01-22 08:51:43 +00:00
if reasons.isEmpty {
viewModel.client = client
if !viewModel.isCompact, viewModel.embeddedStatus == nil {
Task {
await viewModel.loadEmbeddedStatus()
}
}
if preferences.autoExpandSpoilers == true && viewModel.displaySpoiler {
2023-01-22 08:51:43 +00:00
viewModel.displaySpoiler = false
2023-01-03 11:24:15 +00:00
}
}
2022-12-27 06:51:44 +00:00
}
2023-01-22 05:38:30 +00:00
.contextMenu {
contextMenu
2023-01-22 05:38:30 +00:00
}
.accessibilityElement(children: viewModel.isFocused ? .contain : .combine)
.accessibilityActions {
// Add the individual mentions as accessibility actions
ForEach(viewModel.status.mentions, id: \.id) { mention in
Button("@\(mention.username)") {
routerPath.navigate(to: .accountDetail(id: mention.id))
}
}
Button(viewModel.displaySpoiler ? "status.show-more" : "status.show-less") {
withAnimation {
viewModel.displaySpoiler.toggle()
}
}
Button("@\(viewModel.status.account.username)") {
routerPath.navigate(to: .accountDetail(id: viewModel.status.account.id))
}
contextMenu
2023-01-22 05:38:30 +00:00
}
.background {
Color.clear
.contentShape(Rectangle())
.onTapGesture {
viewModel.navigateToDetail(routerPath: routerPath)
}
}
.overlay {
if viewModel.isLoadingRemoteContent {
remoteContentLoadingView
}
}
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("status.filter.filtered-by-\(filter.title)")
2023-01-03 11:24:15 +00:00
Button {
withAnimation {
viewModel.isFiltered = false
}
} label: {
Text("status.filter.show-anyway")
2023-01-03 11:24:15 +00:00
}
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(.init(stringValue: viewModel.status.account.safeDisplayName), emojis: viewModel.status.account.emojis)
Text("status.row.was-boosted")
} else {
Text("status.row.you-boosted")
2023-01-22 05:38:30 +00:00
}
}
.accessibilityElement()
.accessibilityLabel(
Text("\(viewModel.status.account.safeDisplayName)")
+ Text(" ")
+ Text(viewModel.status.account.username != account.account?.username ? "status.row.was-boosted" : "status.row.you-boosted")
)
.font(.scaledFootnote)
2022-12-16 12:16:48 +00:00
.foregroundColor(.gray)
.fontWeight(.semibold)
2022-12-21 16:39:48 +00:00
.onTapGesture {
viewModel.navigateToAccountDetail(account: viewModel.status.account, routerPath: routerPath)
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")
Text("status.row.was-reply")
2022-12-28 09:45:05 +00:00
Text(mention.username)
}
.font(.scaledFootnote)
2022-12-28 09:45:05 +00:00
.foregroundColor(.gray)
.fontWeight(.semibold)
.onTapGesture {
viewModel.navigateToMention(mention: mention, routerPath: routerPath)
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 {
viewModel.navigateToAccountDetail(account: status.account, routerPath: routerPath)
} label: {
2022-12-27 12:38:10 +00:00
accountView(status: status)
}
.buttonStyle(.plain)
Spacer()
menuButton
2023-01-22 05:38:30 +00:00
.accessibilityHidden(true)
}
.accessibilityElement()
.accessibilityLabel(Text("\(status.account.displayName), \(status.createdAt.relativeFormatted)"))
2022-12-20 08:37:07 +00:00
}
makeStatusContentView(status: status)
.contentShape(Rectangle())
.onTapGesture {
2023-01-22 05:38:30 +00:00
viewModel.navigateToDetail(routerPath: routerPath)
}
}
}
.accessibilityElement(children: viewModel.isFocused ? .contain : .combine)
.accessibilityAction {
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 {
if !status.spoilerText.asRawText.isEmpty {
HStack(alignment: .top) {
Text("⚠︎")
2023-01-27 19:36:40 +00:00
.font(.system(.subheadline, weight: .bold))
.foregroundColor(.secondary)
EmojiTextApp(status.spoilerText, emojis: status.emojis, language: status.language)
2023-01-27 19:36:40 +00:00
.font(.system(.subheadline, weight: .bold))
.foregroundColor(.secondary)
.multilineTextAlignment(.leading)
Spacer()
Button {
withAnimation {
viewModel.displaySpoiler.toggle()
}
} label: {
Image(systemName: "chevron.down")
.rotationEffect(Angle(degrees: viewModel.displaySpoiler ? 0 : 180))
}
.buttonStyle(.bordered)
.accessibility(label: viewModel.displaySpoiler ? Text("status.show-more") : Text("status.show-less"))
.accessibilityHidden(true)
}
2023-01-27 19:36:40 +00:00
.onTapGesture { // make whole row tapable to make up for smaller button size
2022-12-28 10:41:56 +00:00
withAnimation {
viewModel.displaySpoiler.toggle()
2022-12-28 10:41:56 +00:00
}
}
}
2023-01-22 05:38:30 +00:00
if !viewModel.displaySpoiler {
HStack {
EmojiTextApp(status.content, emojis: status.emojis, language: status.language)
.font(.scaledBody)
.environment(\.openURL, OpenURLAction { url in
routerPath.handleStatus(status: status, url: url)
})
Spacer()
}
makeTranslateView(status: status)
2023-01-17 10:36:01 +00:00
2022-12-28 10:41:56 +00:00
if let poll = status.poll {
StatusPollView(poll: poll, status: status)
2022-12-28 10:41:56 +00:00
}
2023-01-22 08:51:43 +00:00
embedStatusView
2023-01-21 08:58:38 +00:00
makeMediasView(status: status)
2023-01-22 05:38:30 +00:00
.accessibilityHidden(!viewModel.isFocused)
2023-01-21 08:58:38 +00:00
makeCardView(status: status)
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(.init(stringValue: status.account.safeDisplayName), emojis: status.account.emojis)
.font(.scaledSubheadline)
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.relativeFormatted) +
2023-01-17 10:36:01 +00:00
Text("") +
Text(Image(systemName: viewModel.status.visibility.iconName))
}
.font(.scaledFootnote)
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 {
contextMenu
} label: {
Image(systemName: "ellipsis")
.frame(width: 30, height: 30)
}
.foregroundColor(.gray)
.contentShape(Rectangle())
}
2023-01-22 05:38:30 +00:00
2023-01-21 08:58:38 +00:00
@ViewBuilder
private func makeTranslateView(status: AnyStatus) -> some View {
if let userLang = preferences.serverPreferences?.postLanguage,
preferences.showTranslateButton,
2023-01-21 08:58:38 +00:00
status.language != nil,
userLang != status.language,
!status.content.asRawText.isEmpty,
2023-01-22 05:38:30 +00:00
viewModel.translation == nil
{
2023-01-21 08:58:38 +00:00
Button {
Task {
await viewModel.translate(userLang: userLang)
}
} label: {
if viewModel.isLoadingTranslation {
ProgressView()
} else {
if let statusLanguage = status.language,
2023-01-30 06:27:06 +00:00
let lanugageName = Locale.current.localizedString(forLanguageCode: statusLanguage)
{
Text("status.action.translate-from-\(lanugageName)")
} else {
Text("status.action.translate")
}
2023-01-21 08:58:38 +00:00
}
}
.buttonStyle(.borderless)
}
2023-01-22 09:24:19 +00:00
if let translation = viewModel.translation, !viewModel.isLoadingTranslation {
2023-01-21 08:58:38 +00:00
GroupBox {
VStack(alignment: .leading, spacing: 4) {
Text(translation)
.font(.scaledBody)
Text("status.action.translated-label")
.font(.footnote)
.foregroundColor(.gray)
}
}
.fixedSize(horizontal: false, vertical: true)
}
}
2023-01-22 05:38:30 +00:00
2023-01-21 08:58:38 +00:00
@ViewBuilder
private func makeMediasView(status: AnyStatus) -> some View {
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)
.padding(.vertical, 4)
}
}
}
2023-01-22 05:38:30 +00:00
2023-01-21 08:58:38 +00:00
@ViewBuilder
private func makeCardView(status: AnyStatus) -> some View {
if let card = status.card,
!viewModel.isEmbedLoading,
!viewModel.isCompact,
theme.statusDisplayStyle == .large,
status.content.statusesURLs.isEmpty,
2023-01-27 19:36:40 +00:00
status.mediaAttachments.isEmpty
{
2023-01-21 08:58:38 +00:00
StatusCardView(card: card)
}
}
2023-01-22 08:51:43 +00:00
@ViewBuilder
private var embedStatusView: some View {
if !reasons.contains(.placeholder) {
if !viewModel.isCompact, !viewModel.isEmbedLoading,
let embed = viewModel.embeddedStatus
{
2023-01-22 08:51:43 +00:00
StatusEmbeddedView(status: embed)
2023-01-27 12:38:24 +00:00
.fixedSize(horizontal: false, vertical: true)
2023-01-22 08:51:43 +00:00
} else if viewModel.isEmbedLoading, !viewModel.isCompact {
StatusEmbeddedView(status: .placeholder())
.redacted(reason: .placeholder)
.shimmering()
}
}
}
2023-01-30 06:27:06 +00:00
private var remoteContentLoadingView: some View {
ZStack(alignment: .center) {
VStack {
Spacer()
HStack {
Spacer()
ProgressView()
Spacer()
}
Spacer()
}
}
.background(Color.black.opacity(0.40))
.transition(.opacity)
}
2022-11-21 08:31:32 +00:00
}