PickerInlineRow.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // PickerInlineRow.swift
  2. // Eureka ( https://github.com/xmartlabs/Eureka )
  3. //
  4. // Copyright (c) 2016 Xmartlabs SRL ( http://xmartlabs.com )
  5. //
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. import Foundation
  25. import UIKit
  26. open class PickerInlineCell<T: Equatable> : Cell<T>, CellType {
  27. required public init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  28. super.init(style: style, reuseIdentifier: reuseIdentifier)
  29. }
  30. required public init?(coder aDecoder: NSCoder) {
  31. super.init(coder: aDecoder)
  32. }
  33. open override func setup() {
  34. super.setup()
  35. accessoryType = .none
  36. editingAccessoryType = .none
  37. }
  38. open override func update() {
  39. super.update()
  40. selectionStyle = row.isDisabled ? .none : .default
  41. }
  42. open override func didSelect() {
  43. super.didSelect()
  44. row.deselect()
  45. }
  46. }
  47. // MARK: PickerInlineRow
  48. open class _PickerInlineRow<T> : Row<PickerInlineCell<T>>, NoValueDisplayTextConformance where T: Equatable {
  49. public typealias InlineRow = PickerRow<T>
  50. open var options = [T]()
  51. open var noValueDisplayText: String?
  52. required public init(tag: String?) {
  53. super.init(tag: tag)
  54. }
  55. }
  56. /// A generic inline row where the user can pick an option from a picker view which shows and hides itself automatically
  57. public final class PickerInlineRow<T> : _PickerInlineRow<T>, RowType, InlineRowType where T: Equatable {
  58. required public init(tag: String?) {
  59. super.init(tag: tag)
  60. onExpandInlineRow { cell, row, _ in
  61. let color = cell.detailTextLabel?.textColor
  62. row.onCollapseInlineRow { cell, _, _ in
  63. cell.detailTextLabel?.textColor = color
  64. }
  65. cell.detailTextLabel?.textColor = cell.tintColor
  66. }
  67. }
  68. public override func customDidSelect() {
  69. super.customDidSelect()
  70. if !isDisabled {
  71. toggleInlineRow()
  72. }
  73. }
  74. public func setupInlineRow(_ inlineRow: InlineRow) {
  75. inlineRow.options = self.options
  76. inlineRow.displayValueFor = self.displayValueFor
  77. inlineRow.cell.height = { UITableView.automaticDimension }
  78. }
  79. }
  80. open class _DoublePickerInlineRow<A, B> : Row<PickerInlineCell<Tuple<A, B>>>, NoValueDisplayTextConformance where A: Equatable, B: Equatable {
  81. public typealias InlineRow = DoublePickerRow<A, B>
  82. /// Options for first component. Will be called often so should be O(1)
  83. public var firstOptions: (() -> [A]) = {[]}
  84. /// Options for second component given the selected value from the first component. Will be called often so should be O(1)
  85. public var secondOptions: ((A) -> [B]) = { _ in [] }
  86. public var noValueDisplayText: String?
  87. required public init(tag: String?) {
  88. super.init(tag: tag)
  89. self.displayValueFor = { [weak self] tuple in
  90. if let tuple = tuple {
  91. return String(describing: tuple.a) + ", " + String(describing: tuple.b)
  92. }
  93. return self?.noValueDisplayText
  94. }
  95. }
  96. }
  97. /// A generic inline row where the user can pick an option from a picker view which shows and hides itself automatically
  98. public final class DoublePickerInlineRow<A, B> : _DoublePickerInlineRow<A, B>, RowType, InlineRowType where A: Equatable, B: Equatable {
  99. required public init(tag: String?) {
  100. super.init(tag: tag)
  101. onExpandInlineRow { cell, row, _ in
  102. let color = cell.detailTextLabel?.textColor
  103. row.onCollapseInlineRow { cell, _, _ in
  104. cell.detailTextLabel?.textColor = color
  105. }
  106. cell.detailTextLabel?.textColor = cell.tintColor
  107. }
  108. }
  109. public override func customDidSelect() {
  110. super.customDidSelect()
  111. if !isDisabled {
  112. toggleInlineRow()
  113. }
  114. }
  115. public func setupInlineRow(_ inlineRow: InlineRow) {
  116. inlineRow.firstOptions = firstOptions
  117. inlineRow.secondOptions = secondOptions
  118. inlineRow.displayValueFor = self.displayValueFor
  119. inlineRow.cell.height = { UITableView.automaticDimension }
  120. }
  121. }
  122. open class _TriplePickerInlineRow<A, B, C> : Row<PickerInlineCell<Tuple3<A, B, C>>>, NoValueDisplayTextConformance
  123. where A: Equatable, B: Equatable, C: Equatable {
  124. public typealias InlineRow = TriplePickerRow<A, B, C>
  125. /// Options for first component. Will be called often so should be O(1)
  126. public var firstOptions: (() -> [A]) = {[]}
  127. /// Options for second component given the selected value from the first component. Will be called often so should be O(1)
  128. public var secondOptions: ((A) -> [B]) = { _ in [] }
  129. /// Options for third component given the selected value from the first and second components. Will be called often so should be O(1)
  130. public var thirdOptions: ((A, B) -> [C]) = {_, _ in []}
  131. open var noValueDisplayText: String?
  132. required public init(tag: String?) {
  133. super.init(tag: tag)
  134. self.displayValueFor = { [weak self] tuple in
  135. if let tuple = tuple {
  136. return String(describing: tuple.a) + ", " + String(describing: tuple.b) + ", " + String(describing: tuple.c)
  137. }
  138. return self?.noValueDisplayText
  139. }
  140. }
  141. }
  142. /// A generic inline row where the user can pick an option from a picker view which shows and hides itself automatically
  143. public final class TriplePickerInlineRow<A, B, C> : _TriplePickerInlineRow<A, B, C>, RowType, InlineRowType
  144. where A: Equatable, B: Equatable, C: Equatable {
  145. required public init(tag: String?) {
  146. super.init(tag: tag)
  147. onExpandInlineRow { cell, row, _ in
  148. let color = cell.detailTextLabel?.textColor
  149. row.onCollapseInlineRow { cell, _, _ in
  150. cell.detailTextLabel?.textColor = color
  151. }
  152. cell.detailTextLabel?.textColor = cell.tintColor
  153. }
  154. }
  155. public override func customDidSelect() {
  156. super.customDidSelect()
  157. if !isDisabled {
  158. toggleInlineRow()
  159. }
  160. }
  161. public func setupInlineRow(_ inlineRow: InlineRow) {
  162. inlineRow.firstOptions = firstOptions
  163. inlineRow.secondOptions = secondOptions
  164. inlineRow.thirdOptions = thirdOptions
  165. inlineRow.displayValueFor = self.displayValueFor
  166. inlineRow.cell.height = { UITableView.automaticDimension }
  167. }
  168. }