Impressia/Vernissage/Widgets/InteractionRow.swift

129 lines
4.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
struct InteractionRow: View {
2023-01-05 14:50:48 +01:00
@EnvironmentObject var applicationState: ApplicationState
2023-01-04 17:56:01 +01:00
@ObservedObject public var statusData: StatusData
2023-01-06 13:05:21 +01:00
2023-01-06 18:16:08 +01:00
var onNewStatus: (_ context: StatusData) -> Void?
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-06 18:16:08 +01:00
onNewStatus(statusData)
2023-01-04 17:56:01 +01:00
} label: {
HStack(alignment: .center) {
Image(systemName: "message")
Text("\(statusData.repliesCount)")
.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 {
let status = self.statusData.reblogged
? try await StatusService.shared.unboost(statusId: self.statusData.id, accountData: self.applicationState.accountData)
: try await StatusService.shared.boost(statusId: self.statusData.id, accountData: self.applicationState.accountData)
if let status {
self.statusData.reblogsCount = status.reblogsCount == self.statusData.reblogsCount
? Int32(status.reblogsCount + 1)
: Int32(status.reblogsCount)
self.statusData.reblogged = status.reblogged
}
} catch {
print("Error \(error.localizedDescription)")
}
}
2023-01-04 17:56:01 +01:00
} label: {
HStack(alignment: .center) {
2023-01-04 20:56:26 +01:00
Image(systemName: statusData.reblogged ? "paperplane.fill" : "paperplane")
2023-01-04 17:56:01 +01:00
Text("\(statusData.reblogsCount)")
.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 {
let status = self.statusData.favourited
? try await StatusService.shared.unfavourite(statusId: self.statusData.id, accountData: self.applicationState.accountData)
: try await StatusService.shared.favourite(statusId: self.statusData.id, accountData: self.applicationState.accountData)
if let status {
self.statusData.favouritesCount = status.favouritesCount == self.statusData.favouritesCount
? Int32(status.favouritesCount + 1)
: Int32(status.favouritesCount)
self.statusData.favourited = status.favourited
}
} catch {
print("Error \(error.localizedDescription)")
}
}
2023-01-04 17:56:01 +01:00
} label: {
HStack(alignment: .center) {
Image(systemName: statusData.favourited ? "hand.thumbsup.fill" : "hand.thumbsup")
Text("\(statusData.favouritesCount)")
.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-06 13:05:21 +01:00
_ = self.statusData.bookmarked
2023-01-05 14:50:48 +01:00
? try await StatusService.shared.unbookmark(statusId: self.statusData.id, accountData: self.applicationState.accountData)
: try await StatusService.shared.bookmark(statusId: self.statusData.id, accountData: self.applicationState.accountData)
self.statusData.bookmarked.toggle()
} catch {
print("Error \(error.localizedDescription)")
}
}
2023-01-04 17:56:01 +01:00
} label: {
2023-01-03 14:09:22 +01:00
Image(systemName: statusData.bookmarked ? "bookmark.fill" : "bookmark")
}
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-06 13:05:21 +01:00
.foregroundColor(.accentColor)
2023-01-03 14:09:22 +01:00
}
}
struct InteractionRow_Previews: PreviewProvider {
static var previews: some View {
2023-01-06 18:16:08 +01:00
InteractionRow(statusData: PreviewData.getStatus()) { context in }
2023-01-04 20:56:26 +01:00
.previewLayout(.fixed(width: 300, height: 70))
2023-01-03 14:09:22 +01:00
}
}