Prevent large visible room ranges being computed when the scroll view's content size hasn't settled

This commit is contained in:
Stefan Ceriu
2023-08-23 12:25:39 +03:00
committed by Stefan Ceriu
parent fac84f471e
commit beeb5fc83c
@@ -80,15 +80,13 @@ struct HomeScreen: View {
updateVisibleRange() updateVisibleRange()
} }
.onChange(of: context.searchQuery) { _ in .onChange(of: context.searchQuery) { _ in
// Dispatch allows the view to update after changing the query updateVisibleRange()
DispatchQueue.main.async { updateVisibleRange() }
} }
.onReceive(scrollViewAdapter.scrollDirection) { direction in .onReceive(scrollViewAdapter.scrollDirection) { direction in
withAnimation(.elementDefault) { lastScrollDirection = direction } withAnimation(.elementDefault) { lastScrollDirection = direction }
} }
.onChange(of: context.viewState.visibleRooms) { _ in .onChange(of: context.viewState.visibleRooms) { _ in
// Dispatch gives the view a chance to update updateVisibleRange()
DispatchQueue.main.async { updateVisibleRange() }
} }
.scrollDismissesKeyboard(.immediately) .scrollDismissesKeyboard(.immediately)
.scrollDisabled(context.viewState.roomListMode == .skeletons) .scrollDisabled(context.viewState.roomListMode == .skeletons)
@@ -182,7 +180,14 @@ struct HomeScreen: View {
} }
} }
/// Often times the scroll view's content size isn't correct yet when this method is called e.g. when cancelling a search
/// Dispatch it with a delay to allow the UI to update and the computations to be correct
/// Once we move to iOS 17 we should remove all of this and use scroll anchors instead
private func updateVisibleRange() { private func updateVisibleRange() {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { updateVisibleRange() }
}
private func delayedUpdateVisibleRange() {
guard let scrollView = scrollViewAdapter.scrollView, guard let scrollView = scrollViewAdapter.scrollView,
context.viewState.visibleRooms.count > 0 else { context.viewState.visibleRooms.count > 0 else {
return return