Impressia/Vernissage/Widgets/InteractionRow.swift

134 lines
4.7 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
struct InteractionRow: View {
2023-01-05 14:50:48 +01:00
@EnvironmentObject var applicationState: ApplicationState
2023-01-07 18:43:44 +01:00
@State var statusId = ""
@State var repliesCount = 0
@State var reblogged = false
@State var reblogsCount = 0
@State var favourited = false
@State var favouritesCount = 0
@State var bookmarked = false
2023-01-06 13:05:21 +01:00
2023-01-07 18:43:44 +01:00
var onNewStatus: () -> Void?
2023-01-06 18:16:08 +01:00
2023-01-03 14:09:22 +01:00
var body: some View {
HStack (alignment: .top) {
2023-01-04 17:56:01 +01:00
Button {
2023-01-06 13:05:21 +01:00
HapticService.shared.touch()
2023-01-07 18:43:44 +01:00
onNewStatus()
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()
Button {
2023-01-05 14:50:48 +01:00
Task {
2023-01-06 13:05:21 +01:00
HapticService.shared.touch()
2023-01-05 14:50:48 +01:00
do {
2023-01-07 18:43:44 +01:00
let status = self.reblogged
? try await StatusService.shared.unboost(statusId: self.statusId, accountData: self.applicationState.accountData)
: try await StatusService.shared.boost(statusId: self.statusId, accountData: self.applicationState.accountData)
2023-01-05 14:50:48 +01:00
if let status {
2023-01-07 18:43:44 +01:00
self.reblogsCount = status.reblogsCount == self.reblogsCount
? status.reblogsCount + 1
: status.reblogsCount
2023-01-05 14:50:48 +01:00
2023-01-07 18:43:44 +01:00
self.reblogged = status.reblogged
2023-01-05 14:50:48 +01:00
}
} catch {
print("Error \(error.localizedDescription)")
}
}
2023-01-04 17:56:01 +01:00
} label: {
HStack(alignment: .center) {
2023-01-07 18:43:44 +01:00
Image(systemName: self.reblogged ? "paperplane.fill" : "paperplane")
Text("\(self.reblogsCount)")
2023-01-04 17:56:01 +01:00
.font(.caption)
2023-01-03 14:09:22 +01:00
}
}
Spacer()
2023-01-04 17:56:01 +01:00
Button {
2023-01-05 14:50:48 +01:00
Task {
2023-01-06 13:05:21 +01:00
HapticService.shared.touch()
2023-01-05 14:50:48 +01:00
do {
2023-01-07 18:43:44 +01:00
let status = self.favourited
? try await StatusService.shared.unfavourite(statusId: self.statusId, accountData: self.applicationState.accountData)
: try await StatusService.shared.favourite(statusId: self.statusId, accountData: self.applicationState.accountData)
2023-01-05 14:50:48 +01:00
if let status {
2023-01-07 18:43:44 +01:00
self.favouritesCount = status.favouritesCount == self.favouritesCount
? status.favouritesCount + 1
: status.favouritesCount
2023-01-05 14:50:48 +01:00
2023-01-07 18:43:44 +01:00
self.favourited = status.favourited
2023-01-05 14:50:48 +01:00
}
} catch {
print("Error \(error.localizedDescription)")
}
}
2023-01-04 17:56:01 +01:00
} label: {
HStack(alignment: .center) {
2023-01-07 18:43:44 +01:00
Image(systemName: self.favourited ? "hand.thumbsup.fill" : "hand.thumbsup")
Text("\(self.favouritesCount)")
2023-01-04 17:56:01 +01:00
.font(.caption)
}
}
Spacer()
Button {
2023-01-05 14:50:48 +01:00
Task {
2023-01-06 13:05:21 +01:00
HapticService.shared.touch()
2023-01-05 14:50:48 +01:00
do {
2023-01-07 18:43:44 +01:00
_ = self.bookmarked
? try await StatusService.shared.unbookmark(statusId: self.statusId, accountData: self.applicationState.accountData)
: try await StatusService.shared.bookmark(statusId: self.statusId, accountData: self.applicationState.accountData)
2023-01-05 14:50:48 +01:00
2023-01-07 18:43:44 +01:00
self.bookmarked.toggle()
2023-01-05 14:50:48 +01:00
} catch {
print("Error \(error.localizedDescription)")
}
}
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
Spacer()
Button {
2023-01-05 11:55:20 +01:00
// TODO: Share.
2023-01-06 13:05:21 +01:00
HapticService.shared.touch()
2023-01-04 17:56:01 +01:00
} label: {
Image(systemName: "square.and.arrow.up")
}
2023-01-03 14:09:22 +01:00
}
2023-01-04 17:56:01 +01:00
.font(.title3)
.fontWeight(.semibold)
2023-01-03 14:09:22 +01:00
}
}
struct InteractionRow_Previews: PreviewProvider {
static var previews: some View {
2023-01-07 18:43:44 +01:00
InteractionRow() { }
2023-01-04 20:56:26 +01:00
.previewLayout(.fixed(width: 300, height: 70))
2023-01-03 14:09:22 +01:00
}
}