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 import Env
public struct TimelineView: View { public struct TimelineView: View {
@EnvironmentObject private var account: CurrentAccount
@EnvironmentObject private var watcher: StreamWatcher @EnvironmentObject private var watcher: StreamWatcher
@EnvironmentObject private var client: Client @EnvironmentObject private var client: Client
@StateObject private var viewModel = TimelineViewModel() @StateObject private var viewModel = TimelineViewModel()
@ -56,7 +57,7 @@ public struct TimelineView: View {
} }
.onChange(of: watcher.latestEvent?.id) { id in .onChange(of: watcher.latestEvent?.id) { id in
if let latestEvent = watcher.latestEvent { 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 Network
import Models import Models
import Status import Status
import Env
@MainActor @MainActor
class TimelineViewModel: ObservableObject, StatusesFetcher { class TimelineViewModel: ObservableObject, StatusesFetcher {
@ -85,10 +86,15 @@ class TimelineViewModel: ObservableObject, StatusesFetcher {
} catch {} } catch {}
} }
func handleEvent(event: any StreamEvent) { func handleEvent(event: any StreamEvent, currentAccount: CurrentAccount) {
guard timeline == .home else { return } guard timeline == .home else { return }
if let event = event as? StreamEventUpdate { 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 { } else if let event = event as? StreamEventDelete {
statuses.removeAll(where: { $0.id == event.status }) statuses.removeAll(where: { $0.id == event.status })
statusesState = .display(statuses: statuses, nextPageState: .hasNextPage) statusesState = .display(statuses: statuses, nextPageState: .hasNextPage)