From 9f350d130574b42b1c5fe1bd68bc34a7de3f2ade Mon Sep 17 00:00:00 2001 From: Justin Mazzocchi <2831158+jzzocc@users.noreply.github.com> Date: Sat, 27 Feb 2021 11:40:06 -0800 Subject: [PATCH] Ensure table view is laid out properly --- View Controllers/TableViewController.swift | 32 ++++++++++++---------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/View Controllers/TableViewController.swift b/View Controllers/TableViewController.swift index c90a987..8f874d7 100644 --- a/View Controllers/TableViewController.swift +++ b/View Controllers/TableViewController.swift @@ -115,10 +115,6 @@ class TableViewController: UITableViewController { for loadMoreView in visibleLoadMoreViews { loadMoreView.directionChanged(up: up) } - - if newItemsView.alpha > 0 { - hideNewItemsView() - } } override func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) { @@ -183,14 +179,6 @@ class TableViewController: UITableViewController { sizeTableHeaderFooterViews() } - - override func accessibilityScroll(_ direction: UIAccessibilityScrollDirection) -> Bool { - if newItemsView.alpha > 0 { - hideNewItemsView() - } - - return super.accessibilityScroll(direction) - } } extension TableViewController { @@ -408,8 +396,20 @@ private extension TableViewController { .store(in: &cancellables) tableView.publisher(for: \.contentOffset) + .removeDuplicates() + .handleEvents(receiveOutput: { [weak self] _ in + guard let self = self else { return } + + if (self.newItemsView.layer.animationKeys() ?? []).isEmpty, self.newItemsView.alpha > 0 { + self.hideNewItemsView() + } + }) .compactMap { [weak self] _ in self?.tableView.indexPathsForVisibleRows?.first } - .sink { [weak self] in self?.viewModel.viewedAtTop(indexPath: $0) } + .sink { [weak self] in + guard let self = self else { return } + + self.viewModel.viewedAtTop(indexPath: $0) + } .store(in: &cancellables) NotificationCenter.default.publisher(for: UIScene.willEnterForegroundNotification) @@ -477,6 +477,8 @@ private extension TableViewController { } else if setPreviousOffset { self.tableView.contentOffset.y = preUpdateContentOffsetY } + + self.tableView.layoutIfNeeded() } } @@ -799,7 +801,7 @@ private extension TableViewController { self.newItemsViewVisibleConstraint?.isActive = true self.view.layoutIfNeeded() } completion: { _ in - + self.view.layoutIfNeeded() } } @@ -809,6 +811,8 @@ private extension TableViewController { self.newItemsViewHiddenConstraint?.isActive = true self.newItemsViewVisibleConstraint?.isActive = false self.view.layoutIfNeeded() + } completion: { _ in + self.view.layoutIfNeeded() } } }