This repository was archived by the owner on Nov 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathCollectionExampleController.swift
More file actions
123 lines (96 loc) · 3.08 KB
/
CollectionExampleController.swift
File metadata and controls
123 lines (96 loc) · 3.08 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//
// CollectionExampleController.swift
// ExampleApp
//
// Created by Daniele Margutti on 21/04/2018.
// Copyright © 2018 FlowKit. All rights reserved.
//
import UIKit
public struct Number: ModelProtocol {
public var modelID: Int {
return value
}
let value: Int
init(_ value: Int) {
self.value = value
}
}
public struct Letter: ModelProtocol {
public var modelID: Int {
return self.value.hashValue
}
let value: String
init(_ value: String) {
self.value = value
}
}
class CollectionExampleController: UIViewController {
@IBOutlet public var collectionView: UICollectionView?
private var director: FlowCollectionDirector?
override func viewDidLoad() {
super.viewDidLoad()
self.director = FlowCollectionDirector(self.collectionView!)
let letterAdapter = CollectionAdapter<Letter,LetterCell>()
self.director?.register(adapter: letterAdapter)
letterAdapter.on.dequeue = { ctx in
ctx.cell?.label?.text = "\(ctx.model)"
}
letterAdapter.on.didSelect = { ctx in
print("Tapped letter \(ctx.model)")
}
letterAdapter.on.itemSize = { ctx in
return CGSize.init(width: ctx.collectionSize!.width / 3.0, height: 100)
}
let numberAdapter = CollectionAdapter<Number,NumberCell>()
self.director?.register(adapter: numberAdapter)
numberAdapter.on.dequeue = { ctx in
ctx.cell?.label?.text = "#\(ctx.model)"
ctx.cell?.back?.layer.borderWidth = 2
ctx.cell?.back?.layer.borderColor = UIColor.darkGray.cgColor
ctx.cell?.back?.backgroundColor = UIColor.white
}
numberAdapter.on.didSelect = { ctx in
print("Tapped number \(ctx.model)")
}
numberAdapter.on.itemSize = { ctx in
return CGSize.init(width: ctx.collectionSize!.width / 3.0, height: 100)
}
var list: [ModelProtocol] = (0..<70).map { return Number($0) }
list.append(contentsOf: [Letter("A"),Letter("B"),Letter("C"),Letter("D"),Letter("E"),Letter("F")])
list.shuffle()
let header = CollectionSectionView<CollectionHeader>()
header.on.referenceSize = { _ in
return CGSize(width: self.collectionView!.frame.width, height: 40)
}
header.on.dequeue = { ctx in
ctx.view?.label?.text = "Header Title"
}
let section = CollectionSection.init(list, headerView: header)
self.director?.add(section)
self.director?.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
public class NumberCell: UICollectionViewCell {
@IBOutlet public var label: UILabel?
@IBOutlet public var back: UIView?
}
public class LetterCell: UICollectionViewCell {
@IBOutlet public var label: UILabel?
}
extension MutableCollection {
/// Shuffles the contents of this collection.
mutating func shuffle() {
let c = count
guard c > 1 else { return }
for (firstUnshuffled, unshuffledCount) in zip(indices, stride(from: c, to: 1, by: -1)) {
// Change `Int` in the next line to `IndexDistance` in < Swift 4.1
let d: Int = numericCast(arc4random_uniform(numericCast(unshuffledCount)))
let i = index(firstUnshuffled, offsetBy: d)
swapAt(firstUnshuffled, i)
}
}
}