Created
August 28, 2017 02:31
-
-
Save steve21124/dc8eea42170efb1babf22fd5d6669c4b 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 | |
import EasyPeasy | |
protocol ModelNew { | |
var title: String { get } | |
} | |
struct ModelM : ModelNew, Diffable { | |
var diffIdentifier: AnyHashable { | |
return AnyHashable(identity) | |
} | |
let identity: String | |
let title: String | |
} | |
final class Header : UICollectionReusableView { | |
let label = UILabel() | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
addSubview(label) | |
//label.frame = self.frame | |
label <- Edges(8) | |
label.textAlignment = .center | |
label.font = UIFont.boldSystemFont(ofSize: 20) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} | |
final class Cell : UICollectionViewCell { | |
let label = UILabel() | |
private let paddingView = UIView() | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
contentView.addSubview(paddingView) | |
contentView.addSubview(label) | |
label <- Center() | |
paddingView <- Edges(2) | |
//label.frame = self.frame | |
//label.center = self.center | |
//paddingView.frame = self.frame | |
label.textAlignment = .center | |
label.font = UIFont.systemFont(ofSize: 20) | |
paddingView.backgroundColor = UIColor(white: 0.95, alpha: 1) | |
paddingView.layer.cornerRadius = 4 | |
paddingView.layer.shouldRasterize = true | |
paddingView.layer.rasterizationScale = UIScreen.main.scale | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} | |
extension String{ | |
static func randomEmoji() -> String{ | |
let range = 0x1F601...0x1F64F | |
let ascii = range.lowerBound + Int(arc4random_uniform(UInt32(range.count))) | |
var view = UnicodeScalarView() | |
view.append(UnicodeScalar(ascii)!) | |
let emoji = String(view) | |
return emoji | |
} | |
} | |
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 }) | |
private let section1 = 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)) | |
}) | |
} | |
} | |
var section1Models: [ModelM] = [] { | |
didSet { | |
_dataSource.update( | |
in: section1, | |
items: section1Models, | |
updateMode: .partial(animated: true), | |
completion: { | |
print(self._dataSource.numberOfItems(in: 1)) | |
}) | |
} | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
collectionView.delegate = self | |
collectionView.dataSource = self | |
_dataSource.add(section: section0) | |
_dataSource.add(section: section1) | |
for _ in 0..<20 { | |
section0Models.append(ModelM(identity: UUID().uuidString, title: String.randomEmoji())) | |
} | |
for _ in 0..<20 { | |
section1Models.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)) | |
self.navigationController?.title = "This is title" | |
self.navigationController?.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(addTapped)) | |
} | |
@IBAction func AddItem(_ sender: Any) { | |
section0Models.append(ModelM(identity: UUID().uuidString, title: String.randomEmoji())) | |
} | |
@objc func addTapped() { | |
section0Models.append(ModelM(identity: UUID().uuidString, title: String.randomEmoji())) | |
} | |
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 | |
switch indexPath.section { | |
case 0: | |
let m = _dataSource.item(at: indexPath, in: section0) | |
cell.label.text = m.title | |
case 1: | |
let m = _dataSource.item(at: indexPath, in: section1) | |
cell.label.text = m.title | |
default: | |
cell.label.text = "" | |
} | |
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