Created
August 27, 2017 23:14
-
-
Save steve21124/4ef36081242b1f96d4d00ea6c0188f58 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import UIKit | |
import DataSources | |
protocol ModelNew { | |
var title: String { get } | |
} | |
struct ModelM : ModelNew, Diffable { | |
var diffIdentifier: AnyHashable { | |
return AnyHashable(identity) | |
} | |
let identity: String | |
let title: String | |
} | |
final class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { | |
var collectionView: UICollectionView = { | |
let layout = UICollectionViewFlowLayout() | |
layout.sectionInset = .zero | |
layout.minimumLineSpacing = 0 | |
layout.minimumInteritemSpacing = 0 | |
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) | |
collectionView.register(Cell.self, forCellWithReuseIdentifier: "Cell") | |
collectionView.register(Header.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "Header") | |
collectionView.backgroundColor = .white | |
return collectionView | |
}() | |
private lazy var _dataSource: DataController<CollectionViewAdapter> = .init(adapter: .init(collectionView: self.collectionView)) | |
private let section0 = Section(ModelM.self, isEqual: { $0.identity == $1.identity }) | |
var section0Models: [ModelM] = [] { | |
didSet { | |
_dataSource.update( | |
in: section0, | |
items: section0Models, | |
updateMode: .partial(animated: true), | |
completion: { | |
print(self._dataSource.numberOfItems(in: 0)) | |
}) | |
} | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
collectionView.delegate = self | |
collectionView.dataSource = self | |
_dataSource.add(section: section0) | |
for _ in 0..<20 { | |
section0Models.append(ModelM(identity: UUID().uuidString, title: String.randomEmoji())) | |
} | |
view.addSubview(collectionView) | |
collectionView.frame = self.view.bounds | |
collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight] | |
//This return zero items even though section0Models has 20 items | |
print(_dataSource.numberOfItems(in: 0)) | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
func numberOfSections(in collectionView: UICollectionView) -> Int { | |
return _dataSource.numberOfSections() | |
} | |
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
return _dataSource.numberOfItems(in: section) | |
} | |
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | |
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! Cell | |
let m = _dataSource.item(at: indexPath, in: section0) | |
cell.label.text = m.title | |
return cell | |
} | |
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { | |
if kind == UICollectionElementKindSectionHeader { | |
let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Header", for: indexPath) as! Header | |
view.label.text = "Section " + indexPath.section.description | |
return view | |
} | |
fatalError() | |
} | |
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { | |
return CGSize(width: collectionView.bounds.width, height: 50) | |
} | |
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { | |
return CGSize(width: collectionView.bounds.width / 6, height: 50) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment