SwitchRow.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // SwitchRow.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 with a switch.
  28. open class SwitchRow<T: SwitchCell>: SwitchRowCompatible, Equatable {
  29. // MARK: - Initializer
  30. /// Initializes a `SwitchRow` with a title, a switch 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. switchValue: 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.switchValue = switchValue
  43. self.icon = icon
  44. self.customize = customization
  45. self.action = action
  46. }
  47. // MARK: - SwitchRowCompatible
  48. /// The state of the switch.
  49. public var switchValue: Bool = false {
  50. didSet {
  51. guard switchValue != 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 `switchValue` 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 **SwitchCell**.
  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. #if os(iOS)
  76. /// The default accessory type is `.none`.
  77. public let accessoryType: UITableViewCell.AccessoryType = .none
  78. /// The `SwitchRow` should not be selectable.
  79. public let isSelectable: Bool = false
  80. #elseif os(tvOS)
  81. /// Returns `.checkmark` when the `switchValue` is on, otherwise returns `.none`.
  82. public var accessoryType: UITableViewCell.AccessoryType {
  83. return switchValue ? .checkmark : .none
  84. }
  85. /// The `SwitchRow` is selectable on tvOS.
  86. public let isSelectable: Bool = true
  87. #endif
  88. /// The additional customization during cell configuration.
  89. public let customize: ((UITableViewCell, Row & RowStyle) -> Void)?
  90. // MARK: - Equatable
  91. /// Returns true iff `lhs` and `rhs` have equal titles, detail texts, switch values, and icons.
  92. public static func == (lhs: SwitchRow, rhs: SwitchRow) -> Bool {
  93. return
  94. lhs.text == rhs.text &&
  95. lhs.detailText == rhs.detailText &&
  96. lhs.switchValue == rhs.switchValue &&
  97. lhs.icon == rhs.icon
  98. }
  99. }