|
| 1 | +import UIKit |
| 2 | +import Composed |
| 3 | +import ComposedUI |
| 4 | +import ComposedLayouts |
| 5 | + |
| 6 | +open class MediaViewController: UIViewController { |
| 7 | + |
| 8 | + open private(set) lazy var collectionView: UICollectionView = { |
| 9 | + return UICollectionView(frame: .zero, collectionViewLayout: layout) |
| 10 | + }() |
| 11 | + |
| 12 | + open private(set) lazy var collectionCoordinator: CollectionCoordinator = { |
| 13 | + #warning("Replace with appropriate provider when available") |
| 14 | + let provider = ComposedSectionProvider() |
| 15 | + return CollectionCoordinator(collectionView: collectionView, sectionProvider: provider) |
| 16 | + }() |
| 17 | + |
| 18 | + private lazy var layout: UICollectionViewLayout = { |
| 19 | + let layout: UICollectionViewLayout |
| 20 | + |
| 21 | + if #available(iOS 13.0, *) { |
| 22 | + return UICollectionViewCompositionalLayout { [weak self] index, environment in |
| 23 | + guard let self = self, |
| 24 | + self.collectionCoordinator.sectionProvider.sections.indices.contains(index), |
| 25 | + let section = self.collectionCoordinator.sectionProvider.sections[index] as? CompositionalLayoutHandler else { return nil } |
| 26 | + return section.compositionalLayoutSection(environment: environment) |
| 27 | + } |
| 28 | + } else { |
| 29 | + return UICollectionViewFlowLayout() |
| 30 | + } |
| 31 | + }() |
| 32 | + |
| 33 | + open override func viewDidLoad() { |
| 34 | + super.viewDidLoad() |
| 35 | + |
| 36 | + if #available(iOS 13.0, *) { |
| 37 | + collectionView.backgroundColor = .systemBackground |
| 38 | + } else { |
| 39 | + collectionView.backgroundColor = .groupTableViewBackground |
| 40 | + } |
| 41 | + |
| 42 | + collectionView.alwaysBounceVertical = true |
| 43 | + collectionView.setCollectionViewLayout(layout, animated: false) |
| 44 | + collectionView.frame = view.bounds |
| 45 | + collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight] |
| 46 | + view.addSubview(collectionView) |
| 47 | + } |
| 48 | + |
| 49 | + /// Required specifically for `UICollectionViewFlowLayout` since some rotations don't cause an invalidation!? |
| 50 | + open override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { |
| 51 | + super.viewWillTransition(to: size, with: coordinator) |
| 52 | + self.collectionCoordinator.invalidateLayout() |
| 53 | + } |
| 54 | + |
| 55 | +} |
0 commit comments