diff --git a/Packages/Status/Sources/Status/Detail/StatusDetailVIew.swift b/Packages/Status/Sources/Status/Detail/StatusDetailVIew.swift index 88a43b4d..38d1b566 100644 --- a/Packages/Status/Sources/Status/Detail/StatusDetailVIew.swift +++ b/Packages/Status/Sources/Status/Detail/StatusDetailVIew.swift @@ -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) diff --git a/Packages/Status/Sources/Status/Detail/StatusDetailViewModel.swift b/Packages/Status/Sources/Status/Detail/StatusDetailViewModel.swift index 72965f28..59334223 100644 --- a/Packages/Status/Sources/Status/Detail/StatusDetailViewModel.swift +++ b/Packages/Status/Sources/Status/Detail/StatusDetailViewModel.swift @@ -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() + } + } + } }