mirror of
https://github.com/Dimillian/IceCubesApp.git
synced 2024-11-10 19:20:59 +00:00
Timeline: cleanup datasource near bottom
This commit is contained in:
parent
0b7c6a799f
commit
93421c56d9
3 changed files with 29 additions and 11 deletions
|
@ -57,7 +57,7 @@ import SwiftUI
|
|||
}
|
||||
|
||||
@ObservationIgnored
|
||||
private var visibileStatuses: [Status] = []
|
||||
private var visibleStatuses: [Status] = []
|
||||
|
||||
private var canStreamEvents: Bool = true {
|
||||
didSet {
|
||||
|
@ -301,6 +301,9 @@ extension TimelineViewModel: StatusesFetcher {
|
|||
private func updateTimelineWithNewStatuses(_ newStatuses: [Status]) async {
|
||||
let topStatus = await datasource.getFiltered().first
|
||||
await datasource.insert(contentOf: newStatuses, at: 0)
|
||||
if let lastVisible = visibleStatuses.last {
|
||||
await datasource.remove(after: lastVisible, safeOffset: 15)
|
||||
}
|
||||
await cache()
|
||||
pendingStatusesObserver.pendingStatuses.insert(contentsOf: newStatuses.map(\.id), at: 0)
|
||||
|
||||
|
@ -308,7 +311,7 @@ extension TimelineViewModel: StatusesFetcher {
|
|||
let nextPageState: StatusesState.PagingState = statuses.count < 20 ? .none : .hasNextPage
|
||||
|
||||
if let topStatus = topStatus,
|
||||
visibileStatuses.contains(where: { $0.id == topStatus.id }),
|
||||
visibleStatuses.contains(where: { $0.id == topStatus.id }),
|
||||
scrollToTopVisible
|
||||
{
|
||||
updateTimelineWithScrollToTop(newStatuses: newStatuses, statuses: statuses, nextPageState: nextPageState)
|
||||
|
@ -359,17 +362,17 @@ extension TimelineViewModel: StatusesFetcher {
|
|||
|
||||
func statusDidAppear(status: Status) {
|
||||
pendingStatusesObserver.removeStatus(status: status)
|
||||
visibileStatuses.insert(status, at: 0)
|
||||
visibleStatuses.insert(status, at: 0)
|
||||
|
||||
if let client, timeline.supportNewestPagination {
|
||||
Task {
|
||||
await cache.setLatestSeenStatuses(visibileStatuses, for: client, filter: timeline.id)
|
||||
await cache.setLatestSeenStatuses(visibleStatuses, for: client, filter: timeline.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func statusDidDisappear(status: Status) {
|
||||
visibileStatuses.removeAll(where: { $0.id == status.id })
|
||||
visibleStatuses.removeAll(where: { $0.id == status.id })
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -419,10 +422,11 @@ extension TimelineViewModel {
|
|||
}
|
||||
|
||||
private func handleDeleteEvent(_ event: StreamEventDelete) async {
|
||||
await datasource.remove(event.status)
|
||||
if let _ = await datasource.remove(event.status) {
|
||||
await cache()
|
||||
await updateStatusesState()
|
||||
}
|
||||
}
|
||||
|
||||
private func handleStatusUpdateEvent(_ event: StreamEventStatusUpdate, client: Client) async {
|
||||
guard let originalIndex = await datasource.indexOf(statusId: event.status.id) else { return }
|
||||
|
|
|
@ -68,11 +68,25 @@ actor TimelineDatasource {
|
|||
statuses.insert(contentsOf: contentOf, at: at)
|
||||
}
|
||||
|
||||
func remove(after: Status, safeOffset: Int) {
|
||||
if let index = statuses.firstIndex(of: after) {
|
||||
let safeIndex = index + safeOffset
|
||||
if statuses.count > safeIndex {
|
||||
statuses.removeSubrange(safeIndex..<statuses.endIndex)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func replace(_ status: Status, at: Int) {
|
||||
statuses[at] = status
|
||||
}
|
||||
|
||||
func remove(_ statusId: String) {
|
||||
statuses.removeAll(where: { $0.id == statusId })
|
||||
func remove(_ statusId: String) -> Status? {
|
||||
if let index = statuses.firstIndex(where: { status in
|
||||
status.id == statusId
|
||||
}) {
|
||||
return statuses.remove(at: index)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue