Impressia/Vernissage/Widgets/InteractionRow.swift

198 lines
6.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.
// Licensed under the MIT License.
//
import SwiftUI
2023-02-19 10:32:38 +01:00
import PixelfedKit
2023-01-15 12:41:55 +01:00
import Drops
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
2023-01-10 11:30:30 +01:00
@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-01-23 18:01:27 +01:00
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-01-22 21:44:07 +01:00
Image(systemName: self.reblogged ? "paperplane.fill" : "paperplane")
Text("\(self.reblogsCount)")
2023-01-22 17:41:11 +01:00
.font(.caption)
}
2023-01-22 21:44:07 +01:00
}
Spacer()
ActionButton {
2023-02-18 20:59:21 +01:00
await self.favourite()
2023-01-22 21:44:07 +01:00
} label: {
HStack(alignment: .center) {
Image(systemName: self.favourited ? "hand.thumbsup.fill" : "hand.thumbsup")
Text("\(self.favouritesCount)")
.font(.caption)
}
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-04 17:56:01 +01:00
2023-01-22 21:44:07 +01:00
Spacer()
Menu {
NavigationLink(value: RouteurDestinations.accounts(entityId: statusModel.id, listType: .reblogged)) {
2023-01-22 21:44:07 +01:00
Label("Reboosted by", systemImage: "paperplane")
}
2023-01-23 18:01:27 +01:00
NavigationLink(value: RouteurDestinations.accounts(entityId: statusModel.id, listType: .favourited)) {
2023-01-22 21:44:07 +01:00
Label("Favourited by", systemImage: "hand.thumbsup")
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) {
Label("Open in browser", systemImage: "safari")
}
2023-01-23 11:42:28 +01:00
2023-01-22 21:44:07 +01:00
ShareLink(item: url) {
Label("Share post", systemImage: "square.and.arrow.up")
}
}
2023-02-18 20:59:21 +01:00
if self.statusModel.account.id == self.applicationState.account?.id {
2023-02-18 20:59:21 +01:00
Section(header: Text("Your post")) {
Button(role: .destructive) {
self.deleteStatus()
} label: {
Label("Delete", systemImage: "trash")
}
}
}
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()
}
}
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
}
ToastrService.shared.showSuccess(self.reblogged ? "Reboosted" : "Unreboosted", imageSystemName: "paperplane.fill")
} catch {
ErrorService.shared.handle(error, message: "Reboost action failed.", showToastr: true)
}
}
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
}
ToastrService.shared.showSuccess(self.favourited ? "Favourited" : "Unfavourited", imageSystemName: "hand.thumbsup.fill")
} catch {
ErrorService.shared.handle(error, message: "Favourite action failed.", showToastr: true)
}
}
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()
ToastrService.shared.showSuccess(self.bookmarked ? "Bookmarked" : "Unbookmarked", imageSystemName: "bookmark.fill")
} catch {
ErrorService.shared.handle(error, message: "Bookmark action failed.", showToastr: true)
}
}
private func deleteStatus() {
Task {
do {
try await self.client.statuses?.delete(statusId: self.statusModel.id)
2023-02-18 20:59:21 +01:00
ToastrService.shared.showSuccess("Post deleted", imageSystemName: "checkmark.circle.fill")
self.delete?()
} catch {
ErrorService.shared.handle(error, message: "Delete action failed.", showToastr: true)
}
}
}
2023-01-03 14:09:22 +01:00
}