apptekstudios / ASCollectionView

A SwiftUI collection view with support for custom layouts, preloading, and more.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ASCollectionView does not maintain scroll position when loading new batch on top

andrei1152 opened this issue · comments

Describe the bug
When inserting new data after scrolling to top, ASCollectionView refreshes the data and scrolls to the (new)top, instead of maintaining current scroll position.

To Reproduce

ASCollectionView(data: viewModel.messages) { (message, _) in
    MessageView(message: message, containerWidth: reader.size.width)
}
.layout { _ in .list(spacing: 10)}
.scrollPositionSetter($scrollPosition)
.onReachedBoundary({ (boundary) in
    if boundary == .top { viewModel.fetchMessages() }
})

viewModel.fetchMessages() looks something like this

func fetchMessages() {
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
        self.messages.insert(contentsOf: newDataBatch, at: 0)
    }
}

Expected behaviour
What I'm trying is to implement something similar to a chat like functionality, where you start at the
bottom and scroll upwards until you reach the top, then load a new batch of data, then scroll some more, and so on.
I expect that scrolling remains where I left off after loading the new batch of data.
Instead I get scrolled to the top of the newly fetched data.