RadioSection.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // RadioSection.swift
  3. // QuickTableViewController
  4. //
  5. // Created by Ben on 17/08/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 Foundation
  27. /// A section that allows only one option selected in a table view.
  28. open class RadioSection: Section {
  29. // MARK: - Initializer
  30. /// Initializes a section with a title, containing rows and an optional footer.
  31. public init(title: String?, options: [OptionRowCompatible], footer: String? = nil) {
  32. self.options = options
  33. super.init(title: title, rows: [], footer: footer)
  34. }
  35. private override init(title: String?, rows: [Row & RowStyle], footer: String? = nil) {
  36. fatalError("init with title, rows, and footer is not supported")
  37. }
  38. // MARK: - Section
  39. /// The array of rows in the section.
  40. open override var rows: [Row & RowStyle] {
  41. get {
  42. return options
  43. }
  44. set {
  45. options = newValue as? [OptionRowCompatible] ?? options
  46. }
  47. }
  48. // MARK: - RadioSection
  49. /// A boolean that indicates whether there's always one option selected.
  50. open var alwaysSelectsOneOption: Bool = false {
  51. didSet {
  52. if alwaysSelectsOneOption && selectedOption == nil {
  53. options.first?.isSelected = true
  54. }
  55. }
  56. }
  57. /// The array of options in the section. It's identical to the `rows`.
  58. open private(set) var options: [OptionRowCompatible]
  59. /// Returns the selected index, or nil when nothing is selected.
  60. open var indexOfSelectedOption: Int? {
  61. return options.index { $0.isSelected }
  62. }
  63. /// Returns the selected option row, or nil when nothing is selected.
  64. open var selectedOption: OptionRowCompatible? {
  65. if let index = indexOfSelectedOption {
  66. return options[index]
  67. } else {
  68. return nil
  69. }
  70. }
  71. /// Toggle the selection of the given option and keep options mutually exclusive.
  72. /// If `alwaysSelectOneOption` is set to true, it will not deselect the current selection.
  73. ///
  74. /// - Parameter option: The option to flip the `isSelected` state.
  75. /// - Returns: The indexes of changed options.
  76. open func toggle(_ option: OptionRowCompatible) -> IndexSet {
  77. if option.isSelected && alwaysSelectsOneOption {
  78. return []
  79. }
  80. defer {
  81. option.isSelected = !option.isSelected
  82. }
  83. if option.isSelected {
  84. // Deselect the selected option.
  85. return options.index(where: { $0 === option }).map { [$0] } ?? []
  86. }
  87. var toggledIndexes: IndexSet = []
  88. for (index, element) in options.enumerated() {
  89. switch element {
  90. case let target where target === option:
  91. toggledIndexes.insert(index)
  92. case _ where element.isSelected:
  93. toggledIndexes.insert(index)
  94. element.isSelected = false
  95. default:
  96. break
  97. }
  98. }
  99. return toggledIndexes
  100. }
  101. }