Merge pull request #95 from VernissageApp/bugfix/fix-refreshing-comments

#19 Fix refreshing comments after publishing new one
This commit is contained in:
Marcin Czachurski 2023-10-22 14:03:37 +02:00 committed by GitHub
commit 1199b67765
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 13 deletions

View File

@ -47,8 +47,8 @@ import ClientKit
/// Amount of new statuses which are not displayed yet to the user.
public var amountOfNewStatuses = 0
/// Model for newly created comment.
public var newComment: CommentModel?
/// Id of latest published status by the user.
public var latestPublishedStatusId: String?
/// Active icon name.
public var activeIcon = "Default"

View File

@ -36,10 +36,12 @@ struct CommentsSectionView: View {
if self.applicationState.showInteractionStatusId == commentViewModel.status.id {
VStack(alignment: .leading, spacing: 0) {
InteractionRow(statusModel: commentViewModel.status)
.foregroundColor(self.getInteractionRowTextColor())
.padding(.horizontal, 16)
.padding(.vertical, 8)
InteractionRow(statusModel: commentViewModel.status) {
self.reloadComments()
}
.foregroundColor(self.getInteractionRowTextColor())
.padding(.horizontal, 16)
.padding(.vertical, 8)
}
.background(Color.customGrayColor.opacity(0.5))
.transition(AnyTransition.move(edge: .top).combined(with: .opacity))
@ -54,11 +56,8 @@ struct CommentsSectionView: View {
}
}
}
.onChange(of: self.applicationState.newComment) {
self.commentViewModels = nil
Task {
await self.loadComments()
}
.onChange(of: self.applicationState.latestPublishedStatusId) {
self.reloadComments()
}
.task {
await self.loadComments()
@ -71,9 +70,25 @@ struct CommentsSectionView: View {
private func loadComments() async {
do {
self.commentViewModels = try await self.client.statuses?.comments(to: statusId) ?? []
let comments = try await self.client.statuses?.comments(to: statusId) ?? []
withAnimation {
self.commentViewModels = comments
}
} catch {
ErrorService.shared.handle(error, message: "status.error.loadingCommentsFailed", showToastr: !Task.isCancelled)
}
}
private func reloadComments() {
withAnimation {
self.commentViewModels = nil
}
// We have to download status after some time (when we ask strigt away we don't have ne comment in the list).
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
Task { @MainActor in
await self.loadComments()
}
}
}
}

View File

@ -728,7 +728,11 @@ public struct BaseComposeView: View {
private func sendToServer() async {
do {
let status = self.createStatus()
if try await self.client.statuses?.new(status: status) != nil {
if let status = try await self.client.statuses?.new(status: status) {
print("Status: \(status.id)")
self.applicationState.latestPublishedStatusId = status.id
self.applicationState.showInteractionStatusId = String.empty()
self.close()
}
} catch {