OptionRow.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // OptionRow.swift
  3. // QuickTableViewController
  4. //
  5. // Created by Ben on 30/07/2017.
  6. // Copyright © 2017 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 of selectable option.
  28. open class OptionRow<T: UITableViewCell>: OptionRowCompatible, Equatable {
  29. // MARK: - Initializer
  30. /// Initializes an `OptionRow` with a text, a selection state and an action closure.
  31. /// The detail text, icon, and the customization closure are optional.
  32. public init(
  33. text: String,
  34. detailText: DetailText? = nil,
  35. isSelected: Bool,
  36. icon: Icon? = nil,
  37. customization: ((UITableViewCell, Row & RowStyle) -> Void)? = nil,
  38. action: ((Row) -> Void)?
  39. ) {
  40. self.text = text
  41. self.detailText = detailText
  42. self.isSelected = isSelected
  43. self.icon = icon
  44. self.customize = customization
  45. self.action = action
  46. }
  47. // MARK: - OptionRowCompatible
  48. /// The state of selection.
  49. public var isSelected: Bool = false {
  50. didSet {
  51. guard isSelected != oldValue else {
  52. return
  53. }
  54. DispatchQueue.main.async {
  55. self.action?(self)
  56. }
  57. }
  58. }
  59. // MARK: - Row
  60. /// The text of the row.
  61. public let text: String
  62. /// The detail text of the row.
  63. public let detailText: DetailText?
  64. /// A closure that will be invoked when the `isSelected` is changed.
  65. public let action: ((Row) -> Void)?
  66. // MARK: - RowStyle
  67. /// The type of the table view cell to display the row.
  68. public let cellType: UITableViewCell.Type = T.self
  69. /// The reuse identifier of the table view cell to display the row. The default value is **UITableViewCell**.
  70. public let cellReuseIdentifier: String = T.reuseIdentifier
  71. /// The cell style is `.default`.
  72. public let cellStyle: UITableViewCell.CellStyle = .default
  73. /// The icon of the row.
  74. public let icon: Icon?
  75. /// Returns `.checkmark` when the row is selected, otherwise returns `.none`.
  76. public var accessoryType: UITableViewCell.AccessoryType {
  77. return isSelected ? .checkmark : .none
  78. }
  79. /// `OptionRow` is always selectable.
  80. public let isSelectable: Bool = true
  81. /// Additional customization during cell configuration.
  82. public let customize: ((UITableViewCell, Row & RowStyle) -> Void)?
  83. // MARK: - Equatable
  84. /// Returns true iff `lhs` and `rhs` have equal titles, detail texts, selection states, and icons.
  85. public static func == (lhs: OptionRow, rhs: OptionRow) -> Bool {
  86. return
  87. lhs.text == rhs.text &&
  88. lhs.detailText == rhs.detailText &&
  89. lhs.isSelected == rhs.isSelected &&
  90. lhs.icon == rhs.icon
  91. }
  92. }