Change collection view deselect on appear to happen alongside nav pop

This commit is contained in:
Shadowfacts 2022-12-28 15:01:21 -05:00
parent e4f1309e2d
commit 4f655bb80a
6 changed files with 33 additions and 29 deletions

View File

@ -8,13 +8,13 @@
import UIKit
class AccountListViewController: UIViewController {
class AccountListViewController: UIViewController, CollectionViewController {
typealias Item = String
private let mastodonController: MastodonController
private let accountIDs: [String]
private var collectionView: UICollectionView {
var collectionView: UICollectionView! {
view as! UICollectionView
}
private var dataSource: UICollectionViewDiffableDataSource<Section, Item>!
@ -61,9 +61,7 @@ class AccountListViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
collectionView.indexPathsForSelectedItems?.forEach {
collectionView.deselectItem(at: $0, animated: true)
}
clearSelectionOnAppear(animated: animated)
}
}

View File

@ -12,11 +12,11 @@ import Pachyderm
import CoreData
import WebURLFoundationExtras
class ExploreViewController: UIViewController, UICollectionViewDelegate {
class ExploreViewController: UIViewController, UICollectionViewDelegate, CollectionViewController {
weak var mastodonController: MastodonController!
private var collectionView: UICollectionView!
var collectionView: UICollectionView!
private var dataSource: UICollectionViewDiffableDataSource<Section, Item>!
private(set) var resultsController: SearchResultsViewController!
@ -95,15 +95,7 @@ class ExploreViewController: UIViewController, UICollectionViewDelegate {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Can't use UICollectionViewController's builtin version of this because it requires
// the collection view layout be passed into the constructor. Swipe actions for list collection views
// are created by passing a closure to the layout's configuration. This closure needs to capture
// `self`, so it can't be passed into the super constructor.
if let indexPaths = collectionView.indexPathsForSelectedItems {
for indexPath in indexPaths {
collectionView.deselectItem(at: indexPath, animated: true)
}
}
clearSelectionOnAppear(animated: animated)
}
override func viewDidAppear(_ animated: Bool) {

View File

@ -10,7 +10,7 @@ import UIKit
import Pachyderm
import Combine
class ProfileStatusesViewController: UIViewController, TimelineLikeCollectionViewController {
class ProfileStatusesViewController: UIViewController, TimelineLikeCollectionViewController, CollectionViewController {
weak var owner: ProfileViewController?
let mastodonController: MastodonController
@ -188,9 +188,7 @@ class ProfileStatusesViewController: UIViewController, TimelineLikeCollectionVie
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
collectionView.indexPathsForSelectedItems?.forEach {
collectionView.deselectItem(at: $0, animated: true)
}
clearSelectionOnAppear(animated: animated)
Task {
if case .notLoadedInitial = controller.state {

View File

@ -9,7 +9,7 @@
import UIKit
import Pachyderm
class StatusActionAccountListViewController: UIViewController {
class StatusActionAccountListViewController: UIViewController, CollectionViewController {
private let mastodonController: MastodonController
private let actionType: ActionType
@ -20,8 +20,8 @@ class StatusActionAccountListViewController: UIViewController {
/// If `true`, a warning will be shown below the account list describing that the total favs/reblogs may be innacurate.
var showInacurateCountWarning = false
private var collectionView: UICollectionView {
view as! UICollectionView
var collectionView: UICollectionView! {
view as? UICollectionView
}
private var dataSource: UICollectionViewDiffableDataSource<Section, Item>!
@ -120,9 +120,7 @@ class StatusActionAccountListViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
collectionView.indexPathsForSelectedItems?.forEach {
collectionView.deselectItem(at: $0, animated: true)
}
clearSelectionOnAppear(animated: animated)
if accountIDs == nil {
Task {

View File

@ -210,9 +210,7 @@ class TimelineViewController: UIViewController, TimelineLikeCollectionViewContro
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
collectionView.indexPathsForSelectedItems?.forEach {
collectionView.deselectItem(at: $0, animated: true)
}
clearSelectionOnAppear(animated: animated)
if case .notLoadedInitial = controller.state {
if restoreState() {

View File

@ -11,3 +11,23 @@ import UIKit
protocol CollectionViewController: UIViewController {
var collectionView: UICollectionView! { get }
}
extension CollectionViewController {
func clearSelectionOnAppear(animated: Bool) {
guard let indexPath = collectionView.indexPathsForSelectedItems?.first else {
return
}
if let transitionCoordinator {
transitionCoordinator.animate { context in
self.collectionView.deselectItem(at: indexPath, animated: true)
} completion: { context in
if context.isCancelled {
self.collectionView.selectItem(at: indexPath, animated: false, scrollPosition: [] /* UICollectionViewScrollPositionNone */)
}
}
} else {
collectionView.deselectItem(at: indexPath, animated: animated)
}
}
}