SelectableSection.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // SelectableSection.swift
  2. // Eureka ( https://github.com/xmartlabs/Eureka )
  3. //
  4. // Copyright (c) 2016 Xmartlabs ( 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. // MARK: SelectableSection
  27. /**
  28. Defines how the selection works in a SelectableSection
  29. - MultipleSelection: Multiple options can be selected at once
  30. - SingleSelection: Only one selection at a time. Can additionally specify if deselection is enabled or not.
  31. */
  32. public enum SelectionType {
  33. /**
  34. * Multiple options can be selected at once
  35. */
  36. case multipleSelection
  37. /**
  38. * Only one selection at a time. Can additionally specify if deselection is enabled or not.
  39. */
  40. case singleSelection(enableDeselection: Bool)
  41. }
  42. /**
  43. * Protocol to be implemented by selectable sections types. Enables easier customization
  44. */
  45. public protocol SelectableSectionType: Collection {
  46. associatedtype SelectableRow: BaseRow, SelectableRowType
  47. /// Defines how the selection works (single / multiple selection)
  48. var selectionType: SelectionType { get set }
  49. /// A closure called when a row of this section is selected.
  50. var onSelectSelectableRow: ((SelectableRow.Cell, SelectableRow) -> Void)? { get set }
  51. func selectedRow() -> SelectableRow?
  52. func selectedRows() -> [SelectableRow]
  53. }
  54. extension SelectableSectionType where Self: Section {
  55. /**
  56. Returns the selected row of this section. Should be used if selectionType is SingleSelection
  57. */
  58. public func selectedRow() -> SelectableRow? {
  59. return selectedRows().first
  60. }
  61. /**
  62. Returns the selected rows of this section. Should be used if selectionType is MultipleSelection
  63. */
  64. public func selectedRows() -> [SelectableRow] {
  65. let selectedRows: [BaseRow] = self.filter { $0 is SelectableRow && $0.baseValue != nil }
  66. return selectedRows.map { $0 as! SelectableRow }
  67. }
  68. /**
  69. Internal function used to set up a collection of rows before they are added to the section
  70. */
  71. func prepare(selectableRows rows: [BaseRow]) {
  72. for row in rows {
  73. if let row = row as? SelectableRow {
  74. row.onCellSelection { [weak self] cell, row in
  75. guard let s = self, !row.isDisabled else { return }
  76. switch s.selectionType {
  77. case .multipleSelection:
  78. row.value = row.value == nil ? row.selectableValue : nil
  79. case let .singleSelection(enableDeselection):
  80. s.forEach {
  81. guard $0.baseValue != nil && $0 != row && $0 is SelectableRow else { return }
  82. $0.baseValue = nil
  83. $0.updateCell()
  84. }
  85. // Check if row is not already selected
  86. if row.value == nil {
  87. row.value = row.selectableValue
  88. } else if enableDeselection {
  89. row.value = nil
  90. }
  91. }
  92. row.updateCell()
  93. s.onSelectSelectableRow?(cell, row)
  94. }
  95. }
  96. }
  97. }
  98. }
  99. /// A subclass of Section that serves to create a section with a list of selectable options.
  100. open class SelectableSection<Row>: Section, SelectableSectionType where Row: SelectableRowType, Row: BaseRow {
  101. public typealias SelectableRow = Row
  102. /// Defines how the selection works (single / multiple selection)
  103. public var selectionType = SelectionType.singleSelection(enableDeselection: true)
  104. /// A closure called when a row of this section is selected.
  105. public var onSelectSelectableRow: ((Row.Cell, Row) -> Void)?
  106. public override init(_ initializer: @escaping (SelectableSection<Row>) -> Void) {
  107. super.init({ _ in })
  108. initializer(self)
  109. }
  110. public init(_ header: String?, selectionType: SelectionType, _ initializer: @escaping (SelectableSection<Row>) -> Void = { _ in }) {
  111. self.selectionType = selectionType
  112. super.init(header, { _ in })
  113. initializer(self)
  114. }
  115. public init(header: String?, footer: String?, selectionType: SelectionType, _ initializer: @escaping (SelectableSection<Row>) -> Void = { _ in }) {
  116. self.selectionType = selectionType
  117. super.init(header: header, footer: footer, { _ in })
  118. initializer(self)
  119. }
  120. public required init() {
  121. super.init()
  122. }
  123. #if swift(>=4.1)
  124. public required init<S>(_ elements: S) where S : Sequence, S.Element == BaseRow {
  125. super.init(elements)
  126. }
  127. #endif
  128. open override func rowsHaveBeenAdded(_ rows: [BaseRow], at: IndexSet) {
  129. prepare(selectableRows: rows)
  130. }
  131. }