Add more safety to scroll to

This commit is contained in:
Thomas Ricouard 2023-02-05 08:13:38 +01:00
parent 7e88339dc5
commit b0630ae9c3
1 changed files with 7 additions and 4 deletions

View File

@ -62,10 +62,13 @@ public struct TimelineView: View {
}
}
.onChange(of: viewModel.scrollToIndex) { index in
if let index {
collectionView?.scrollToItem(at: .init(row: index, section: 0),
at: .top,
animated: viewModel.scrollToIndexAnimated)
if let collectionView,
let index,
let rows = collectionView.dataSource?.collectionView(collectionView, numberOfItemsInSection: 0),
rows > index {
collectionView.scrollToItem(at: .init(row: index, section: 0),
at: .top,
animated: viewModel.scrollToIndexAnimated)
viewModel.scrollToIndexAnimated = false
viewModel.scrollToIndex = nil
}