Skip to content

Commit 58c67b1

Browse files
committed
Presentable section behaves as a presentables array
1 parent 7e738a7 commit 58c67b1

File tree

9 files changed

+83
-14
lines changed

9 files changed

+83
-14
lines changed

Example/Presentables/CollectionDataManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class CollectionDataManager: PresentableCollectionViewDataManager {
2525
}).cellSelected {
2626
print("Selected: \(i)")
2727
}
28-
section.presentables.append(presentable)
28+
section.append(presentable)
2929
}
3030

3131
data.append(section)

Example/Presentables/ManagerTableViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class ManagerTableViewController: PresentableTableViewController {
2727
}).cellSelected {
2828
print("First cell has been selected")
2929
}
30-
section.presentables.append(presentable)
30+
section.append(presentable)
3131

3232
data.append(section)
3333

Example/Presentables/TableDataManager.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ class TableDataManager: PresentableTableViewDataManager {
2929
}).cellSelected {
3030
print("First cell has been selected")
3131
}
32-
section.presentables.append(presentable)
32+
section.append(presentable)
3333

3434
for i in 2...51 {
3535
let presentable = Presentable<TableViewCell2>.create({ (cell) in
3636
cell.textLabel?.text = "Id: \((i))"
3737
})
38-
section.presentables.append(presentable)
38+
section.append(presentable)
3939
}
4040

4141
data.append(section)

Presentables.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'Presentables'
11-
s.version = '0.6.3'
11+
s.version = '0.6.4'
1212
s.summary = 'Simple reactive library for managing table views & collection views, written in Swift'
1313

1414
# This description is used to generate tags and improve search results.

Presentables/Classes/Collection views/PresentableCollectionViewDataManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ open class PresentableCollectionViewDataManager: NSObject, PresentableManager, U
5353
}
5454

5555
open func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
56-
return data[section].presentables.count
56+
return data[section].count
5757
}
5858

5959
open func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

Presentables/Classes/Helpers/Array+Presentables.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public extension Array where Element == PresentableType {
1515
public var section: PresentableSection {
1616
get {
1717
let section = PresentableSection()
18-
section.presentables = self
18+
section._presentables = self
1919
return section
2020
}
2121
}
@@ -29,7 +29,7 @@ public extension Array where Element: PresentableSectionMarker {
2929

3030
func presentable(forIndexPath indexPath: IndexPath) -> PresentableType {
3131
let sections: PresentableSections = sectionsOrError()
32-
return sections[indexPath.section].presentables[indexPath.row]
32+
return sections[indexPath.section][indexPath.row]
3333
}
3434

3535
func section(forIndexPath indexPath: IndexPath) -> PresentableSection {

Presentables/Classes/Libs/PresentableSection.swift

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public protocol PresentableSectionMarker {
2121

2222
public typealias PresentableSectionMarkers = [PresentableSectionMarker]
2323

24-
public class PresentableSection: PresentableSectionMarker {
24+
public class PresentableSection: PresentableSectionMarker, Collection {
2525

2626
public enum Animation {
2727
case none
@@ -52,7 +52,18 @@ public class PresentableSection: PresentableSectionMarker {
5252
}
5353

5454
public var presenterAnimation: Animation = .none
55+
56+
@available(*, deprecated, message: "Use section.append(Presentable) directly")
5557
public var presentables: [PresentableType] {
58+
get {
59+
return _presentables
60+
}
61+
set {
62+
_presentables = newValue
63+
}
64+
}
65+
66+
var _presentables: [PresentableType] {
5667
get {
5768
return bindablePresenters.value ?? []
5869
}
@@ -61,6 +72,64 @@ public class PresentableSection: PresentableSectionMarker {
6172
}
6273
}
6374

75+
// MARK: Access
76+
77+
public var startIndex: Int {
78+
get {
79+
return 0
80+
}
81+
}
82+
83+
public var endIndex: Int {
84+
get {
85+
return _presentables.isEmpty ? 0 : (_presentables.count - 1)
86+
}
87+
}
88+
89+
public func index(after i: Int) -> Int {
90+
if i >= count {
91+
fatalError("Presentable array index is out of bounds")
92+
}
93+
return i + 1
94+
}
95+
96+
public subscript(index: Int) -> PresentableType {
97+
get {
98+
return _presentables[index]
99+
}
100+
set(newValue) {
101+
_presentables[index] = newValue
102+
}
103+
}
104+
105+
public func index(where predicate: (PresentableType) throws -> Bool) rethrows -> Int? {
106+
return try _presentables.index(where: predicate)
107+
}
108+
109+
public func append(_ presentable: PresentableType) {
110+
_presentables.append(presentable)
111+
}
112+
113+
public func removeAll() {
114+
_presentables.removeAll()
115+
}
116+
117+
public func insert(_ presentable: PresentableType, at index: Int) {
118+
_presentables.insert(presentable, at: index)
119+
}
120+
121+
@discardableResult public func remove(at index: Int) -> PresentableType {
122+
return _presentables.remove(at: index)
123+
}
124+
125+
public var count: Int {
126+
return _presentables.count
127+
}
128+
129+
public var isEmpty: Bool {
130+
return _presentables.isEmpty
131+
}
132+
64133
// MARK: Initialization
65134

66135
public init() { }

Presentables/Classes/Table views/PresentableTableViewDataManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ open class PresentableTableViewDataManager: NSObject, PresentableManager, UITabl
5151
}
5252

5353
open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
54-
return data[section].presentables.count
54+
return data[section].count
5555
}
5656

5757
open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class TableDataManager: PresentableTableViewDataManager {
7676
let presentable = Presentable<MyTableViewCell>.create({ (cell) in
7777
cell.textLabel?.text = "First cell"
7878
})
79-
section.presentables.append(presentable)
79+
section.append(presentable)
8080

8181
// Now add your section to the data source
8282
data.append(section)
@@ -137,14 +137,14 @@ class TableDataManager: PresentableTableViewDataManager {
137137
}).cellSelected {
138138
print("First cell has been selected")
139139
}
140-
section.presentables.append(presentable)
140+
section.append(presentable)
141141

142142
// And add loads more different cells
143143
for i in 2...51 {
144144
let presentable = Presentable<TableViewCell2>.create({ (cell) in
145145
cell.textLabel?.text = "Id: \((i))"
146146
})
147-
section.presentables.append(presentable)
147+
section.append(presentable)
148148
}
149149

150150
// Now add your section to the data source
@@ -189,7 +189,7 @@ class ManagerTableViewController: PresentableTableViewController {
189189
let presentable = Presentable<TableViewCell2>.create({ (cell) in
190190
cell.textLabel?.text = "Custom cell"
191191
})
192-
section.presentables.append(presentable)
192+
section.append(presentable)
193193

194194
data.append(section)
195195
}

0 commit comments

Comments
 (0)