NavigationRow.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // NavigationRow.swift
  3. // QuickTableViewController
  4. //
  5. // Created by Ben on 01/09/2015.
  6. // Copyright (c) 2015 bcylin.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in all
  16. // copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. // SOFTWARE.
  25. //
  26. import UIKit
  27. /// A class that represents a row that triggers certain navigation when selected.
  28. open class NavigationRow<T: UITableViewCell>: NavigationRowCompatible, Equatable {
  29. // MARK: - Initializer
  30. #if os(iOS)
  31. /// Designated initializer on iOS. Returns a `NavigationRow` with a text and a detail text.
  32. /// The icon, customization, action and accessory button action closures are optional.
  33. public init(
  34. text: String,
  35. detailText: DetailText,
  36. icon: Icon? = nil,
  37. customization: ((UITableViewCell, Row & RowStyle) -> Void)? = nil,
  38. action: ((Row) -> Void)? = nil,
  39. accessoryButtonAction: ((Row) -> Void)? = nil
  40. ) {
  41. self.text = text
  42. self.detailText = detailText
  43. self.icon = icon
  44. self.customize = customization
  45. self.action = action
  46. self.accessoryButtonAction = accessoryButtonAction
  47. }
  48. #elseif os(tvOS)
  49. /// Designated initializer on tvOS. Returns a `NavigationRow` with a text and a detail text.
  50. /// The icon, customization and action closures are optional.
  51. public init(
  52. text: String,
  53. detailText: DetailText,
  54. icon: Icon? = nil,
  55. customization: ((UITableViewCell, Row & RowStyle) -> Void)? = nil,
  56. action: ((Row) -> Void)? = nil
  57. ) {
  58. self.text = text
  59. self.detailText = detailText
  60. self.icon = icon
  61. self.customize = customization
  62. self.action = action
  63. }
  64. #endif
  65. // MARK: - Row
  66. /// The text of the row.
  67. public let text: String
  68. /// The detail text of the row.
  69. public let detailText: DetailText?
  70. /// A closure that will be invoked when the row is selected.
  71. public let action: ((Row) -> Void)?
  72. #if os(iOS)
  73. /// A closure that will be invoked when the accessory button is selected.
  74. public let accessoryButtonAction: ((Row) -> Void)?
  75. #endif
  76. // MARK: - RowStyle
  77. /// The type of the table view cell to display the row.
  78. public let cellType: UITableViewCell.Type = T.self
  79. /// Returns the reuse identifier of the table view cell to display the row.
  80. public var cellReuseIdentifier: String {
  81. return T.reuseIdentifier + (detailText?.style.stringValue ?? "")
  82. }
  83. /// Returns the table view cell style for the specified subtitle.
  84. public var cellStyle: UITableViewCell.CellStyle {
  85. return detailText?.style ?? .default
  86. }
  87. /// The icon of the row.
  88. public let icon: Icon?
  89. /// Returns the accessory type with the disclosure indicator when `action` is not nil,
  90. /// and with the detail button when `accessoryButtonAction` is not nil.
  91. public var accessoryType: UITableViewCell.AccessoryType {
  92. #if os(iOS)
  93. switch (action, accessoryButtonAction) {
  94. case (nil, nil): return .none
  95. case (.some, nil): return .disclosureIndicator
  96. case (nil, .some): return .detailButton
  97. case (.some, .some): return .detailDisclosureButton
  98. }
  99. #elseif os(tvOS)
  100. return (action == nil) ? .none : .disclosureIndicator
  101. #endif
  102. }
  103. /// The `NavigationRow` is selectable when action is not nil.
  104. public var isSelectable: Bool {
  105. return action != nil
  106. }
  107. /// The additional customization during cell configuration.
  108. public let customize: ((UITableViewCell, Row & RowStyle) -> Void)?
  109. // MARK: Equatable
  110. /// Returns true iff `lhs` and `rhs` have equal titles, detail texts and icons.
  111. public static func == (lhs: NavigationRow, rhs: NavigationRow) -> Bool {
  112. return
  113. lhs.text == rhs.text &&
  114. lhs.detailText == rhs.detailText &&
  115. lhs.icon == rhs.icon
  116. }
  117. }
  118. private extension UITableViewCell.CellStyle {
  119. var stringValue: String {
  120. switch self {
  121. case .default: return ".default"
  122. case .subtitle: return ".subtitle"
  123. case .value1: return ".value1"
  124. case .value2: return ".value2"
  125. }
  126. }
  127. }