2023-03-14 17:44:24 +01:00
|
|
|
//
|
|
|
|
// https://mczachurski.dev
|
|
|
|
// Copyright © 2023 Marcin Czachurski and the repository contributors.
|
2023-03-28 10:35:38 +02:00
|
|
|
// Licensed under the Apache License 2.0.
|
2023-03-14 17:44:24 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
public extension View {
|
|
|
|
func imageContextMenu(client: Client, statusModel: StatusModel) -> some View {
|
|
|
|
modifier(ImageContextMenu(client: client, id: statusModel.id, url: statusModel.url))
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-03-14 17:44:24 +01:00
|
|
|
func imageContextMenu(client: Client, statusData: StatusData) -> some View {
|
|
|
|
modifier(ImageContextMenu(client: client, id: statusData.id, url: statusData.url))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private struct ImageContextMenu: ViewModifier {
|
|
|
|
private let client: Client
|
|
|
|
private let id: String
|
|
|
|
private let url: URL?
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-03-14 17:44:24 +01:00
|
|
|
init(client: Client, id: String, url: URL?) {
|
|
|
|
self.client = client
|
|
|
|
self.id = id
|
|
|
|
self.url = url
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-03-14 17:44:24 +01:00
|
|
|
func body(content: Content) -> some View {
|
|
|
|
ZStack(alignment: .trailing) {
|
|
|
|
content
|
2023-04-01 12:10:59 +02:00
|
|
|
.contextMenu {
|
2023-03-14 17:44:24 +01:00
|
|
|
Button {
|
|
|
|
Task {
|
|
|
|
await self.reboost()
|
|
|
|
}
|
|
|
|
} label: {
|
2023-03-19 18:50:37 +01:00
|
|
|
Label("status.title.reboost", image: "custom.rocket")
|
2023-03-14 17:44:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Button {
|
|
|
|
Task {
|
|
|
|
await self.favourite()
|
|
|
|
}
|
|
|
|
} label: {
|
|
|
|
Label("status.title.favourite", systemImage: "hand.thumbsup")
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-03-14 17:44:24 +01:00
|
|
|
Button {
|
|
|
|
Task {
|
|
|
|
await self.bookmark()
|
|
|
|
}
|
|
|
|
} label: {
|
|
|
|
Label("status.title.bookmark", systemImage: "bookmark")
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-03-14 17:44:24 +01:00
|
|
|
Divider()
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-03-14 17:44:24 +01:00
|
|
|
if let url = self.url {
|
|
|
|
Link(destination: url) {
|
|
|
|
Label("status.title.openInBrowser", systemImage: "safari")
|
|
|
|
}
|
|
|
|
|
|
|
|
ShareLink(item: url) {
|
|
|
|
Label("status.title.shareStatus", systemImage: "square.and.arrow.up")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-03-14 17:44:24 +01:00
|
|
|
private func reboost() async {
|
|
|
|
do {
|
|
|
|
_ = try await self.client.statuses?.boost(statusId: self.id)
|
2023-03-19 18:50:37 +01:00
|
|
|
ToastrService.shared.showSuccess(NSLocalizedString("status.title.reboosted", comment: "Reboosted"), imageName: "custom.rocket.fill")
|
2023-03-14 17:44:24 +01:00
|
|
|
} catch {
|
|
|
|
ErrorService.shared.handle(error, message: "status.error.reboostFailed", showToastr: true)
|
|
|
|
}
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-03-14 17:44:24 +01:00
|
|
|
private func favourite() async {
|
|
|
|
do {
|
|
|
|
_ = try await self.client.statuses?.favourite(statusId: self.id)
|
|
|
|
ToastrService.shared.showSuccess(NSLocalizedString("status.title.favourited", comment: "Favourited"), imageSystemName: "hand.thumbsup.fill")
|
|
|
|
} catch {
|
|
|
|
ErrorService.shared.handle(error, message: "status.error.favouriteFailed", showToastr: true)
|
|
|
|
}
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-03-14 17:44:24 +01:00
|
|
|
private func bookmark() async {
|
|
|
|
do {
|
|
|
|
_ = try await self.client.statuses?.bookmark(statusId: self.id)
|
|
|
|
ToastrService.shared.showSuccess(NSLocalizedString("status.title.bookmarked", comment: "Bookmarked"), imageSystemName: "bookmark.fill")
|
|
|
|
} catch {
|
|
|
|
ErrorService.shared.handle(error, message: "status.error.bookmarkFailed", showToastr: true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|