Skip to content

Commit

Permalink
Presentable section behaves as a presentables array
Browse files Browse the repository at this point in the history
  • Loading branch information
rafiki270 committed Apr 27, 2018
1 parent 7e738a7 commit 58c67b1
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Example/Presentables/CollectionDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CollectionDataManager: PresentableCollectionViewDataManager {
}).cellSelected {
print("Selected: \(i)")
}
section.presentables.append(presentable)
section.append(presentable)
}

data.append(section)
Expand Down
2 changes: 1 addition & 1 deletion Example/Presentables/ManagerTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ManagerTableViewController: PresentableTableViewController {
}).cellSelected {
print("First cell has been selected")
}
section.presentables.append(presentable)
section.append(presentable)

data.append(section)

Expand Down
4 changes: 2 additions & 2 deletions Example/Presentables/TableDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ class TableDataManager: PresentableTableViewDataManager {
}).cellSelected {
print("First cell has been selected")
}
section.presentables.append(presentable)
section.append(presentable)

for i in 2...51 {
let presentable = Presentable<TableViewCell2>.create({ (cell) in
cell.textLabel?.text = "Id: \((i))"
})
section.presentables.append(presentable)
section.append(presentable)
}

data.append(section)
Expand Down
2 changes: 1 addition & 1 deletion Presentables.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

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

# This description is used to generate tags and improve search results.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ open class PresentableCollectionViewDataManager: NSObject, PresentableManager, U
}

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

open func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
Expand Down
4 changes: 2 additions & 2 deletions Presentables/Classes/Helpers/Array+Presentables.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public extension Array where Element == PresentableType {
public var section: PresentableSection {
get {
let section = PresentableSection()
section.presentables = self
section._presentables = self
return section
}
}
Expand All @@ -29,7 +29,7 @@ public extension Array where Element: PresentableSectionMarker {

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

func section(forIndexPath indexPath: IndexPath) -> PresentableSection {
Expand Down
71 changes: 70 additions & 1 deletion Presentables/Classes/Libs/PresentableSection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public protocol PresentableSectionMarker {

public typealias PresentableSectionMarkers = [PresentableSectionMarker]

public class PresentableSection: PresentableSectionMarker {
public class PresentableSection: PresentableSectionMarker, Collection {

public enum Animation {
case none
Expand Down Expand Up @@ -52,7 +52,18 @@ public class PresentableSection: PresentableSectionMarker {
}

public var presenterAnimation: Animation = .none

@available(*, deprecated, message: "Use section.append(Presentable) directly")
public var presentables: [PresentableType] {
get {
return _presentables
}
set {
_presentables = newValue
}
}

var _presentables: [PresentableType] {
get {
return bindablePresenters.value ?? []
}
Expand All @@ -61,6 +72,64 @@ public class PresentableSection: PresentableSectionMarker {
}
}

// MARK: Access

public var startIndex: Int {
get {
return 0
}
}

public var endIndex: Int {
get {
return _presentables.isEmpty ? 0 : (_presentables.count - 1)
}
}

public func index(after i: Int) -> Int {
if i >= count {
fatalError("Presentable array index is out of bounds")
}
return i + 1
}

public subscript(index: Int) -> PresentableType {
get {
return _presentables[index]
}
set(newValue) {
_presentables[index] = newValue
}
}

public func index(where predicate: (PresentableType) throws -> Bool) rethrows -> Int? {
return try _presentables.index(where: predicate)
}

public func append(_ presentable: PresentableType) {
_presentables.append(presentable)
}

public func removeAll() {
_presentables.removeAll()
}

public func insert(_ presentable: PresentableType, at index: Int) {
_presentables.insert(presentable, at: index)
}

@discardableResult public func remove(at index: Int) -> PresentableType {
return _presentables.remove(at: index)
}

public var count: Int {
return _presentables.count
}

public var isEmpty: Bool {
return _presentables.isEmpty
}

// MARK: Initialization

public init() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ open class PresentableTableViewDataManager: NSObject, PresentableManager, UITabl
}

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

open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class TableDataManager: PresentableTableViewDataManager {
let presentable = Presentable<MyTableViewCell>.create({ (cell) in
cell.textLabel?.text = "First cell"
})
section.presentables.append(presentable)
section.append(presentable)

// Now add your section to the data source
data.append(section)
Expand Down Expand Up @@ -137,14 +137,14 @@ class TableDataManager: PresentableTableViewDataManager {
}).cellSelected {
print("First cell has been selected")
}
section.presentables.append(presentable)
section.append(presentable)

// And add loads more different cells
for i in 2...51 {
let presentable = Presentable<TableViewCell2>.create({ (cell) in
cell.textLabel?.text = "Id: \((i))"
})
section.presentables.append(presentable)
section.append(presentable)
}

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

data.append(section)
}
Expand Down

0 comments on commit 58c67b1

Please sign in to comment.