Bubble/Threaded/Components/Post/PostMenu.swift

109 lines
3.7 KiB
Swift
Raw Normal View History

2024-01-21 16:34:26 +01:00
//Made by Lumaa
import SwiftUI
2024-01-26 14:09:52 +01:00
import UniformTypeIdentifiers
2024-01-21 16:34:26 +01:00
struct PostMenu: View {
2024-02-15 23:22:14 +01:00
@Environment(UniversalNavigator.self) private var navigator
2024-01-26 14:09:52 +01:00
@Environment(AccountManager.self) private var accountManager
2024-01-28 17:51:50 +01:00
@Environment(\.dismiss) private var dismiss
2024-01-26 14:09:52 +01:00
@Environment(\.colorScheme) private var colorScheme
2024-01-21 16:34:26 +01:00
@Environment(\.displayScale) private var displayScale
var status: Status
private var pref: UserPreferences {
try! UserPreferences.loadAsCurrent() ?? .defaultPreferences
}
2024-01-21 16:34:26 +01:00
2024-01-26 14:09:52 +01:00
private var isOwner: Bool {
if let acc = accountManager.getAccount() {
2024-02-16 16:20:39 +01:00
return status.reblogAsAsStatus?.account.id ?? status.account.id == acc.id
2024-01-26 14:09:52 +01:00
}
return false
}
2024-01-21 16:34:26 +01:00
var body: some View {
Menu {
2024-01-26 14:09:52 +01:00
if isOwner {
Button(role: .destructive) {
Task {
await deleteStatus()
}
} label: {
Label("status.menu.delete", systemImage: "trash")
}
Button {
navigator.presentedSheet = .post(content: status.reblogAsAsStatus?.content.asRawText ?? status.content.asRawText, replyId: nil, editId: status.reblogAsAsStatus?.id ?? status.id)
} label: {
Label("status.menu.edit", systemImage: "pencil.and.scribble")
}
Divider()
2024-01-21 16:34:26 +01:00
}
Menu {
ShareLink(item: URL(string: status.url ?? "https://joinmastodon.org/")!) {
Label("status.menu.share-link", systemImage: "square.and.arrow.up")
}
Button {
Task {
2024-01-26 14:09:52 +01:00
createImage()
2024-01-21 16:34:26 +01:00
}
} label: {
Label("status.menu.share-image", systemImage: "photo")
}
2024-01-26 14:09:52 +01:00
Divider()
Button {
UIPasteboard.general.setValue(status.reblogAsAsStatus?.content.asRawText ?? status.content.asRawText, forPasteboardType: UTType.plainText.identifier)
} label: {
Label("status.menu.copy-text", systemImage: "list.clipboard")
}
2024-01-21 16:34:26 +01:00
} label: {
Label("status.menu.share", systemImage: "paperplane")
}
} label: {
Image(systemName: "ellipsis")
.foregroundStyle(Color.white.opacity(0.3))
.font(.body)
.contentShape(Rectangle())
.padding(7.5)
2024-01-21 16:34:26 +01:00
}
}
@MainActor
private func createImage() {
let view = HStack {
CompactPostView(status: status, imaging: true)
2024-01-21 16:34:26 +01:00
.padding(15)
.background(Color.appBackground)
}
.environment(\.colorScheme, colorScheme == .dark ? .dark : .light)
2024-02-15 23:22:14 +01:00
.environment(UniversalNavigator())
2024-01-21 16:34:26 +01:00
.environment(AccountManager())
.environment(AppDelegate())
.environment(pref)
.environmentObject(Navigator())
2024-01-21 16:34:26 +01:00
let render = ImageRenderer(content: view)
render.scale = displayScale
render.isOpaque = false
if let image = render.uiImage {
2024-01-26 14:09:52 +01:00
navigator.presentedSheet = .shareImage(image: image, status: status)
}
}
private func deleteStatus() async {
if let client = accountManager.getClient() {
_ = try? await client.delete(endpoint: Statuses.status(id: status.id))
2024-01-28 17:51:50 +01:00
if navigator.path.last == .post(status: status) {
dismiss()
}
2024-01-21 16:34:26 +01:00
}
}
}