Move status row to all statuses

This commit is contained in:
Marcin Czachursk 2023-01-07 18:43:44 +01:00
parent 4483d7500e
commit 7a7012004c
4 changed files with 70 additions and 50 deletions

View File

@ -12,6 +12,7 @@ public class ApplicationState: ObservableObject {
private init() { } private init() { }
@Published var accountData: AccountData? @Published var accountData: AccountData?
@Published var showInteractionStatusId = ""
} }
extension ApplicationState { extension ApplicationState {

View File

@ -55,7 +55,13 @@ struct StatusView: View {
.foregroundColor(.lightGrayColor) .foregroundColor(.lightGrayColor)
.font(.footnote) .font(.footnote)
InteractionRow(statusData: statusData) { context in InteractionRow(statusId: statusData.id,
repliesCount: Int(statusData.repliesCount),
reblogged: statusData.reblogged,
reblogsCount: Int(statusData.reblogsCount),
favourited: statusData.favourited,
favouritesCount: Int(statusData.favouritesCount),
bookmarked: statusData.bookmarked) {
self.showCompose.toggle() self.showCompose.toggle()
} }
.padding(8) .padding(8)

View File

@ -27,7 +27,7 @@ struct CommentsSection: View {
Rectangle() Rectangle()
.size(width: UIScreen.main.bounds.width, height: 4) .size(width: UIScreen.main.bounds.width, height: 4)
.fill(Color.mainTextColor) .fill(Color.mainTextColor)
.opacity(0.1) .opacity(0.2)
} }
HStack (alignment: .top) { HStack (alignment: .top) {
@ -60,26 +60,6 @@ struct CommentsSection: View {
.fontWeight(.bold) .fontWeight(.bold)
Spacer() Spacer()
Button {
HapticService.shared.touch()
onNewStatus(status)
} label: {
Image(systemName: "message")
.foregroundColor(.lightGrayColor)
.font(.footnote)
}
.padding(.trailing, 8)
Button {
HapticService.shared.touch()
// TODO: favorite
} label: {
Image(systemName: status.favourited ? "hand.thumbsup.fill" : "hand.thumbsup")
.foregroundColor(.lightGrayColor)
.font(.footnote)
}
.padding(.trailing, 8)
Text(status.createdAt.toRelative(.isoDateTimeMilliSec)) Text(status.createdAt.toRelative(.isoDateTimeMilliSec))
.foregroundColor(.lightGrayColor) .foregroundColor(.lightGrayColor)
@ -115,9 +95,36 @@ struct CommentsSection: View {
.padding(.bottom, 8) .padding(.bottom, 8)
} }
} }
.onTapGesture {
withAnimation(.linear(duration: 0.3)) {
if status.id == self.applicationState.showInteractionStatusId {
self.applicationState.showInteractionStatusId = ""
} else {
self.applicationState.showInteractionStatusId = status.id
}
}
}
} }
.padding(.horizontal, 8) .padding(.horizontal, 8)
.padding(.bottom, 8) .padding(.bottom, 8)
if self.applicationState.showInteractionStatusId == status.id {
VStack (alignment: .leading) {
InteractionRow(statusId: status.id,
repliesCount: status.repliesCount,
reblogged: status.reblogged,
reblogsCount: status.reblogsCount,
favourited: status.favourited,
favouritesCount: status.favouritesCount,
bookmarked: status.bookmarked) {
onNewStatus(status)
}
.padding(.horizontal, 16)
.padding(.vertical, 8)
}
.background(Color.mainTextColor.opacity(0.08))
.transition(AnyTransition.move(edge: .top).combined(with: .opacity))
}
CommentsSection(statusId: status.id, withDivider: false) { context in CommentsSection(statusId: status.id, withDivider: false) { context in
onNewStatus(context) onNewStatus(context)

View File

@ -8,19 +8,25 @@ import SwiftUI
struct InteractionRow: View { struct InteractionRow: View {
@EnvironmentObject var applicationState: ApplicationState @EnvironmentObject var applicationState: ApplicationState
@ObservedObject public var statusData: StatusData @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
var onNewStatus: (_ context: StatusData) -> Void? var onNewStatus: () -> Void?
var body: some View { var body: some View {
HStack (alignment: .top) { HStack (alignment: .top) {
Button { Button {
HapticService.shared.touch() HapticService.shared.touch()
onNewStatus(statusData) onNewStatus()
} label: { } label: {
HStack(alignment: .center) { HStack(alignment: .center) {
Image(systemName: "message") Image(systemName: "message")
Text("\(statusData.repliesCount)") Text("\(repliesCount)")
.font(.caption) .font(.caption)
} }
} }
@ -32,16 +38,16 @@ struct InteractionRow: View {
HapticService.shared.touch() HapticService.shared.touch()
do { do {
let status = self.statusData.reblogged let status = self.reblogged
? try await StatusService.shared.unboost(statusId: self.statusData.id, accountData: self.applicationState.accountData) ? try await StatusService.shared.unboost(statusId: self.statusId, accountData: self.applicationState.accountData)
: try await StatusService.shared.boost(statusId: self.statusData.id, accountData: self.applicationState.accountData) : try await StatusService.shared.boost(statusId: self.statusId, accountData: self.applicationState.accountData)
if let status { if let status {
self.statusData.reblogsCount = status.reblogsCount == self.statusData.reblogsCount self.reblogsCount = status.reblogsCount == self.reblogsCount
? Int32(status.reblogsCount + 1) ? status.reblogsCount + 1
: Int32(status.reblogsCount) : status.reblogsCount
self.statusData.reblogged = status.reblogged self.reblogged = status.reblogged
} }
} catch { } catch {
print("Error \(error.localizedDescription)") print("Error \(error.localizedDescription)")
@ -49,8 +55,8 @@ struct InteractionRow: View {
} }
} label: { } label: {
HStack(alignment: .center) { HStack(alignment: .center) {
Image(systemName: statusData.reblogged ? "paperplane.fill" : "paperplane") Image(systemName: self.reblogged ? "paperplane.fill" : "paperplane")
Text("\(statusData.reblogsCount)") Text("\(self.reblogsCount)")
.font(.caption) .font(.caption)
} }
} }
@ -62,16 +68,16 @@ struct InteractionRow: View {
HapticService.shared.touch() HapticService.shared.touch()
do { do {
let status = self.statusData.favourited let status = self.favourited
? try await StatusService.shared.unfavourite(statusId: self.statusData.id, accountData: self.applicationState.accountData) ? try await StatusService.shared.unfavourite(statusId: self.statusId, accountData: self.applicationState.accountData)
: try await StatusService.shared.favourite(statusId: self.statusData.id, accountData: self.applicationState.accountData) : try await StatusService.shared.favourite(statusId: self.statusId, accountData: self.applicationState.accountData)
if let status { if let status {
self.statusData.favouritesCount = status.favouritesCount == self.statusData.favouritesCount self.favouritesCount = status.favouritesCount == self.favouritesCount
? Int32(status.favouritesCount + 1) ? status.favouritesCount + 1
: Int32(status.favouritesCount) : status.favouritesCount
self.statusData.favourited = status.favourited self.favourited = status.favourited
} }
} catch { } catch {
print("Error \(error.localizedDescription)") print("Error \(error.localizedDescription)")
@ -79,8 +85,8 @@ struct InteractionRow: View {
} }
} label: { } label: {
HStack(alignment: .center) { HStack(alignment: .center) {
Image(systemName: statusData.favourited ? "hand.thumbsup.fill" : "hand.thumbsup") Image(systemName: self.favourited ? "hand.thumbsup.fill" : "hand.thumbsup")
Text("\(statusData.favouritesCount)") Text("\(self.favouritesCount)")
.font(.caption) .font(.caption)
} }
} }
@ -92,17 +98,17 @@ struct InteractionRow: View {
HapticService.shared.touch() HapticService.shared.touch()
do { do {
_ = self.statusData.bookmarked _ = self.bookmarked
? try await StatusService.shared.unbookmark(statusId: self.statusData.id, accountData: self.applicationState.accountData) ? try await StatusService.shared.unbookmark(statusId: self.statusId, accountData: self.applicationState.accountData)
: try await StatusService.shared.bookmark(statusId: self.statusData.id, accountData: self.applicationState.accountData) : try await StatusService.shared.bookmark(statusId: self.statusId, accountData: self.applicationState.accountData)
self.statusData.bookmarked.toggle() self.bookmarked.toggle()
} catch { } catch {
print("Error \(error.localizedDescription)") print("Error \(error.localizedDescription)")
} }
} }
} label: { } label: {
Image(systemName: statusData.bookmarked ? "bookmark.fill" : "bookmark") Image(systemName: self.bookmarked ? "bookmark.fill" : "bookmark")
} }
Spacer() Spacer()
@ -122,7 +128,7 @@ struct InteractionRow: View {
struct InteractionRow_Previews: PreviewProvider { struct InteractionRow_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
InteractionRow(statusData: PreviewData.getStatus()) { context in } InteractionRow() { }
.previewLayout(.fixed(width: 300, height: 70)) .previewLayout(.fixed(width: 300, height: 70))
} }
} }