Impressia/Vernissage/Widgets/InteractionRow.swift

152 lines
5.6 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-01-10 11:30:30 +01:00
import MastodonKit
2023-01-15 12:41:55 +01:00
import Drops
2023-01-03 14:09:22 +01:00
struct InteractionRow: View {
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 statusViewModel: 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-01-03 14:09:22 +01:00
var body: some View {
HStack (alignment: .top) {
ActionButton {
2023-01-23 18:01:27 +01:00
self.routerPath.presentedSheet = .replyToStatusEditor(status: statusViewModel)
2023-01-04 17:56:01 +01:00
} label: {
HStack(alignment: .center) {
Image(systemName: "message")
2023-01-07 18:43:44 +01:00
Text("\(repliesCount)")
2023-01-04 17:56:01 +01:00
.font(.caption)
2023-01-03 14:09:22 +01:00
}
}
2023-01-04 17:56:01 +01:00
Spacer()
2023-01-22 21:44:07 +01:00
ActionButton {
do {
let status = self.reblogged
2023-02-03 15:16:30 +01:00
? try await self.client.statuses?.unboost(statusId: self.statusViewModel.id)
: try await self.client.statuses?.boost(statusId: self.statusViewModel.id)
2023-01-05 14:50:48 +01:00
if let status {
self.reblogsCount = status.reblogsCount == self.reblogsCount
? status.reblogsCount + 1
: status.reblogsCount
2023-01-05 14:50:48 +01:00
self.reblogged = status.reblogged
2023-01-05 14:50:48 +01:00
}
2023-01-15 12:41:55 +01:00
2023-01-22 17:41:11 +01:00
ToastrService.shared.showSuccess("Reboosted", imageSystemName: "paperplane.fill")
} catch {
2023-01-22 17:41:11 +01:00
ErrorService.shared.handle(error, message: "Reboost action failed.", showToastr: true)
2023-01-03 14:09:22 +01:00
}
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 {
do {
let status = self.favourited
2023-02-03 15:16:30 +01:00
? try await self.client.statuses?.unfavourite(statusId: self.statusViewModel.id)
: try await self.client.statuses?.favourite(statusId: self.statusViewModel.id)
2023-01-06 13:05:21 +01:00
if let status {
self.favouritesCount = status.favouritesCount == self.favouritesCount
? status.favouritesCount + 1
: status.favouritesCount
2023-01-05 14:50:48 +01:00
self.favourited = status.favourited
2023-01-05 14:50:48 +01:00
}
2023-01-15 12:41:55 +01:00
ToastrService.shared.showSuccess("Favourited", imageSystemName: "hand.thumbsup.fill")
} catch {
2023-01-15 12:41:55 +01:00
ErrorService.shared.handle(error, message: "Favourite action failed.", showToastr: true)
2023-01-05 14:50:48 +01:00
}
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 {
do {
_ = self.bookmarked
2023-02-03 15:16:30 +01:00
? try await self.client.statuses?.unbookmark(statusId: self.statusViewModel.id)
: try await self.client.statuses?.bookmark(statusId: self.statusViewModel.id)
2023-01-05 14:50:48 +01:00
self.bookmarked.toggle()
2023-01-28 11:48:26 +01:00
ToastrService.shared.showSuccess("Bookmarked", imageSystemName: "bookmark.fill")
} catch {
2023-01-28 11:48:26 +01:00
ErrorService.shared.handle(error, message: "Bookmark action failed.", showToastr: true)
2023-01-05 14:50:48 +01:00
}
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 {
2023-01-23 18:01:27 +01:00
NavigationLink(value: RouteurDestinations.accounts(entityId: statusViewModel.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: statusViewModel.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
2023-01-22 21:44:07 +01:00
if let url = statusViewModel.url {
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")
}
}
} 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() {
2023-01-10 20:38:02 +01:00
self.repliesCount = self.statusViewModel.repliesCount
self.reblogged = self.statusViewModel.reblogged
self.reblogsCount = self.statusViewModel.reblogsCount
self.favourited = self.statusViewModel.favourited
self.favouritesCount = self.statusViewModel.favouritesCount
self.bookmarked = self.statusViewModel.bookmarked
2023-01-03 14:09:22 +01:00
}
}