Handle status update in post context

This commit is contained in:
Thomas Ricouard 2022-12-25 17:39:23 +01:00
parent d600ab8800
commit 8e16764ff7
2 changed files with 21 additions and 2 deletions

View file

@ -6,6 +6,8 @@ import Network
import DesignSystem
public struct StatusDetailView: View {
@EnvironmentObject private var currentAccount: CurrentAccount
@EnvironmentObject private var watcher: StreamWatcher
@EnvironmentObject private var client: Client
@EnvironmentObject private var routeurPath: RouterPath
@StateObject private var viewModel: StatusDetailViewModel
@ -63,6 +65,10 @@ public struct StatusDetailView: View {
proxy.scrollTo(viewModel.statusId, anchor: .center)
}
}
.onChange(of: watcher.latestEvent?.id) { _ in
guard let lastEvent = watcher.latestEvent else { return }
viewModel.handleEvent(event: lastEvent, currentAccount: currentAccount.account)
}
}
.navigationTitle(viewModel.title)
.navigationBarTitleDisplayMode(.inline)

View file

@ -15,7 +15,7 @@ class StatusDetailViewModel: ObservableObject {
@Published var state: State = .loading
@Published var title: String = ""
init(statusId: String) {
state = .loading
self.statusId = statusId
@ -24,7 +24,6 @@ class StatusDetailViewModel: ObservableObject {
func fetchStatusDetail() async {
guard let client else { return }
do {
state = .loading
let status: Status = try await client.get(endpoint: Statuses.status(id: statusId))
let context: StatusContext = try await client.get(endpoint: Statuses.context(id: statusId))
state = .display(status: status, context: context)
@ -33,4 +32,18 @@ class StatusDetailViewModel: ObservableObject {
state = .error(error: error)
}
}
func handleEvent(event: any StreamEvent, currentAccount: Account?) {
if let event = event as? StreamEventUpdate,
event.status.account.id == currentAccount?.id {
Task {
await fetchStatusDetail()
}
} else if event is StreamEventDelete {
Task {
await fetchStatusDetail()
}
}
}
}