Skip to content

Commit 36db19e

Browse files
committed
simplify model items
1 parent e595e24 commit 36db19e

11 files changed

Lines changed: 72 additions & 84 deletions

Example/Tests/FlexCollectionItemTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class FlexCollectionItemTests: XCTestCase {
3939
pressed = true
4040
}
4141
item.configureCell(cell)
42-
item.onTap()
42+
item.onTap?()
4343
XCTAssertEqual(cell.backgroundColor, .red)
4444
XCTAssertEqual(item.cellIdentifier(), cellID)
4545
XCTAssertTrue(pressed == true)

Example/Tests/FlexDataSourceItemTests.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import LithoOperators
1111
@testable import FlexDataSource
1212

1313
class FlexDataSourceItemTests: XCTestCase {
14-
1514
func testFunctionalFlexDataSourceItem() {
1615
let identifier = "TableViewCell"
1716
let item = FunctionalFlexDataSourceItem<UITableViewCell>(identifier: identifier, set(\UITableViewCell.backgroundColor, .red))
@@ -38,7 +37,7 @@ class FlexDataSourceItemTests: XCTestCase {
3837
pressed = true
3938
}
4039
item.configureCell(cell)
41-
item.onTap()
40+
item.onTap?()
4241
XCTAssertEqual(cell.backgroundColor, .red)
4342
XCTAssertEqual(item.cellIdentifier(), cellID)
4443
XCTAssertTrue(pressed)
@@ -51,8 +50,8 @@ class FlexDataSourceItemTests: XCTestCase {
5150
let cell = UITableViewCell()
5251
let item = SwipableItem<UITableViewCell>(identifier: cellID, set(\UITableViewCell.backgroundColor, .blue), { wasTapped = true }, {wasSwiped = true})
5352
item.configureCell(cell)
54-
item.onTap()
55-
item.onSwipe()
53+
item.onTap?()
54+
item.onSwipe?()
5655
XCTAssertEqual(cell.backgroundColor, .blue)
5756
XCTAssertEqual(item.cellIdentifier(), cellID)
5857
XCTAssertTrue(wasSwiped == true)

Example/Tests/FlexItemsTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ class FlexItemTests: XCTestCase {
3333
}
3434
let configurer: (Human, UITableViewCell) -> Void = setMainLabel
3535
var wasTapped: Bool = false
36-
let item = FlexTappableModelItem<Human, UITableViewCell>(model: Human(id: 123, name: "Calvin Collins"), configurer: configurer, tap: { _ in
36+
let item = FlexModelItem<Human, UITableViewCell>(model: Human(id: 123, name: "Calvin Collins"), configurer: configurer, tap: { _ in
3737
wasTapped = true
3838
})
39-
item.onTap()
39+
item.onTap?()
4040
XCTAssert(wasTapped)
4141
}
4242

@@ -47,13 +47,13 @@ class FlexItemTests: XCTestCase {
4747
let configurer: (Human, UITableViewCell) -> Void = setMainLabel
4848
var wasSwiped: Bool = false
4949
var wasTapped: Bool = false
50-
let item = FlexSwipeTapModelItem<Human, UITableViewCell>(model: Human(id: 123, name: "Calvin Collins"), configurer: configurer, tap: { _ in
50+
let item = FlexModelItem<Human, UITableViewCell>(model: Human(id: 123, name: "Calvin Collins"), configurer: configurer, tap: { _ in
5151
wasTapped = true
5252
}, swipe: { _ in
5353
wasSwiped = true
5454
})
55-
item.onSwipe()
56-
item.onTap()
55+
item.onSwipe?()
56+
item.onTap?()
5757
XCTAssert(wasTapped && wasSwiped)
5858
}
5959
}

Sources/flex-data-source/FlexCollectionDataSource.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public extension FlexCollectionDataSource {
8686
func tappableOnSelect(_ collectionView: UICollectionView, _ indexPath: IndexPath) -> Void {
8787
collectionView.deselectItem(at: indexPath, animated: true)
8888
if let tappable = sections?[indexPath.section].items?[indexPath.row] as? Tappable {
89-
tappable.onTap()
89+
tappable.onTap?()
9090
}
9191
}
9292

Sources/flex-data-source/FlexCollectionItem.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ open class FunctionalFlexCollectionItem<T>: ConcreteFlexCollectionItem<T> where
4747
}
4848

4949
open class TappableFlexCollectionItem<T>: FunctionalFlexCollectionItem<T>, Tappable where T: UICollectionViewCell {
50-
public var onTap: () -> Void
50+
public var onTap: (() -> Void)?
5151

5252
public init(identifier: String, _ configureCell: @escaping (UICollectionViewCell) -> Void, _ onTap: @escaping () -> Void) {
5353
self.onTap = onTap

Sources/flex-data-source/FlexCollectionItems.swift

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,62 +9,55 @@ import Foundation
99
import LithoOperators
1010
import Prelude
1111

12-
open class FlexModelCollectionItem<T, C>: ConcreteFlexCollectionItem<C> where C: UICollectionViewCell {
12+
open class FlexModelCollectionItem<T, C>: ConcreteFlexCollectionItem<C>, Tappable where C: UICollectionViewCell {
1313
open var model: T
1414
open var configurer: (C) -> Void
15+
public var onTap: (() -> Void)?
16+
public var onButtonPressed: (() -> Void)?
17+
public var gestureRecognizer: UIGestureRecognizer?
18+
public var onGesture: ((T, UIGestureRecognizer?) -> Void)?
1519

1620
public init(_ model: T, _ configurer: @escaping (T, C) -> Void) {
1721
self.model = model
1822
self.configurer = model *-> configurer
1923
super.init(identifier: String(describing: C.self))
2024
}
2125

22-
override open func configureCell(_ cell: UICollectionViewCell) {
23-
if let cell = cell as? C {
24-
configurer(cell)
25-
}
26-
}
27-
}
28-
29-
open class FlexTappableModelCollectionItem<T, C>: FlexModelCollectionItem<T, C>, Tappable where C: UICollectionViewCell {
30-
public var onTap: () -> Void = {}
31-
3226
public init(model: T, configurer: @escaping (T, C) -> Void, tap: @escaping (T) -> Void) {
3327
self.onTap = voidCurry(model, tap)
34-
super.init(model, configurer)
28+
self.model = model
29+
self.configurer = model *-> configurer
30+
super.init(identifier: String(describing: C.self))
3531
}
36-
}
37-
38-
open class FlexButtonTappableModelCollectionItem<T, C>: FlexTappableModelCollectionItem<T, C> where C: UICollectionViewCell {
39-
public var onButtonPressed: () -> Void
4032

4133
public init(model: T,
4234
configurer: @escaping (T, C) -> Void,
4335
tap: @escaping (T) -> Void,
4436
buttonPressed: @escaping () -> Void) {
4537
self.onButtonPressed = buttonPressed
46-
super.init(model: model, configurer: configurer, tap: tap)
38+
self.model = model
39+
self.configurer = model *-> configurer
40+
super.init(identifier: String(describing: C.self))
4741
}
4842

4943
public init(model: T,
5044
configurer: @escaping (T, C) -> Void,
5145
tap: @escaping (T) -> Void,
5246
buttonPressed: @escaping (T) -> Void) {
5347
self.onButtonPressed = voidCurry(model, buttonPressed)
54-
super.init(model: model, configurer: configurer, tap: tap)
48+
self.model = model
49+
self.configurer = model *-> configurer
50+
super.init(identifier: String(describing: C.self))
5551
}
56-
}
57-
58-
open class FlexGestureModelCollectionItem<T, C>: FlexModelCollectionItem<T, C> where C: UICollectionViewCell {
59-
public var gestureRecognizer: UIGestureRecognizer?
60-
public var onGesture: ((T, UIGestureRecognizer?) -> Void)?
6152

6253
override open func configureCell(_ cell: UICollectionViewCell) {
6354
if let recognizer = gestureRecognizer {
6455
cell.contentView.removeGestureRecognizer(recognizer)
6556
cell.contentView.addGestureRecognizer(recognizer)
6657
}
67-
super.configureCell(cell)
58+
if let cell = cell as? C {
59+
configurer(cell)
60+
}
6861
}
6962

7063
@objc open func gesturePerformed() {
@@ -76,7 +69,7 @@ public func modelItem<T, U: UICollectionViewCell>(_ configurer: @escaping (T, U)
7669
return configurer -*> FlexModelCollectionItem.init
7770
}
7871

79-
public func tappableModelItem<T, U: UICollectionViewCell>(_ configurer: @escaping (T, U) -> Void, onTap: @escaping (T) -> Void) -> (T) -> FlexTappableModelCollectionItem<T, U> {
80-
return (configurer, onTap) -**> FlexTappableModelCollectionItem.init
72+
public func tappableModelItem<T, U: UICollectionViewCell>(_ configurer: @escaping (T, U) -> Void, onTap: @escaping (T) -> Void) -> (T) -> FlexModelCollectionItem<T, U> {
73+
return (configurer, onTap) -**> FlexModelCollectionItem.init
8174
}
8275

Sources/flex-data-source/FlexDataSourceItem.swift

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
//
77

88
import UIKit
9+
import LithoOperators
910

1011
public protocol FlexDataSourceItem {
1112
func cellIdentifier() -> String
1213
func cellClass() -> UITableViewCell.Type
1314
func configureCell(_ cell: UITableViewCell)
1415
}
1516

16-
open class ConcreteFlexDataSourceItem<T>: FlexDataSourceItem where T: UITableViewCell {
17+
open class ConcreteFlexDataSourceItem<C>: FlexDataSourceItem where C: UITableViewCell {
1718
private let identifier: String
1819

1920
public init(identifier: String) {
@@ -25,15 +26,15 @@ open class ConcreteFlexDataSourceItem<T>: FlexDataSourceItem where T: UITableVie
2526
}
2627

2728
open func cellClass() -> UITableViewCell.Type {
28-
return T.self
29+
return C.self
2930
}
3031

3132
open func configureCell(_ cell: UITableViewCell) {
3233
// NO OP: override me!
3334
}
3435
}
3536

36-
open class FunctionalFlexDataSourceItem<T>: ConcreteFlexDataSourceItem<T> where T: UITableViewCell {
37+
open class FunctionalFlexDataSourceItem<C>: ConcreteFlexDataSourceItem<C> where C: UITableViewCell {
3738
private let configureCell: (UITableViewCell) -> Void
3839

3940
public init(identifier: String = "cell", _ configureCell: @escaping (UITableViewCell) -> Void) {
@@ -46,22 +47,22 @@ open class FunctionalFlexDataSourceItem<T>: ConcreteFlexDataSourceItem<T> where
4647
}
4748
}
4849

49-
open class TappableFunctionalFlexItem<T>: FunctionalFlexDataSourceItem<T>, Tappable where T: UITableViewCell {
50-
public var onTap: () -> Void
50+
open class TappableFunctionalFlexItem<C>: FunctionalFlexDataSourceItem<C>, Tappable where C: UITableViewCell {
51+
public var onTap: (() -> Void)?
5152

52-
public init(identifier: String, _ configureCell: @escaping (UITableViewCell) -> Void, _ onTap: @escaping () -> Void) {
53+
public init(identifier: String, _ configureCell: @escaping (UITableViewCell) -> Void, _ onTap: (() -> Void)?) {
5354
self.onTap = onTap
5455
super.init(identifier: identifier, configureCell)
5556
}
5657
}
5758

58-
open class SwipableItem<T>: TappableFunctionalFlexItem<T>, Swipable where T: UITableViewCell {
59-
public var onSwipe: () -> Void
59+
open class SwipableItem<C>: TappableFunctionalFlexItem<C>, Swipable where C: UITableViewCell {
60+
public var onSwipe: (() -> Void)?
6061

6162
public init(identifier: String,
6263
_ configureCell: @escaping (UITableViewCell) -> Void,
63-
_ onTap: @escaping () -> Void,
64-
_ onSwipe: @escaping () -> Void) {
64+
_ onTap: (() -> Void)?,
65+
_ onSwipe: (() -> Void)?) {
6566
self.onSwipe = onSwipe
6667
super.init(identifier: identifier, configureCell, onTap)
6768
}

Sources/flex-data-source/FlexDataSourceProtocol.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ public let deselectRow: (UITableView, IndexPath) -> Void = { $0.deselectRow(at:
5959
extension FlexDataSourceProtocol {
6060
public func tappableOnSelect(_ tableView: UITableView, _ indexPath: IndexPath) -> Void {
6161
deselectRow(tableView, indexPath)
62-
if let tappable = sections?[indexPath.section].items?[indexPath.row] as? Tappable {
63-
tappable.onTap()
62+
if let tappable = sections?[indexPath.section].items?[indexPath.row] as? Tappable, let tap = tappable.onTap {
63+
tap()
6464
}
6565
}
6666

@@ -86,8 +86,8 @@ extension FlexDataSourceProtocol {
8686

8787
public func commitEditingStyleForRow(_ tableView: UITableView, editingStyle: UITableViewCell.EditingStyle, at indexPath: IndexPath) {
8888
if editingStyle == .delete {
89-
if let item = sections?[indexPath.section].items?[indexPath.row] as? Swipable {
90-
item.onSwipe()
89+
if let item = sections?[indexPath.section].items?[indexPath.row] as? Swipable, let swipe = item.onSwipe {
90+
swipe()
9191
sections?[indexPath.section].items?.remove(at: indexPath.row)
9292
tableView.deleteRows(at: [indexPath], with: .fade)
9393
}

Sources/flex-data-source/FlexItems.swift

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,62 +9,57 @@ import Foundation
99
import LithoOperators
1010
import Prelude
1111

12-
open class FlexModelItem<T, C>: ConcreteFlexDataSourceItem<C> where C: UITableViewCell {
12+
open class FlexModelItem<T, C>: ConcreteFlexDataSourceItem<C>, Tappable, Swipable where C: UITableViewCell {
1313
open var model: T
1414
open var configurer: (C) -> Void
15+
public var onTap: (() -> Void)?
16+
public var onSwipe: (() -> Void)?
17+
public var gestureRecognizer: UIGestureRecognizer?
18+
public var onGesture: ((T, UIGestureRecognizer?) -> Void)?
1519

1620
public init(_ model: T, _ configurer: @escaping (T, C) -> Void) {
1721
self.model = model
1822
self.configurer = model *-> configurer
1923
super.init(identifier: String(describing: C.self))
2024
}
2125

22-
override open func configureCell(_ cell: UITableViewCell) {
23-
if let cell = cell as? C {
24-
configurer(cell)
25-
}
26-
}
27-
}
28-
29-
open class FlexTappableModelItem<T, C>: FlexModelItem<T, C>, Tappable where C: UITableViewCell {
30-
public var onTap: () -> Void = {}
31-
3226
public init(model: T, configurer: @escaping (T, C) -> Void, tap: @escaping (T) -> Void) {
3327
self.onTap = voidCurry(model, tap)
34-
super.init(model, configurer)
28+
self.model = model
29+
self.configurer = model *-> configurer
30+
super.init(identifier: String(describing: C.self))
3531
}
36-
}
37-
38-
open class FlexSwipeTapModelItem<T, C>: FlexTappableModelItem<T, C>, Swipable where C: UITableViewCell {
39-
public var onSwipe: () -> Void
4032

4133
public init(model: T,
4234
configurer: @escaping (T, C) -> Void,
4335
tap: @escaping (T) -> Void,
4436
swipe: @escaping () -> Void) {
4537
self.onSwipe = swipe
46-
super.init(model: model, configurer: configurer, tap: tap)
38+
self.onTap = voidCurry(model, tap)
39+
self.model = model
40+
self.configurer = model *-> configurer
41+
super.init(identifier: String(describing: C.self))
4742
}
4843

4944
public init(model: T,
5045
configurer: @escaping (T, C) -> Void,
5146
tap: @escaping (T) -> Void,
5247
swipe: @escaping (T) -> Void) {
5348
self.onSwipe = voidCurry(model, swipe)
54-
super.init(model: model, configurer: configurer, tap: tap)
49+
self.onTap = voidCurry(model, tap)
50+
self.model = model
51+
self.configurer = model *-> configurer
52+
super.init(identifier: String(describing: C.self))
5553
}
56-
}
57-
58-
open class FlexGestureModelItem<T, C>: FlexModelItem<T, C> where C: UITableViewCell {
59-
public var gestureRecognizer: UIGestureRecognizer?
60-
public var onGesture: ((T, UIGestureRecognizer?) -> Void)?
6154

6255
override open func configureCell(_ cell: UITableViewCell) {
6356
if let recognizer = gestureRecognizer {
6457
cell.contentView.removeGestureRecognizer(recognizer)
6558
cell.contentView.addGestureRecognizer(recognizer)
6659
}
67-
super.configureCell(cell)
60+
if let cell = cell as? C {
61+
configurer(cell)
62+
}
6863
}
6964

7065
@objc open func gesturePerformed() {
@@ -76,10 +71,10 @@ public func modelItem<T, U: UITableViewCell>(_ configurer: @escaping (T, U) -> V
7671
return configurer -*> FlexModelItem.init
7772
}
7873

79-
public func tappableModelItem<T, U: UITableViewCell>(_ configurer: @escaping (T, U) -> Void, onTap: @escaping (T) -> Void) -> (T) -> FlexTappableModelItem<T, U> {
80-
return (configurer, onTap) -**> FlexTappableModelItem.init
74+
public func tappableModelItem<T, U: UITableViewCell>(_ configurer: @escaping (T, U) -> Void, onTap: @escaping (T) -> Void) -> (T) -> FlexModelItem<T, U> {
75+
return (configurer, onTap) -**> FlexModelItem.init
8176
}
8277

83-
public func swipeTappableModelItem<T, U: UITableViewCell>(_ configurer: @escaping (T, U) -> Void, onTap: @escaping (T) -> Void, onSwipe: @escaping (T) -> Void) -> (T) -> FlexSwipeTapModelItem<T, U> {
84-
return (configurer, onTap, onSwipe) -***> FlexSwipeTapModelItem.init
78+
public func swipeTappableModelItem<T, U: UITableViewCell>(_ configurer: @escaping (T, U) -> Void, onTap: @escaping (T) -> Void, onSwipe: @escaping (T) -> Void) -> (T) -> FlexModelItem<T, U> {
79+
return (configurer, onTap, onSwipe) -***> FlexModelItem.init
8580
}

Sources/flex-data-source/Swipeable.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
import Foundation
99

1010
public protocol Swipable {
11-
var onSwipe: () -> Void { get }
11+
var onSwipe: (() -> Void)? { get }
1212
}
1313

1414
public func swipe(on swipable: Swipable) {
15-
swipable.onSwipe()
15+
swipable.onSwipe?()
1616
}

0 commit comments

Comments
 (0)