forked from square/Listable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListView.LayoutManager.swift
55 lines (45 loc) · 2 KB
/
ListView.LayoutManager.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//
// ListView.LayoutManager.swift
// ListableUI
//
// Created by Kyle Van Essen on 5/3/20.
//
import Foundation
import UIKit
extension ListView
{
final class LayoutManager
{
unowned let collectionView : UICollectionView
private(set) var collectionViewLayout : CollectionViewLayout
init(layout collectionViewLayout : CollectionViewLayout, collectionView : UICollectionView)
{
self.collectionViewLayout = collectionViewLayout
self.collectionView = collectionView
}
func stateForItem(at indexPath: IndexPath) -> AnyPresentationItemState {
self.collectionViewLayout.layout.content.item(at: indexPath).state
}
func set(layout : LayoutDescription, animated : Bool, completion : @escaping () -> ())
{
if self.collectionViewLayout.layoutDescription.configuration.isSameLayoutType(as: layout.configuration) {
self.collectionViewLayout.layoutDescription = layout
let shouldRebuild = self.collectionViewLayout.layoutDescription.configuration.shouldRebuild(layout: self.collectionViewLayout.layout)
if shouldRebuild {
/// TODO: We shouldn't need to rebuild in any case here; just push the new values through to the ListLayout.
self.collectionViewLayout.setNeedsRebuild()
}
} else {
self.collectionViewLayout = CollectionViewLayout(
delegate: self.collectionViewLayout.delegate,
layoutDescription: layout,
appearance: self.collectionViewLayout.appearance,
behavior: self.collectionViewLayout.behavior
)
self.collectionView.setCollectionViewLayout(self.collectionViewLayout, animated: animated) { _ in
completion()
}
}
}
}
}