-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCollectionViewController.swift
98 lines (74 loc) · 3.12 KB
/
CollectionViewController.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//
// CollectionViewController.swift
// Mojave
//
// Created by Andrew Cavanagh on 8/28/16.
// Copyright © 2016 Andrew Cavanagh. All rights reserved.
//
import UIKit
import Mojave
private let reuseIdentifier = "Cell"
struct TestModel: DataSourceModel {
let color: UIColor
}
struct Section: DataSourceSection {
var items = [DataSourceModel]()
}
class CollectionViewController: UICollectionViewController, DataSourceDelegate {
var dataSource: DataSource
var dataSourceCollectionView: UICollectionView? {
return collectionView
}
init() {
let initialState = DataSourceState(sections: [Section()])
dataSource = DataSource(initialState: initialState)
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
let initialState = DataSourceState(sections: [Section()])
dataSource = DataSource(initialState: initialState)
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
dataSource.delegate = self
self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
var changeset = DataSourceChangeset.empty
let firstSectionItems = [TestModel(color: .blue),
TestModel(color: .red),
TestModel(color: .green),
TestModel(color: .orange),
TestModel(color: .purple)]
let newSection = Section(items: firstSectionItems)
changeset.append(insertedSections: [0 : newSection])
dataSource.apply(changeset: changeset)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return dataSource.state.numberOfSections
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dataSource.state.numberOfItems(in: section)
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
let item = dataSource.state.item(at: indexPath)
if let item = item as? TestModel {
cell.contentView.backgroundColor = item.color
} else {
fatalError("INVALID CELL")
}
return cell
}
func dataSource(_ dataSource: DataSource, didModify previousState: DataSourceState, with changes: DataSourceAppliedChanges) {}
}
extension CollectionViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 100, height: 100)
}
}