Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a debouncer to NavBars animator #3519

Merged
merged 3 commits into from
Nov 1, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions DuckDuckGo/BarsAnimator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,38 @@ class BarsAnimator {
}

private func transitioningAndScrolling(in scrollView: UIScrollView) {
let ratio = calculateTransitionRatio(for: scrollView.contentOffset.y)

// On iOS 18 we end up in a loop after setBarsVisibility.
// It seems to trigger a new didScrollEvent when rendering some PDF files
// That causes an infinite loop.
// Are viewDidScroll calls happening more often for PDF's on iOS 18?
// Adding a debouncer while we investigate further
// https://app.asana.com/0/1204099484721401/1208671955053442/f
let debounceDelay: TimeInterval = 0.01
struct Debounce {
static var workItem: DispatchWorkItem?
}
Debounce.workItem?.cancel()

if ratio == 1.0 {
barsState = .hidden
} else if ratio == 0 {
barsState = .revealed
} else if transitionProgress == ratio {
return
Debounce.workItem = DispatchWorkItem { [weak self] in
guard let self = self else { return }

let ratio = self.calculateTransitionRatio(for: scrollView.contentOffset.y)

if ratio == 1.0 {
self.barsState = .hidden
} else if ratio == 0 {
self.barsState = .revealed
} else if self.transitionProgress == ratio {
return
}

self.delegate?.setBarsVisibility(1.0 - ratio, animated: false)
self.transitionProgress = ratio
}

delegate?.setBarsVisibility(1.0 - ratio, animated: false)
transitionProgress = ratio
// Schedule the work item
DispatchQueue.main.asyncAfter(deadline: .now() + debounceDelay, execute: Debounce.workItem!)
}

private func hiddenAndScrolling(in scrollView: UIScrollView) {
Expand Down
Loading