From 7a7012004ccd952a9c9a71360e714aa355df1ba1 Mon Sep 17 00:00:00 2001 From: Marcin Czachursk Date: Sat, 7 Jan 2023 18:43:44 +0100 Subject: [PATCH] Move status row to all statuses --- Vernissage/Models/ApplicationState.swift | 1 + Vernissage/Views/StatusView.swift | 8 ++- Vernissage/Widgets/CommentsSection.swift | 49 +++++++++++-------- Vernissage/Widgets/InteractionRow.swift | 62 +++++++++++++----------- 4 files changed, 70 insertions(+), 50 deletions(-) diff --git a/Vernissage/Models/ApplicationState.swift b/Vernissage/Models/ApplicationState.swift index 9896adf..47d9f82 100644 --- a/Vernissage/Models/ApplicationState.swift +++ b/Vernissage/Models/ApplicationState.swift @@ -12,6 +12,7 @@ public class ApplicationState: ObservableObject { private init() { } @Published var accountData: AccountData? + @Published var showInteractionStatusId = "" } extension ApplicationState { diff --git a/Vernissage/Views/StatusView.swift b/Vernissage/Views/StatusView.swift index 77163a4..abcfc52 100644 --- a/Vernissage/Views/StatusView.swift +++ b/Vernissage/Views/StatusView.swift @@ -55,7 +55,13 @@ struct StatusView: View { .foregroundColor(.lightGrayColor) .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() } .padding(8) diff --git a/Vernissage/Widgets/CommentsSection.swift b/Vernissage/Widgets/CommentsSection.swift index a1d14a6..fe868ae 100644 --- a/Vernissage/Widgets/CommentsSection.swift +++ b/Vernissage/Widgets/CommentsSection.swift @@ -27,7 +27,7 @@ struct CommentsSection: View { Rectangle() .size(width: UIScreen.main.bounds.width, height: 4) .fill(Color.mainTextColor) - .opacity(0.1) + .opacity(0.2) } HStack (alignment: .top) { @@ -60,26 +60,6 @@ struct CommentsSection: View { .fontWeight(.bold) 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)) .foregroundColor(.lightGrayColor) @@ -115,9 +95,36 @@ struct CommentsSection: View { .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(.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 onNewStatus(context) diff --git a/Vernissage/Widgets/InteractionRow.swift b/Vernissage/Widgets/InteractionRow.swift index 272d859..415994c 100644 --- a/Vernissage/Widgets/InteractionRow.swift +++ b/Vernissage/Widgets/InteractionRow.swift @@ -8,19 +8,25 @@ import SwiftUI struct InteractionRow: View { @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 { HStack (alignment: .top) { Button { HapticService.shared.touch() - onNewStatus(statusData) + onNewStatus() } label: { HStack(alignment: .center) { Image(systemName: "message") - Text("\(statusData.repliesCount)") + Text("\(repliesCount)") .font(.caption) } } @@ -32,16 +38,16 @@ struct InteractionRow: View { HapticService.shared.touch() 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) + 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) if let status { - self.statusData.reblogsCount = status.reblogsCount == self.statusData.reblogsCount - ? Int32(status.reblogsCount + 1) - : Int32(status.reblogsCount) + self.reblogsCount = status.reblogsCount == self.reblogsCount + ? status.reblogsCount + 1 + : status.reblogsCount - self.statusData.reblogged = status.reblogged + self.reblogged = status.reblogged } } catch { print("Error \(error.localizedDescription)") @@ -49,8 +55,8 @@ struct InteractionRow: View { } } label: { HStack(alignment: .center) { - Image(systemName: statusData.reblogged ? "paperplane.fill" : "paperplane") - Text("\(statusData.reblogsCount)") + Image(systemName: self.reblogged ? "paperplane.fill" : "paperplane") + Text("\(self.reblogsCount)") .font(.caption) } } @@ -62,16 +68,16 @@ struct InteractionRow: View { HapticService.shared.touch() 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) + 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) if let status { - self.statusData.favouritesCount = status.favouritesCount == self.statusData.favouritesCount - ? Int32(status.favouritesCount + 1) - : Int32(status.favouritesCount) + self.favouritesCount = status.favouritesCount == self.favouritesCount + ? status.favouritesCount + 1 + : status.favouritesCount - self.statusData.favourited = status.favourited + self.favourited = status.favourited } } catch { print("Error \(error.localizedDescription)") @@ -79,8 +85,8 @@ struct InteractionRow: View { } } label: { HStack(alignment: .center) { - Image(systemName: statusData.favourited ? "hand.thumbsup.fill" : "hand.thumbsup") - Text("\(statusData.favouritesCount)") + Image(systemName: self.favourited ? "hand.thumbsup.fill" : "hand.thumbsup") + Text("\(self.favouritesCount)") .font(.caption) } } @@ -92,17 +98,17 @@ struct InteractionRow: View { HapticService.shared.touch() do { - _ = self.statusData.bookmarked - ? 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.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) - self.statusData.bookmarked.toggle() + self.bookmarked.toggle() } catch { print("Error \(error.localizedDescription)") } } } label: { - Image(systemName: statusData.bookmarked ? "bookmark.fill" : "bookmark") + Image(systemName: self.bookmarked ? "bookmark.fill" : "bookmark") } Spacer() @@ -122,7 +128,7 @@ struct InteractionRow: View { struct InteractionRow_Previews: PreviewProvider { static var previews: some View { - InteractionRow(statusData: PreviewData.getStatus()) { context in } + InteractionRow() { } .previewLayout(.fixed(width: 300, height: 70)) } }