Skip to content

Commit 44ecf42

Browse files
committed
Add accessoryButtonAction to NavigationRow
1 parent 5e2eaad commit 44ecf42

2 files changed

Lines changed: 108 additions & 2 deletions

File tree

Source/Rows/NavigationRow.swift

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,28 @@ open class NavigationRow<T: UITableViewCell>: NavigationRowCompatible, Equatable
3131

3232
// MARK: - Initializer
3333

34+
#if os(iOS)
35+
36+
/// Initializes a `NavigationRow` with a text and a detail text.
37+
/// The icon, customization, action and accessory button action closures are optional.
38+
public init(
39+
text: String,
40+
detailText: DetailText,
41+
icon: Icon? = nil,
42+
customization: ((UITableViewCell, Row & RowStyle) -> Void)? = nil,
43+
action: ((Row) -> Void)? = nil,
44+
accessoryButtonAction: ((Row) -> Void)? = nil
45+
) {
46+
self.text = text
47+
self.detailText = detailText
48+
self.icon = icon
49+
self.customize = customization
50+
self.action = action
51+
self.accessoryButtonAction = accessoryButtonAction
52+
}
53+
54+
#elseif os(tvOS)
55+
3456
/// Initializes a `NavigationRow` with a text and a detail text.
3557
/// The icon, customization and action closures are optional.
3658
public init(
@@ -47,6 +69,8 @@ open class NavigationRow<T: UITableViewCell>: NavigationRowCompatible, Equatable
4769
self.action = action
4870
}
4971

72+
#endif
73+
5074
// MARK: - Row
5175

5276
/// The text of the row.
@@ -58,6 +82,13 @@ open class NavigationRow<T: UITableViewCell>: NavigationRowCompatible, Equatable
5882
/// A closure that will be invoked when the row is selected.
5983
public let action: ((Row) -> Void)?
6084

85+
#if os(iOS)
86+
87+
/// A closure that will be invoked when the accessory button is selected.
88+
public let accessoryButtonAction: ((Row) -> Void)?
89+
90+
#endif
91+
6192
// MARK: - RowStyle
6293

6394
/// The type of the table view cell to display the row.
@@ -76,9 +107,19 @@ open class NavigationRow<T: UITableViewCell>: NavigationRowCompatible, Equatable
76107
/// The icon of the row.
77108
public let icon: Icon?
78109

79-
/// Returns `.disclosureIndicator` when action is not nil, otherwise returns `.none`.
110+
/// Returns the accessory type with the disclosure indicator when `action` is not nil,
111+
/// and with the detail button when `accessoryButtonAction` is not nil.
80112
public var accessoryType: UITableViewCell.AccessoryType {
81-
return (action == nil) ? .none : .disclosureIndicator
113+
#if os(iOS)
114+
switch (action, accessoryButtonAction) {
115+
case (nil, nil): return .none
116+
case (.some, nil): return .disclosureIndicator
117+
case (nil, .some): return .detailButton
118+
case (.some, .some): return .detailDisclosureButton
119+
}
120+
#elseif os(tvOS)
121+
return (action == nil) ? .none : .disclosureIndicator
122+
#endif
82123
}
83124

84125
/// The `NavigationRow` is selectable when action is not nil.

Tests/Row/NavigationRowSpec.swift

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,39 @@ import Quick
3131
internal final class NavigationRowSpec: QuickSpec {
3232

3333
override func spec() {
34+
35+
#if os(iOS)
36+
describe("initialization") {
37+
let detailText = DetailText.subtitle("subtitle")
38+
let icon = Icon.named("icon")
39+
40+
var actionInvoked = false
41+
var accessoryButtonActionInvoked = false
42+
43+
let row = NavigationRow(
44+
text: "title",
45+
detailText: detailText,
46+
icon: icon,
47+
action: { _ in actionInvoked = true },
48+
accessoryButtonAction: { _ in accessoryButtonActionInvoked = true }
49+
)
50+
51+
it("should initialize with given parameters") {
52+
expect(row.text) == "title"
53+
expect(row.detailText) == detailText
54+
expect(row.icon) == icon
55+
expect(row.cellReuseIdentifier) == "UITableViewCell.subtitle"
56+
57+
row.action?(row)
58+
expect(actionInvoked) == true
59+
60+
row.accessoryButtonAction?(row)
61+
expect(accessoryButtonActionInvoked) == true
62+
}
63+
}
64+
65+
#elseif os(tvOS)
66+
3467
describe("initialization") {
3568
let detailText = DetailText.subtitle("subtitle")
3669
let icon = Icon.named("icon")
@@ -53,6 +86,8 @@ internal final class NavigationRowSpec: QuickSpec {
5386
}
5487
}
5588

89+
#endif
90+
5691
describe("cellReuseIdentifier") {
5792
let a = NavigationRow(text: "", detailText: .none)
5893
let b = NavigationRow(text: "", detailText: .subtitle(""))
@@ -67,6 +102,36 @@ internal final class NavigationRowSpec: QuickSpec {
67102
}
68103
}
69104

105+
#if os(iOS)
106+
107+
describe("accessoryType") {
108+
let a = NavigationRow(text: "", detailText: .none)
109+
let b = NavigationRow(text: "", detailText: .none, action: { _ in })
110+
let c = NavigationRow(text: "", detailText: .none, accessoryButtonAction: { _ in })
111+
let d = NavigationRow(text: "", detailText: .none, action: { _ in }, accessoryButtonAction: { _ in })
112+
113+
it("should return the corresponding accessory type") {
114+
expect(a.accessoryType) == UITableViewCell.AccessoryType.none
115+
expect(b.accessoryType) == UITableViewCell.AccessoryType.disclosureIndicator
116+
expect(c.accessoryType) == UITableViewCell.AccessoryType.detailButton
117+
expect(d.accessoryType) == UITableViewCell.AccessoryType.detailDisclosureButton
118+
}
119+
}
120+
121+
#elseif os(iOS)
122+
123+
describe("accessoryType") {
124+
let a = NavigationRow(text: "", detailText: .none)
125+
let b = NavigationRow(text: "", detailText: .none, action: { _ in })
126+
127+
it("should return the the corresponding accessory type") {
128+
expect(a.accessoryType) == UITableViewCell.AccessoryType.none
129+
expect(b.accessoryType) == UITableViewCell.AccessoryType.disclosureIndicator
130+
}
131+
}
132+
133+
#endif
134+
70135
describe("equatable") {
71136
let image = UIImage()
72137
let row = NavigationRow(text: "Same", detailText: .subtitle("Same"), icon: .image(image), action: nil)

0 commit comments

Comments
 (0)