Conversation
…ound Fix centeredItem() viewport center calculation that double-counted contentOffset. UIScrollView.bounds.origin equals contentOffset, so bounds.midX already includes the offset. Adding contentOffset again produced a center at 2*offset+width/2 instead of offset+width/2, biasing the nearest-item lookup toward incorrect pages when multiple items were visible during app state transitions. Also switch the willResignActive notification handler from Task to MainActor.assumeIsolated to eliminate the yield gap that allowed collection view state to change before the position was committed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Minimizing and reopening the app while reading in Divina scroll mode causes the reader to jump backward by 2 pages (or 1 page from even-numbered pages, then 2 on subsequent cycles).
Approach
The root cause is in
centeredItem()which calculates the viewport center to find the nearest page. ForUIScrollView,bounds.originequalscontentOffset, sobounds.midXalready includes the content offset. The old code addedcontentOffsetagain, producing a center at2 * contentOffset + width/2instead of the correctcontentOffset + width/2. This biased the nearest-item lookup toward wrong pages whenever multiple items were momentarily visible during app state transitions.Additionally, the
willResignActivenotification handler dispatched viaTask { @MainActor }which yields before executing, allowing the collection view state to change before the position was committed.Scope
centeredItem()center calculation to usebounds.midX/midYdirectly (matching the macOS implementation)Taskto synchronousMainActor.assumeIsolatedTesting