Impressia/Vernissage/Widgets/InteractionRow.swift

216 lines
7.8 KiB
Swift
Raw Normal View History

2023-01-03 14:09:22 +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-01-03 14:09:22 +01:00
//
import SwiftUI
2023-02-19 10:32:38 +01:00
import PixelfedKit
2023-01-15 12:41:55 +01:00
import Drops
2023-04-07 14:20:12 +02:00
import ClientKit
2023-04-07 14:38:50 +02:00
import ServicesKit
2023-04-07 16:59:18 +02:00
import EnvironmentKit
import WidgetsKit
2023-01-03 14:09:22 +01:00
struct InteractionRow: View {
2023-02-18 20:59:21 +01:00
typealias DeleteAction = () -> Void
2023-01-05 14:50:48 +01:00
@EnvironmentObject var applicationState: ApplicationState
2023-02-03 15:16:30 +01:00
@EnvironmentObject var client: Client
2023-01-23 18:01:27 +01:00
@EnvironmentObject var routerPath: RouterPath
@State var statusModel: StatusModel
2023-01-10 11:30:30 +01:00
@State private var repliesCount = 0
@State private var reblogged = false
@State private var reblogsCount = 0
@State private var favourited = false
@State private var favouritesCount = 0
@State private var bookmarked = false
2023-02-18 20:59:21 +01:00
private let delete: DeleteAction?
public init(statusModel: StatusModel, delete: DeleteAction? = nil) {
self.statusModel = statusModel
2023-02-18 20:59:21 +01:00
self.delete = delete
}
2023-01-03 14:09:22 +01:00
var body: some View {
HStack(alignment: .top) {
if self.statusModel.commentsDisabled == false {
ActionButton {
self.routerPath.presentedSheet = .replyToStatusEditor(status: statusModel)
} label: {
HStack(alignment: .center) {
Image(systemName: "message")
Text("\(repliesCount)")
.font(.caption)
}
2023-01-03 14:09:22 +01:00
}
Spacer()
2023-01-03 14:09:22 +01:00
}
2023-01-22 21:44:07 +01:00
ActionButton {
2023-02-18 20:59:21 +01:00
await self.reboost()
2023-01-22 17:41:11 +01:00
} label: {
HStack(alignment: .center) {
2023-03-19 18:50:37 +01:00
Image(self.reblogged ? "custom.rocket.fill" : "custom.rocket")
2023-01-22 21:44:07 +01:00
Text("\(self.reblogsCount)")
2023-01-22 17:41:11 +01:00
.font(.caption)
}
2023-01-22 21:44:07 +01:00
}
2023-01-22 21:44:07 +01:00
Spacer()
2023-01-22 21:44:07 +01:00
ActionButton {
2023-02-18 20:59:21 +01:00
await self.favourite()
2023-01-22 21:44:07 +01:00
} label: {
HStack(alignment: .center) {
2023-04-06 13:28:34 +02:00
Image(systemName: self.favourited ? "star.fill" : "star")
2023-01-22 21:44:07 +01:00
Text("\(self.favouritesCount)")
.font(.caption)
}
2023-01-04 17:56:01 +01:00
}
2023-01-04 17:56:01 +01:00
Spacer()
ActionButton {
2023-02-18 20:59:21 +01:00
await self.bookmark()
2023-01-04 17:56:01 +01:00
} label: {
2023-01-07 18:43:44 +01:00
Image(systemName: self.bookmarked ? "bookmark.fill" : "bookmark")
2023-01-03 14:09:22 +01:00
}
2023-01-22 21:44:07 +01:00
Spacer()
2023-01-22 21:44:07 +01:00
Menu {
2023-03-07 16:45:44 +01:00
NavigationLink(value: RouteurDestinations.accounts(listType: .reblogged(entityId: statusModel.id))) {
2023-03-19 18:50:37 +01:00
Label("status.title.reboostedBy", image: "custom.rocket")
2023-01-22 21:44:07 +01:00
}
2023-01-23 18:01:27 +01:00
2023-03-07 16:45:44 +01:00
NavigationLink(value: RouteurDestinations.accounts(listType: .favourited(entityId: statusModel.id))) {
2023-04-06 13:28:34 +02:00
Label("status.title.favouritedBy", systemImage: "star")
2023-01-22 15:56:35 +01:00
}
2023-01-23 11:42:28 +01:00
if let url = statusModel.url {
2023-01-22 21:44:07 +01:00
Divider()
Link(destination: url) {
2023-03-13 13:53:36 +01:00
Label("status.title.openInBrowser", systemImage: "safari")
2023-01-22 21:44:07 +01:00
}
2023-01-23 11:42:28 +01:00
2023-01-22 21:44:07 +01:00
ShareLink(item: url) {
2023-03-13 13:53:36 +01:00
Label("status.title.shareStatus", systemImage: "square.and.arrow.up")
2023-01-22 21:44:07 +01:00
}
}
if self.statusModel.account.id == self.applicationState.account?.id {
2023-03-13 13:53:36 +01:00
Section(header: Text("status.title.yourStatus", comment: "Your post")) {
2023-02-18 20:59:21 +01:00
Button(role: .destructive) {
self.deleteStatus()
} label: {
2023-03-13 13:53:36 +01:00
Label("status.title.delete", systemImage: "trash")
2023-02-18 20:59:21 +01:00
}
}
2023-04-04 09:14:07 +02:00
} else {
Divider()
Button {
self.routerPath.presentedSheet = .report(objectType: .post, objectId: self.statusModel.id)
} label: {
Label(NSLocalizedString("status.title.report", comment: "Report"), systemImage: "exclamationmark.triangle")
}
2023-02-18 20:59:21 +01:00
}
2023-01-22 21:44:07 +01:00
} label: {
Image(systemName: "gear")
2023-01-04 17:56:01 +01:00
}
2023-01-03 14:09:22 +01:00
}
2023-01-04 17:56:01 +01:00
.font(.title3)
2023-01-12 18:34:48 +01:00
.fontWeight(.bold)
2023-01-10 11:30:30 +01:00
.onAppear {
self.refreshCounters()
}
}
2023-01-10 11:30:30 +01:00
private func refreshCounters() {
self.repliesCount = self.statusModel.repliesCount
self.reblogged = self.statusModel.reblogged
self.reblogsCount = self.statusModel.reblogsCount
self.favourited = self.statusModel.favourited
self.favouritesCount = self.statusModel.favouritesCount
self.bookmarked = self.statusModel.bookmarked
2023-01-03 14:09:22 +01:00
}
2023-02-18 20:59:21 +01:00
private func reboost() async {
do {
let status = self.reblogged
? try await self.client.statuses?.unboost(statusId: self.statusModel.id)
: try await self.client.statuses?.boost(statusId: self.statusModel.id)
2023-02-18 20:59:21 +01:00
if let status {
self.reblogsCount = status.reblogsCount == self.reblogsCount
? status.reblogsCount + 1
: status.reblogsCount
self.reblogged = status.reblogged
}
2023-03-13 13:53:36 +01:00
ToastrService.shared.showSuccess(self.reblogged
? NSLocalizedString("status.title.reboosted", comment: "Reboosted")
2023-03-19 18:50:37 +01:00
: NSLocalizedString("status.title.unreboosted", comment: "Unreboosted"), imageName: "custom.rocket.fill")
2023-02-18 20:59:21 +01:00
} catch {
2023-03-13 13:53:36 +01:00
ErrorService.shared.handle(error, message: "status.error.reboostFailed", showToastr: true)
2023-02-18 20:59:21 +01:00
}
}
2023-02-18 20:59:21 +01:00
private func favourite() async {
do {
let status = self.favourited
? try await self.client.statuses?.unfavourite(statusId: self.statusModel.id)
: try await self.client.statuses?.favourite(statusId: self.statusModel.id)
2023-02-18 20:59:21 +01:00
if let status {
self.favouritesCount = status.favouritesCount == self.favouritesCount
? status.favouritesCount + 1
: status.favouritesCount
self.favourited = status.favourited
}
2023-03-13 13:53:36 +01:00
ToastrService.shared.showSuccess(self.favourited
? NSLocalizedString("status.title.favourited", comment: "Favourited")
2023-04-06 13:28:34 +02:00
: NSLocalizedString("status.title.unfavourited", comment: "Unfavourited"), imageSystemName: "star.fill")
2023-02-18 20:59:21 +01:00
} catch {
2023-03-13 13:53:36 +01:00
ErrorService.shared.handle(error, message: "status.error.favouriteFailed", showToastr: true)
2023-02-18 20:59:21 +01:00
}
}
2023-02-18 20:59:21 +01:00
private func bookmark() async {
do {
_ = self.bookmarked
? try await self.client.statuses?.unbookmark(statusId: self.statusModel.id)
: try await self.client.statuses?.bookmark(statusId: self.statusModel.id)
2023-02-18 20:59:21 +01:00
self.bookmarked.toggle()
2023-03-13 13:53:36 +01:00
ToastrService.shared.showSuccess(self.bookmarked
? NSLocalizedString("status.title.bookmarked", comment: "Bookmarked")
: NSLocalizedString("status.title.unbookmarked", comment: "Unbookmarked"), imageSystemName: "bookmark.fill")
2023-02-18 20:59:21 +01:00
} catch {
2023-03-13 13:53:36 +01:00
ErrorService.shared.handle(error, message: "status.error.bookmarkFailed", showToastr: true)
2023-02-18 20:59:21 +01:00
}
}
2023-02-18 20:59:21 +01:00
private func deleteStatus() {
Task {
do {
try await self.client.statuses?.delete(statusId: self.statusModel.id)
2023-03-13 13:53:36 +01:00
ToastrService.shared.showSuccess("status.title.statusDeleted", imageSystemName: "checkmark.circle.fill")
2023-02-18 20:59:21 +01:00
self.delete?()
} catch {
2023-03-13 13:53:36 +01:00
ErrorService.shared.handle(error, message: "status.error.deleteFailed", showToastr: true)
2023-02-18 20:59:21 +01:00
}
}
}
2023-01-03 14:09:22 +01:00
}