Skip to content

Commit 4ff964a

Browse files
committed
simplify model items
1 parent e595e24 commit 4ff964a

4 files changed

Lines changed: 29 additions & 34 deletions

File tree

Sources/flex-data-source/FlexDataSourceProtocol.swift

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

@@ -87,7 +87,7 @@ extension FlexDataSourceProtocol {
8787
public func commitEditingStyleForRow(_ tableView: UITableView, editingStyle: UITableViewCell.EditingStyle, at indexPath: IndexPath) {
8888
if editingStyle == .delete {
8989
if let item = sections?[indexPath.section].items?[indexPath.row] as? Swipable {
90-
item.onSwipe()
90+
item.onSwipe?()
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
}

Sources/flex-data-source/Tappable.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 Tappable {
11-
var onTap: () -> Void { get }
11+
var onTap: (() -> Void)? { get }
1212
}
1313

1414
public func tap(on tappable: Tappable) {
15-
tappable.onTap()
15+
tappable.onTap?()
1616
}

0 commit comments

Comments
 (0)