Ensure own status is added on top

This commit is contained in:
Thomas Ricouard 2022-12-25 19:18:19 +01:00
parent 6bbe352f7e
commit b04ccc18fa
2 changed files with 10 additions and 3 deletions

View File

@ -7,6 +7,7 @@ import DesignSystem
import Env
public struct TimelineView: View {
@EnvironmentObject private var account: CurrentAccount
@EnvironmentObject private var watcher: StreamWatcher
@EnvironmentObject private var client: Client
@StateObject private var viewModel = TimelineViewModel()
@ -56,7 +57,7 @@ public struct TimelineView: View {
}
.onChange(of: watcher.latestEvent?.id) { id in
if let latestEvent = watcher.latestEvent {
viewModel.handleEvent(event: latestEvent)
viewModel.handleEvent(event: latestEvent, currentAccount: account)
}
}
}

View File

@ -2,6 +2,7 @@ import SwiftUI
import Network
import Models
import Status
import Env
@MainActor
class TimelineViewModel: ObservableObject, StatusesFetcher {
@ -85,10 +86,15 @@ class TimelineViewModel: ObservableObject, StatusesFetcher {
} catch {}
}
func handleEvent(event: any StreamEvent) {
func handleEvent(event: any StreamEvent, currentAccount: CurrentAccount) {
guard timeline == .home else { return }
if let event = event as? StreamEventUpdate {
pendingStatuses.insert(event.status, at: 0)
if event.status.account.id == currentAccount.account?.id {
statuses.insert(event.status, at: 0)
statusesState = .display(statuses: statuses, nextPageState: .hasNextPage)
} else {
pendingStatuses.insert(event.status, at: 0)
}
} else if let event = event as? StreamEventDelete {
statuses.removeAll(where: { $0.id == event.status })
statusesState = .display(statuses: statuses, nextPageState: .hasNextPage)