MultipleSelectorViewController.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // MultipleSelectorViewController.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. /// Selector Controller that enables multiple selection
  27. open class _MultipleSelectorViewController<Row: SelectableRowType, OptionsRow: OptionsProviderRow> : FormViewController, TypedRowControllerType where Row: BaseRow, Row.Cell.Value == OptionsRow.OptionsProviderType.Option, OptionsRow.OptionsProviderType.Option: Hashable {
  28. /// The row that pushed or presented this controller
  29. public var row: RowOf<Set<OptionsRow.OptionsProviderType.Option>>!
  30. public var selectableRowSetup: ((_ row: Row) -> Void)?
  31. public var selectableRowCellSetup: ((_ cell: Row.Cell, _ row: Row) -> Void)?
  32. public var selectableRowCellUpdate: ((_ cell: Row.Cell, _ row: Row) -> Void)?
  33. /// A closure to be called when the controller disappears.
  34. public var onDismissCallback: ((UIViewController) -> Void)?
  35. /// A closure that should return key for particular row value.
  36. /// This key is later used to break options by sections.
  37. public var sectionKeyForValue: ((Row.Cell.Value) -> (String))?
  38. /// A closure that returns header title for a section for particular key.
  39. /// By default returns the key itself.
  40. public var sectionHeaderTitleForKey: ((String) -> String?)? = { $0 }
  41. /// A closure that returns footer title for a section for particular key.
  42. public var sectionFooterTitleForKey: ((String) -> String?)?
  43. public var optionsProviderRow: OptionsRow {
  44. return row as! OptionsRow
  45. }
  46. override public init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
  47. super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  48. }
  49. convenience public init(_ callback: ((UIViewController) -> Void)?) {
  50. self.init(nibName: nil, bundle: nil)
  51. onDismissCallback = callback
  52. }
  53. public required init?(coder aDecoder: NSCoder) {
  54. super.init(coder: aDecoder)
  55. }
  56. open override func viewDidLoad() {
  57. super.viewDidLoad()
  58. setupForm()
  59. }
  60. open func setupForm() {
  61. optionsProviderRow.optionsProvider?.options(for: self) { [weak self] (options: [OptionsRow.OptionsProviderType.Option]?) in
  62. guard let strongSelf = self, let options = options else { return }
  63. strongSelf.optionsProviderRow.cachedOptionsData = options
  64. strongSelf.setupForm(with: options)
  65. }
  66. }
  67. open func setupForm(with options: [OptionsRow.OptionsProviderType.Option]) {
  68. if let optionsBySections = optionsBySections(with: options) {
  69. for (sectionKey, options) in optionsBySections {
  70. form +++ section(with: options,
  71. header: sectionHeaderTitleForKey?(sectionKey),
  72. footer: sectionFooterTitleForKey?(sectionKey))
  73. }
  74. } else {
  75. form +++ section(with: options, header: row.title, footer: nil)
  76. }
  77. }
  78. open func optionsBySections(with options: [OptionsRow.OptionsProviderType.Option]) -> [(String, [Row.Cell.Value])]? {
  79. guard let sectionKeyForValue = sectionKeyForValue else { return nil }
  80. let sections = options.reduce([:]) { (reduced, option) -> [String: [Row.Cell.Value]] in
  81. var reduced = reduced
  82. let key = sectionKeyForValue(option)
  83. var items = reduced[key] ?? []
  84. items.append(option)
  85. reduced[key] = items
  86. return reduced
  87. }
  88. return sections.sorted(by: { (lhs, rhs) in lhs.0 < rhs.0 })
  89. }
  90. func section(with options: [OptionsRow.OptionsProviderType.Option], header: String?, footer: String?) -> SelectableSection<Row> {
  91. let section = SelectableSection<Row>(header: header ?? "", footer: footer ?? "", selectionType: .multipleSelection) { section in
  92. section.onSelectSelectableRow = { [weak self] _, selectableRow in
  93. var newValue: Set<OptionsRow.OptionsProviderType.Option> = self?.row.value ?? []
  94. if let selectableValue = selectableRow.value {
  95. newValue.insert(selectableValue)
  96. } else {
  97. newValue.remove(selectableRow.selectableValue!)
  98. }
  99. self?.row.value = newValue
  100. }
  101. }
  102. for option in options {
  103. section <<< Row.init { lrow in
  104. lrow.title = String(describing: option)
  105. lrow.selectableValue = option
  106. lrow.value = self.row.value?.contains(option) ?? false ? option : nil
  107. self.selectableRowSetup?(lrow)
  108. }.cellSetup { [weak self] cell, row in
  109. self?.selectableRowCellSetup?(cell, row)
  110. }.cellUpdate { [weak self] cell, row in
  111. self?.selectableRowCellUpdate?(cell, row)
  112. }
  113. }
  114. return section
  115. }
  116. }
  117. open class MultipleSelectorViewController<OptionsRow: OptionsProviderRow>: _MultipleSelectorViewController<ListCheckRow<OptionsRow.OptionsProviderType.Option>, OptionsRow> where OptionsRow.OptionsProviderType.Option: Hashable{
  118. override public init(nibName nibNameOrNil: String? = nil, bundle nibBundleOrNil: Bundle? = nil) {
  119. super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  120. }
  121. public required init?(coder aDecoder: NSCoder) {
  122. super.init(coder: aDecoder)
  123. }
  124. }