IceCubesApp/Packages/StatusKit/Sources/StatusKit/Row/Subviews/StatusRowContextMenu.swift

345 lines
13 KiB
Swift
Raw Normal View History

2023-02-21 06:23:42 +00:00
import DesignSystem
2023-01-17 10:36:01 +00:00
import Env
2023-01-04 17:37:58 +00:00
import Foundation
2023-02-19 14:29:07 +00:00
import Network
2023-02-21 06:23:42 +00:00
import SwiftUI
2023-01-04 17:37:58 +00:00
2023-09-18 19:03:52 +00:00
@MainActor
2023-01-04 17:37:58 +00:00
struct StatusRowContextMenu: View {
2023-02-19 14:29:07 +00:00
@Environment(\.displayScale) var displayScale
2023-10-26 04:23:00 +00:00
@Environment(\.openWindow) var openWindow
2023-02-21 06:23:42 +00:00
@Environment(Client.self) private var client
@Environment(SceneDelegate.self) private var sceneDelegate
2023-09-19 07:18:20 +00:00
@Environment(UserPreferences.self) private var preferences
@Environment(CurrentAccount.self) private var account
@Environment(CurrentInstance.self) private var currentInstance
@Environment(StatusDataController.self) private var statusDataController
2023-10-26 04:23:00 +00:00
@Environment(QuickLook.self) private var quickLook
2023-12-15 19:13:50 +00:00
@Environment(Theme.self) private var theme
2023-01-17 10:36:01 +00:00
var viewModel: StatusRowViewModel
@Binding var showTextForSelection: Bool
2024-01-31 06:56:50 +00:00
@Binding var isBlockConfirmationPresented: Bool
2023-02-21 06:23:42 +00:00
var boostLabel: some View {
2023-09-16 12:15:03 +00:00
if viewModel.status.visibility == .priv, viewModel.status.account.id == account.account?.id {
if statusDataController.isReblogged {
return Label("status.action.unboost", systemImage: "lock.rotation")
}
return Label("status.action.boost-to-followers", systemImage: "lock.rotation")
}
2023-02-21 06:23:42 +00:00
2023-09-16 12:15:03 +00:00
if statusDataController.isReblogged {
return Label("status.action.unboost", image: "Rocket.fill")
}
return Label("status.action.boost", image: "Rocket")
}
2023-02-21 06:23:42 +00:00
2023-01-04 17:37:58 +00:00
var body: some View {
if !viewModel.isRemote {
2024-01-22 08:14:45 +00:00
ControlGroup {
Button {
#if targetEnvironment(macCatalyst) || os(visionOS)
openWindow(value: WindowDestinationEditor.replyToStatusEditor(status: viewModel.status))
#else
viewModel.routerPath.presentedSheet = .replyToStatusEditor(status: viewModel.status)
#endif
} label: {
Label("status.action.reply", systemImage: "arrowshape.turn.up.left")
}
Button { Task {
HapticManager.shared.fireHaptic(.notification(.success))
SoundEffectManager.shared.playSound(.favorite)
await statusDataController.toggleFavorite(remoteStatus: nil)
} } label: {
Label(statusDataController.isFavorited ? "status.action.unfavorite" : "status.action.favorite", systemImage: statusDataController.isFavorited ? "star.fill" : "star")
}
Button { Task {
HapticManager.shared.fireHaptic(.notification(.success))
SoundEffectManager.shared.playSound(.boost)
await statusDataController.toggleReblog(remoteStatus: nil)
} } label: {
boostLabel
}
.disabled(viewModel.status.visibility == .direct || viewModel.status.visibility == .priv && viewModel.status.account.id != account.account?.id)
Button { Task {
SoundEffectManager.shared.playSound(.bookmark)
HapticManager.shared.fireHaptic(.notification(.success))
await statusDataController.toggleBookmark(remoteStatus: nil)
} } label: {
Label(statusDataController.isBookmarked ? "status.action.unbookmark" : "status.action.bookmark",
systemImage: statusDataController.isBookmarked ? "bookmark.fill" : "bookmark")
}
}
2024-01-22 08:14:45 +00:00
.controlGroupStyle(.compactMenu)
2023-01-04 17:37:58 +00:00
Button {
2024-01-09 12:28:57 +00:00
#if targetEnvironment(macCatalyst) || os(visionOS)
2023-12-18 07:22:59 +00:00
openWindow(value: WindowDestinationEditor.quoteStatusEditor(status: viewModel.status))
#else
viewModel.routerPath.presentedSheet = .quoteStatusEditor(status: viewModel.status)
#endif
2023-01-04 17:37:58 +00:00
} label: {
Label("status.action.quote", systemImage: "quote.bubble")
2023-01-04 17:37:58 +00:00
}
.disabled(viewModel.status.visibility == .direct || viewModel.status.visibility == .priv)
2023-01-04 17:37:58 +00:00
}
2023-01-17 10:36:01 +00:00
2023-01-07 17:01:06 +00:00
Divider()
2023-01-17 10:36:01 +00:00
2023-02-19 14:29:07 +00:00
Menu("status.action.share-title") {
if let urlString = viewModel.status.reblog?.url ?? viewModel.status.url,
let url = URL(string: urlString)
{
ShareLink(item: url,
subject: Text(viewModel.status.reblog?.account.safeDisplayName ?? viewModel.status.account.safeDisplayName),
2023-03-13 12:38:28 +00:00
message: Text(viewModel.status.reblog?.content.asRawText ?? viewModel.status.content.asRawText))
{
2023-02-19 14:29:07 +00:00
Label("status.action.share", systemImage: "square.and.arrow.up")
}
2023-02-21 06:23:42 +00:00
2023-02-19 14:29:07 +00:00
ShareLink(item: url) {
Label("status.action.share-link", systemImage: "link")
}
2023-02-21 06:23:42 +00:00
2023-02-19 14:29:07 +00:00
Button {
let view = HStack {
StatusRowView(viewModel: viewModel)
2023-02-19 14:29:07 +00:00
.padding(16)
}
.environment(\.isInCaptureMode, true)
2023-09-18 19:03:52 +00:00
.environment(Theme.shared)
2023-09-19 07:18:20 +00:00
.environment(preferences)
.environment(account)
.environment(currentInstance)
.environment(SceneDelegate())
2023-10-26 04:23:00 +00:00
.environment(quickLook)
.environment(viewModel.client)
2024-01-04 13:08:24 +00:00
.environment(RouterPath())
2023-02-19 14:29:07 +00:00
.preferredColorScheme(Theme.shared.selectedScheme == .dark ? .dark : .light)
2023-02-19 15:03:27 +00:00
.foregroundColor(Theme.shared.labelColor)
2023-02-19 14:29:07 +00:00
.background(Theme.shared.primaryBackgroundColor)
2023-02-19 15:03:27 +00:00
.frame(width: sceneDelegate.windowWidth - 12)
.tint(Theme.shared.tintColor)
2023-02-19 14:29:07 +00:00
let renderer = ImageRenderer(content: view)
renderer.scale = displayScale
2023-02-19 15:03:27 +00:00
renderer.isOpaque = false
2023-02-19 14:29:07 +00:00
if let image = renderer.uiImage {
viewModel.routerPath.presentedSheet = .shareImage(image: image, status: viewModel.status)
}
} label: {
Label("status.action.share-image", systemImage: "photo")
}
2023-01-17 10:36:01 +00:00
}
}
2023-01-17 10:36:01 +00:00
if let url = URL(string: viewModel.status.reblog?.url ?? viewModel.status.url ?? "") {
2023-02-01 06:09:41 +00:00
Button { UIApplication.shared.open(url) } label: {
Label("status.action.view-in-browser", systemImage: "safari")
2023-01-04 17:37:58 +00:00
}
}
2023-01-17 10:36:01 +00:00
Button {
2023-02-06 17:41:12 +00:00
UIPasteboard.general.string = viewModel.status.reblog?.content.asRawText ?? viewModel.status.content.asRawText
} label: {
Label("status.action.copy-text", systemImage: "doc.on.doc")
}
Button {
showTextForSelection = true
} label: {
Label("status.action.select-text", systemImage: "selection.pin.in.out")
}
Button {
UIPasteboard.general.string = viewModel.status.reblog?.url ?? viewModel.status.url
} label: {
Label("status.action.copy-link", systemImage: "link")
}
if let lang = preferences.serverPreferences?.postLanguage ?? Locale.current.language.languageCode?.identifier {
Button {
Task {
await viewModel.translate(userLang: lang)
}
} label: {
2023-02-22 18:09:39 +00:00
Label("status.action.translate", systemImage: "captions.bubble")
}
}
2023-01-04 17:37:58 +00:00
if account.account?.id == viewModel.status.reblog?.account.id ?? viewModel.status.account.id {
Section("status.action.section.your-post") {
2023-01-04 17:37:58 +00:00
Button {
Task {
if viewModel.isPinned {
await viewModel.unPin()
} else {
await viewModel.pin()
}
}
} label: {
Label(viewModel.isPinned ? "status.action.unpin" : "status.action.pin", systemImage: viewModel.isPinned ? "pin.fill" : "pin")
2023-01-04 17:37:58 +00:00
}
if currentInstance.isEditSupported {
Button {
2024-01-09 12:28:57 +00:00
#if targetEnvironment(macCatalyst) || os(visionOS)
2023-12-18 07:22:59 +00:00
openWindow(value: WindowDestinationEditor.editStatusEditor(status: viewModel.status.reblogAsAsStatus ?? viewModel.status))
#else
viewModel.routerPath.presentedSheet = .editStatusEditor(status: viewModel.status.reblogAsAsStatus ?? viewModel.status)
#endif
} label: {
Label("status.action.edit", systemImage: "pencil")
}
2023-01-04 17:37:58 +00:00
}
Button(role: .destructive,
action: { viewModel.showDeleteAlert = true },
2023-02-12 15:29:41 +00:00
label: { Label("status.action.delete", systemImage: "trash") })
2023-01-04 17:37:58 +00:00
}
} else {
if !viewModel.isRemote {
Section(viewModel.status.reblog?.account.acct ?? viewModel.status.account.acct) {
if viewModel.authorRelationship?.muting == true {
Button {
Task {
do {
let operationAccount = viewModel.status.reblog?.account ?? viewModel.status.account
viewModel.authorRelationship = try await client.post(endpoint: Accounts.unmute(id: operationAccount.id))
2024-02-14 11:48:14 +00:00
} catch {}
}
} label: {
Label("account.action.unmute", systemImage: "speaker")
}
} else {
Menu {
ForEach(Duration.mutingDurations(), id: \.rawValue) { duration in
Button(duration.description) {
Task {
do {
let operationAccount = viewModel.status.reblog?.account ?? viewModel.status.account
viewModel.authorRelationship = try await client.post(endpoint: Accounts.mute(id: operationAccount.id, json: MuteData(duration: duration.rawValue)))
2024-02-14 11:48:14 +00:00
} catch {}
}
}
}
} label: {
Label("account.action.mute", systemImage: "speaker.slash")
}
2024-01-22 08:14:45 +00:00
}
2024-02-14 11:48:14 +00:00
2024-01-23 05:55:12 +00:00
#if targetEnvironment(macCatalyst)
accountContactMenuItems
#else
2024-02-14 11:48:14 +00:00
ControlGroup {
accountContactMenuItems
}
2024-01-23 05:55:12 +00:00
#endif
2023-01-04 17:37:58 +00:00
}
}
2023-02-13 20:12:18 +00:00
Section {
Button(role: .destructive) {
viewModel.routerPath.presentedSheet = .report(status: viewModel.status.reblogAsAsStatus ?? viewModel.status)
2023-02-13 20:12:18 +00:00
} label: {
Label("status.action.report", systemImage: "exclamationmark.bubble")
}
}
2023-01-04 17:37:58 +00:00
}
}
2024-02-14 11:48:14 +00:00
2024-01-23 05:55:12 +00:00
@ViewBuilder
private var accountContactMenuItems: some View {
Button {
#if targetEnvironment(macCatalyst) || os(visionOS)
openWindow(value: WindowDestinationEditor.mentionStatusEditor(account: viewModel.status.reblog?.account ?? viewModel.status.account, visibility: .pub))
#else
viewModel.routerPath.presentedSheet = .mentionStatusEditor(account: viewModel.status.reblog?.account ?? viewModel.status.account, visibility: .pub)
#endif
} label: {
Label("status.action.mention", systemImage: "at")
}
Button {
#if targetEnvironment(macCatalyst) || os(visionOS)
openWindow(value: WindowDestinationEditor.mentionStatusEditor(account: viewModel.status.reblog?.account ?? viewModel.status.account, visibility: .direct))
#else
viewModel.routerPath.presentedSheet = .mentionStatusEditor(account: viewModel.status.reblog?.account ?? viewModel.status.account, visibility: .direct)
#endif
} label: {
Label("status.action.message", systemImage: "tray.full")
}
if viewModel.authorRelationship?.blocking == true {
Button {
Task {
do {
let operationAccount = viewModel.status.reblog?.account ?? viewModel.status.account
viewModel.authorRelationship = try await client.post(endpoint: Accounts.unblock(id: operationAccount.id))
2024-02-14 11:48:14 +00:00
} catch {}
2024-01-23 05:55:12 +00:00
}
} label: {
Label("account.action.unblock", systemImage: "person.crop.circle.badge.exclamationmark")
}
} else {
Button {
2024-01-31 06:56:50 +00:00
isBlockConfirmationPresented = true
2024-01-23 05:55:12 +00:00
} label: {
Label("account.action.block", systemImage: "person.crop.circle.badge.xmark")
}
}
}
2023-01-04 17:37:58 +00:00
}
2023-02-19 14:29:07 +00:00
struct ActivityView: UIViewControllerRepresentable {
let image: Image
2023-02-21 06:23:42 +00:00
func makeUIViewController(context _: UIViewControllerRepresentableContext<ActivityView>) -> UIActivityViewController {
2023-09-16 12:15:03 +00:00
UIActivityViewController(activityItems: [image], applicationActivities: nil)
2023-02-19 14:29:07 +00:00
}
2023-02-21 06:23:42 +00:00
func updateUIViewController(_: UIActivityViewController, context _: UIViewControllerRepresentableContext<ActivityView>) {}
2023-02-19 14:29:07 +00:00
}
struct SelectTextView: View {
@Environment(\.dismiss) private var dismiss
let content: AttributedString
var body: some View {
NavigationStack {
SelectableText(content: content)
.padding()
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button {
dismiss()
} label: {
Text("action.done").bold()
}
}
}
2023-12-15 19:13:50 +00:00
.background(Theme.shared.primaryBackgroundColor)
.navigationTitle("status.action.select-text")
.navigationBarTitleDisplayMode(.inline)
}
}
}
struct SelectableText: UIViewRepresentable {
let content: AttributedString
2023-12-18 07:22:59 +00:00
func makeUIView(context _: Context) -> UITextView {
let attributedText = NSMutableAttributedString(content)
attributedText.addAttribute(
.font,
value: Font.scaledBodyFont,
range: NSRange(location: 0, length: content.characters.count)
)
let textView = UITextView()
textView.isEditable = false
textView.attributedText = attributedText
textView.textColor = UIColor(Color.label)
2023-12-15 19:13:50 +00:00
textView.backgroundColor = UIColor(Theme.shared.primaryBackgroundColor)
return textView
}
2023-12-18 07:22:59 +00:00
func updateUIView(_: UITextView, context _: Context) {}
func makeCoordinator() {}
}