Skip to content

Commit 1cb3c31

Browse files
Ondrej RafajOndrej Rafaj
authored andcommitted
fixing data set binding + cleaning
1 parent 705e2a9 commit 1cb3c31

File tree

8 files changed

+144
-117
lines changed

8 files changed

+144
-117
lines changed

Example/Pods/Pods.xcodeproj/project.pbxproj

Lines changed: 18 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/Presentables/DataController.swift

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,32 @@ import Presentables
1212

1313
class DataController: PresentableTableViewDataManager {
1414

15+
// MARK: Initialization
16+
17+
override init() {
18+
super.init()
19+
20+
loadBasicData()
21+
22+
// Add new section every couple of seconds
23+
Timer.scheduledTimer(withTimeInterval: 10, repeats: true) { (timer) in
24+
self.loadBasicData()
25+
}
26+
}
27+
28+
// MARK: Overriding table view delegate
29+
30+
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
31+
return 44
32+
}
1533

16-
// MARK: Data
34+
}
35+
36+
// MARK: - Data
37+
38+
extension DataController {
1739

18-
private func newPresenter(_ i: Int) -> MyCellPresenter {
40+
fileprivate func newPresenter(_ i: Int) -> MyCellPresenter {
1941
let presenter = MyCellPresenter()
2042
presenter.configure = { presentable in
2143
guard let cell = presentable as? MyCell else {
@@ -26,7 +48,7 @@ class DataController: PresentableTableViewDataManager {
2648
return presenter
2749
}
2850

29-
private func newHeader(_ i: Int) -> MyHeaderPresenter {
51+
fileprivate func newHeader(_ i: Int) -> MyHeaderPresenter {
3052
let header = MyHeaderPresenter()
3153
header.configure = { presentable in
3254
guard let header = presentable as? MyHeader else {
@@ -37,7 +59,7 @@ class DataController: PresentableTableViewDataManager {
3759
return header
3860
}
3961

40-
private func loadBasicData() {
62+
fileprivate func loadBasicData() {
4163
let section = PresentableSection()
4264

4365
section.header = newHeader((data.count + 1))
@@ -58,23 +80,4 @@ class DataController: PresentableTableViewDataManager {
5880
data.append(section)
5981
}
6082

61-
// MARK: Initialization
62-
63-
override init() {
64-
super.init()
65-
66-
loadBasicData()
67-
68-
// Add new section every couple of seconds
69-
Timer.scheduledTimer(withTimeInterval: 10, repeats: true) { (timer) in
70-
self.loadBasicData()
71-
}
72-
}
73-
74-
// MARK: Overriding table view delegate
75-
76-
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
77-
return 44
78-
}
79-
8083
}

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.1.0'
11+
s.version = '0.1.1'
1212
s.summary = 'Simple reactive library written in Swift'
1313

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

Presentables/Classes/Presenters/UITableViewCell+Presenter.swift

Lines changed: 0 additions & 9 deletions
This file was deleted.

Presentables/Classes/Protocols/Presentable.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,4 @@
99
import Foundation
1010

1111

12-
public protocol Presentable: Identifiable {
13-
14-
}
12+
public protocol Presentable: Identifiable { }

Presentables/Classes/Protocols/Presenter.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,9 @@ public protocol Presenter: Configurable {
1616

1717
}
1818

19-
extension Presenter {
20-
21-
}
19+
extension Presenter { }
2220

2321

2422
public protocol PresenterHeaderFooter: Presenter { }
25-
2623
public protocol PresenterHeader: PresenterHeaderFooter { }
27-
2824
public protocol PresenterFooter: PresenterHeaderFooter { }

Presentables/Classes/Table views/Presentable+UITableView.swift

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -66,48 +66,9 @@ extension UITableView: PresentableCollectionElement {
6666
}
6767
if section.bindablePresenters.listener == nil {
6868
section.bindablePresenters.bind(listener: { (presenters) in
69-
if section.presenterAnimation == .none {
70-
self.reloadData()
71-
}
72-
else {
73-
//self.insertAndDeleteCellsForObjects(objects: presenters, originalObjects: presenters)
74-
}
69+
self.reloadData()
7570
})
7671
}
7772
}
7873

7974
}
80-
81-
extension UITableView {
82-
83-
func arrayInsertionDeletionAndNoopIndexes<T: Equatable>(objects: [T], originalObjects: [T]) -> ([Int], [Int], [Int]) {
84-
let insertions = objects.filter({ !originalObjects.contains($0) }).map({ objects.index(of: $0)! })
85-
let noops = originalObjects.filter({ objects.contains($0) }).map({ originalObjects.index(of: $0)! })
86-
let deletions = originalObjects.filter({ !objects.contains($0) }).map({ originalObjects.index(of: $0)! })
87-
88-
return (insertions, deletions, noops)
89-
}
90-
91-
func arrayInsertionDeletionAndNoopIndexPaths<T: Equatable>(objects: [T], originalObjects: [T], section: Int = 0) -> ([IndexPath], [IndexPath], [IndexPath]) {
92-
let (insertions, deletions, noops) = arrayInsertionDeletionAndNoopIndexes(objects: objects, originalObjects: originalObjects)
93-
94-
let insertionIndexPaths = insertions.map({ IndexPath(row: $0, section: section) })
95-
let deletionIndexPaths = deletions.map({ IndexPath(row: $0, section: section) })
96-
let noopIndexPaths = noops.map({ IndexPath(row: $0, section: section) })
97-
98-
return (insertionIndexPaths, deletionIndexPaths, noopIndexPaths)
99-
}
100-
101-
func insertAndDeleteCellsForObjects<T: Equatable>(objects: [T], originalObjects: [T], section: Int = 0) {
102-
let (insertions, deletions, _) = arrayInsertionDeletionAndNoopIndexPaths(objects: objects, originalObjects: originalObjects, section: section)
103-
104-
if insertions.count > 0 || deletions.count > 0 {
105-
beginUpdates()
106-
insertRows(at: insertions, with: .automatic)
107-
deleteRows(at: deletions, with: .automatic)
108-
endUpdates()
109-
}
110-
}
111-
112-
}
113-

README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,103 @@ it, simply add the following line to your Cartfile:
2727
```ruby
2828
github "manGoweb/Presentables"
2929
```
30+
## Usage
3031

32+
Create your cell (or header, implementation is pretty much the same)
33+
34+
```Swift
35+
import UIKit
36+
import Presentables
37+
38+
39+
class MyCell: UITableViewCell, Presentable {
40+
41+
}
42+
43+
class MyCellPresenter: Presenter {
44+
45+
var presentable: Presentable.Type = MyCell.self
46+
47+
var configure: ((Presentable) -> ())?
48+
49+
}
50+
```
51+
52+
Create a data manager
53+
54+
```Swift
55+
class MyDataManagager: PresentableTableViewDataManager {
56+
57+
// MARK: Initialization
58+
59+
override init() {
60+
super.init()
61+
62+
// Start a new section
63+
let section = PresentableSection()
64+
65+
// Every section needs a header ... obviously
66+
let header = MyHeaderPresenter()
67+
header.configure = { presentable in
68+
guard let header = presentable as? MyHeader else {
69+
return
70+
}
71+
header.titleLabel.text = "My section"
72+
}
73+
74+
section.header = header
75+
76+
// Create some basic cells
77+
for i: Int in 1...5 {
78+
let presenter = MyCellPresenter()
79+
presenter.configure = { presentable in
80+
guard let cell = presentable as? MyCell else {
81+
return
82+
}
83+
cell.textLabel?.text = "Cell number: \(i)"
84+
}
85+
section.presenters.append(presenter)
86+
}
87+
88+
// Add section to the data set
89+
data.append(section)
90+
}
91+
92+
// Override any delegate or datasource method you may want to
93+
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
94+
return 44
95+
}
96+
97+
}
98+
```
99+
100+
And lastly, bind your table view to your data manager
101+
102+
```Swift
103+
import UIKit
104+
import Presentables
105+
106+
107+
class ViewController: UITableViewController {
108+
109+
let dataController = MyDataManagager()
110+
111+
112+
// MARK: View lifecycle
113+
114+
override func viewDidLoad() {
115+
super.viewDidLoad()
116+
117+
title = "Presentables!"
118+
119+
var dc: PresentableManager = dataController
120+
tableView.bind(withPresentableManager: &dc)
121+
}
122+
123+
}
124+
```
125+
126+
And that's all folks!
31127

32128
## Author
33129

0 commit comments

Comments
 (0)