-
Notifications
You must be signed in to change notification settings - Fork 1
/
TableViewComponent.stencil
106 lines (87 loc) · 2.79 KB
/
TableViewComponent.stencil
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
import UIKit
{% for type in types.implementing.TableViewComponent %}
class {{type.name|replace:"View",""|replace:"Cell",""}}ViewController: UIViewController {
private let tableView: UITableView
private let dataSource = {{type.name|replace:"Cell",""|replace:"Cell",""}}DataSource()
init(tableView: UITableView? = nil) {
if let tableView = tableView {
self.tableView = tableView
} else {
self.tableView = UITableView(frame: .zero)
}
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - View lifecycle
override func loadView() {
self.view = tableView
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = dataSource
tableView.register({{type.name}}.self, forCellReuseIdentifier: "{{type.name}}")
}
// MARK: - Public API
func reload(with models: [{{type.name}}Model]) {
dataSource.reload(tableView, with: models)
}
}
class {{type.name|replace:"Cell",""}}DataSource: NSObject, UITableViewDataSource {
private var models = [{{type.name}}Model]()
init(models: [{{type.name}}Model] = []) {
self.models = models
super.init()
}
// MARK: - Public API
func model(at indexPath: IndexPath) -> {{type.name}}Model {
return models[indexPath.item]
}
func reload(_ tableView: UITableView,
with models: [{{type.name}}Model]) {
self.models = models
tableView.reloadData()
}
// MARK: - UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return models.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "{{type.name}}", for: indexPath)
let model = self.model(at: indexPath)
if let view = cell as? {{type.name}} {
{% for variable in type.variables %}
{% for key, value in variable.annotations %}
{% ifnot variable|annotated:"$RawBinding" %}
{% if value.length > 0 %}
view.{{key}} = {{value}}
{% else %}
{% for variableType in value %}
view.{{variable.annotations[key][variableType]}}
{% endfor %}
{% endif %}
{% else %}
{{ value }}
{% endif %}
{% endfor %}
{% endfor %}
}
return cell
}
}
struct {{type.name}}Model: Hashable {
{% for variable in type.variables %}
{% ifnot variable|annotated:"$RawBinding" %}
{% for key, value in variable.annotations %}
{% for variableType in variable.annotations[key] %}
{{key}}: {{variableType}}
{% endfor %}
{% endfor %}
{% endif %}
{% endfor %}
{% for key in type.annotations %}
{{key}}: {{type.annotations[key]}}
{% endfor %}
}
{% endfor %}