Skip to content

Commit 3b18330

Browse files
committed
Pause navbar observer when view is not visible
1 parent c22901d commit 3b18330

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

WooCommerce/Classes/ViewRelated/Dashboard/DashboardViewController.swift

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ final class DashboardViewController: UIViewController {
106106
private let viewModel: DashboardViewModel = .init()
107107

108108
private var subscriptions = Set<AnyCancellable>()
109+
private var navbarObserverSubscription: AnyCancellable?
109110

110111
// MARK: View Lifecycle
111112

@@ -148,6 +149,11 @@ final class DashboardViewController: UIViewController {
148149
observeNavigationBarHeightForHeaderVisibility()
149150
}
150151

152+
override func viewWillDisappear(_ animated: Bool) {
153+
stopObservingNavigationBarHeightForHeaderVisibility()
154+
super.viewWillDisappear(animated)
155+
}
156+
151157
override func viewDidLayoutSubviews() {
152158
super.viewDidLayoutSubviews()
153159
dashboardUI?.view.frame = containerView.bounds
@@ -170,7 +176,9 @@ final class DashboardViewController: UIViewController {
170176
}
171177

172178
func showHeaderWithAnimation() {
173-
headerAnimator?.stopAnimation(true)
179+
if headerAnimator?.isRunning == true {
180+
headerAnimator?.stopAnimation(true)
181+
}
174182
headerAnimator = UIViewPropertyAnimator.runningPropertyAnimator(
175183
withDuration: Constants.animationDurationSeconds,
176184
delay: 0,
@@ -183,7 +191,9 @@ final class DashboardViewController: UIViewController {
183191
}
184192

185193
func hideHeaderWithAnimation() {
186-
headerAnimator?.stopAnimation(true)
194+
if headerAnimator?.isRunning == true {
195+
headerAnimator?.stopAnimation(true)
196+
}
187197
headerAnimator = UIViewPropertyAnimator.runningPropertyAnimator(
188198
withDuration: Constants.animationDurationSeconds,
189199
delay: 0,
@@ -634,19 +644,25 @@ private extension DashboardViewController {
634644
}
635645

636646
func observeNavigationBarHeightForHeaderVisibility() {
637-
navigationController?.navigationBar.publisher(for: \.frame, options: [.initial, .new])
647+
navbarObserverSubscription = navigationController?.navigationBar.publisher(for: \.frame, options: [.initial, .new])
638648
.map({ [collapsedNavigationBarHeight] rect in
639649
rect.height <= collapsedNavigationBarHeight
640650
}) // true if navigation bar is collapsed
641651
.removeDuplicates()
642652
.sink(receiveValue: { [weak self] navigationBarIsShort in
653+
guard let self else { return }
654+
643655
if navigationBarIsShort {
644-
self?.hideHeaderWithAnimation()
656+
self.hideHeaderWithAnimation()
645657
} else {
646-
self?.showHeaderWithAnimation()
658+
self.showHeaderWithAnimation()
647659
}
648660
})
649-
.store(in: &subscriptions)
661+
}
662+
663+
func stopObservingNavigationBarHeightForHeaderVisibility() {
664+
navbarObserverSubscription?.cancel()
665+
navbarObserverSubscription = nil
650666
}
651667

652668
var collapsedNavigationBarHeight: CGFloat {

0 commit comments

Comments
 (0)