Refresh comments section when adding new comment.

This commit is contained in:
Marcin Czachursk 2023-02-09 18:58:54 +01:00
parent e8d3787511
commit 461df5e453
5 changed files with 31 additions and 6 deletions

View File

@ -16,6 +16,7 @@ public class ApplicationState: ObservableObject {
@Published var lastSeenStatusId: String?
@Published var amountOfNewStatuses = 0
@Published var newComment: CommentModel?
@Published var tintColor = TintColor.accentColor2
@Published var theme = Theme.system
@Published var avatarShape = AvatarShape.circle

View File

@ -10,3 +10,9 @@ public struct CommentModel {
var status: StatusModel
var showDivider: Bool
}
extension CommentModel: Equatable {
public static func == (lhs: CommentModel, rhs: CommentModel) -> Bool {
return lhs.status.id == rhs.status.id
}
}

View File

@ -101,7 +101,11 @@ struct ComposeView: View {
private func publishStatus() async {
do {
_ = try await self.client.statuses?.new(status: Mastodon.Statuses.Components(inReplyToId: self.statusViewModel?.id, text: self.text))
if let newStatus = try await self.client.statuses?.new(status:Mastodon.Statuses.Components(inReplyToId: self.statusViewModel?.id, text: self.text)) {
let statusModel = StatusModel(status: newStatus)
let commentModel = CommentModel(status: statusModel, showDivider: false)
self.applicationState.newComment = commentModel
}
} catch {
ErrorService.shared.handle(error, message: "Error during post status.", showToastr: true)
}

View File

@ -153,6 +153,8 @@ struct MainView: View {
Menu {
ForEach(self.dbAccounts) { account in
Button {
HapticService.shared.touch()
let accountModel = AccountModel(accountData: account)
self.applicationState.account = accountModel
self.client.setAccount(account: accountModel)
@ -172,6 +174,7 @@ struct MainView: View {
Divider()
Button {
HapticService.shared.touch()
self.routerPath.presentedSheet = .settings
} label: {
Label("Settings", systemImage: "gear")
@ -203,6 +206,7 @@ struct MainView: View {
if viewMode == .local || viewMode == .home || viewMode == .federated {
ToolbarItem(placement: .navigationBarTrailing) {
Button {
HapticService.shared.touch()
self.routerPath.presentedSheet = .newStatusEditor
} label: {
Image(systemName: "square.and.pencil")

View File

@ -50,17 +50,27 @@ struct CommentsSection: View {
}
}
}
.task {
do {
self.commentViewModels = try await self.client.statuses?.comments(to: statusId) ?? []
} catch {
ErrorService.shared.handle(error, message: "Comments cannot be downloaded.", showToastr: !Task.isCancelled)
.onChange(of: self.applicationState.newComment) { _ in
self.commentViewModels = nil
Task {
await self.loadComments()
}
}
.task {
await self.loadComments()
}
}
private func getInteractionRowTextColor() -> Color {
return self.colorScheme == .dark ? Color.black : Color.white
}
private func loadComments() async {
do {
self.commentViewModels = try await self.client.statuses?.comments(to: statusId) ?? []
} catch {
ErrorService.shared.handle(error, message: "Comments cannot be downloaded.", showToastr: !Task.isCancelled)
}
}
}