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-04-01 12:10:59 +02:00
|
|
|
|
2023-10-19 13:24:02 +02:00
|
|
|
@Environment(ApplicationState.self) var applicationState
|
|
|
|
@Environment(Client.self) var client
|
|
|
|
@Environment(RouterPath.self) var routerPath
|
2023-10-20 07:45:18 +02:00
|
|
|
@Environment(\.modelContext) private var modelContext
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-02-19 08:30:01 +01:00
|
|
|
@State var statusModel: StatusModel
|
2023-04-01 12:10:59 +02:00
|
|
|
|
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-04-01 12:10:59 +02:00
|
|
|
|
2023-02-18 20:59:21 +01:00
|
|
|
private let delete: DeleteAction?
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-02-19 08:30:01 +01:00
|
|
|
public init(statusModel: StatusModel, delete: DeleteAction? = nil) {
|
|
|
|
self.statusModel = statusModel
|
2023-02-18 20:59:21 +01:00
|
|
|
self.delete = delete
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-01-03 14:09:22 +01:00
|
|
|
var body: some View {
|
2023-04-01 12:10:59 +02:00
|
|
|
HStack(alignment: .top) {
|
2023-02-19 08:30:01 +01:00
|
|
|
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
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-02-19 08:30:01 +01:00
|
|
|
Spacer()
|
2023-01-03 14:09:22 +01:00
|
|
|
}
|
2023-04-01 12:10:59 +02: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-04-01 12:10:59 +02:00
|
|
|
|
2023-01-22 21:44:07 +01:00
|
|
|
Spacer()
|
2023-04-01 12:10:59 +02:00
|
|
|
|
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-04-01 12:10:59 +02:00
|
|
|
|
2023-01-04 17:56:01 +01:00
|
|
|
Spacer()
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-01-09 12:14:33 +01:00
|
|
|
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-04-01 12:10:59 +02:00
|
|
|
|
2023-01-22 21:44:07 +01:00
|
|
|
Spacer()
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-01-22 21:44:07 +01:00
|
|
|
Menu {
|
2023-09-30 13:52:46 +02:00
|
|
|
NavigationLink(value: RouteurDestinations.accounts(listType: .reblogged(entityId: statusModel.getOrginalStatusId()))) {
|
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-09-30 13:52:46 +02:00
|
|
|
NavigationLink(value: RouteurDestinations.accounts(listType: .favourited(entityId: statusModel.getOrginalStatusId()))) {
|
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
|
|
|
|
2023-02-19 08:30:01 +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
|
|
|
}
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-02-19 08:30:01 +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 {
|
2023-09-30 13:52:46 +02:00
|
|
|
self.routerPath.presentedSheet = .report(objectType: .post, objectId: self.statusModel.getOrginalStatusId())
|
2023-04-04 09:14:07 +02:00
|
|
|
} 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-04-01 12:10:59 +02:00
|
|
|
|
2023-01-10 11:30:30 +01:00
|
|
|
private func refreshCounters() {
|
2023-02-19 08:30:01 +01:00
|
|
|
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-04-01 12:10:59 +02:00
|
|
|
|
2023-02-18 20:59:21 +01:00
|
|
|
private func reboost() async {
|
|
|
|
do {
|
|
|
|
let status = self.reblogged
|
2023-09-30 13:52:46 +02:00
|
|
|
? try await self.client.statuses?.unboost(statusId: self.statusModel.getOrginalStatusId())
|
|
|
|
: try await self.client.statuses?.boost(statusId: self.statusModel.getOrginalStatusId())
|
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-10-18 11:14:56 +02:00
|
|
|
ToastrService.shared.showSuccess(self.reblogged ? "status.title.reboosted" : "status.title.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-04-01 12:10:59 +02:00
|
|
|
|
2023-02-18 20:59:21 +01:00
|
|
|
private func favourite() async {
|
|
|
|
do {
|
|
|
|
let status = self.favourited
|
2023-09-30 13:52:46 +02:00
|
|
|
? try await self.client.statuses?.unfavourite(statusId: self.statusModel.getOrginalStatusId())
|
|
|
|
: try await self.client.statuses?.favourite(statusId: self.statusModel.getOrginalStatusId())
|
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-04-01 12:10:59 +02:00
|
|
|
|
2023-10-18 11:14:56 +02:00
|
|
|
ToastrService.shared.showSuccess(self.favourited ? "status.title.favourited" : "status.title.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-04-01 12:10:59 +02:00
|
|
|
|
2023-02-18 20:59:21 +01:00
|
|
|
private func bookmark() async {
|
|
|
|
do {
|
|
|
|
_ = self.bookmarked
|
2023-09-30 13:52:46 +02:00
|
|
|
? try await self.client.statuses?.unbookmark(statusId: self.statusModel.getOrginalStatusId())
|
|
|
|
: try await self.client.statuses?.bookmark(statusId: self.statusModel.getOrginalStatusId())
|
2023-02-18 20:59:21 +01:00
|
|
|
|
|
|
|
self.bookmarked.toggle()
|
2023-10-18 11:14:56 +02:00
|
|
|
ToastrService.shared.showSuccess(self.bookmarked ? "status.title.bookmarked" : "status.title.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-04-01 12:10:59 +02:00
|
|
|
|
2023-02-18 20:59:21 +01:00
|
|
|
private func deleteStatus() {
|
2023-10-22 13:26:55 +02:00
|
|
|
Task { @MainActor in
|
2023-02-18 20:59:21 +01:00
|
|
|
do {
|
2023-05-03 10:58:03 +02:00
|
|
|
// Remove from server.
|
2023-02-19 08:30:01 +01:00
|
|
|
try await self.client.statuses?.delete(statusId: self.statusModel.id)
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-05-03 10:58:03 +02: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
|
|
|
}
|