SelectorRow.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // SelectorRow.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. open class PushSelectorCell<T: Equatable> : Cell<T>, CellType {
  27. required public init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  28. super.init(style: style, reuseIdentifier: reuseIdentifier)
  29. }
  30. required public init?(coder aDecoder: NSCoder) {
  31. super.init(coder: aDecoder)
  32. }
  33. open override func update() {
  34. super.update()
  35. accessoryType = .disclosureIndicator
  36. editingAccessoryType = accessoryType
  37. selectionStyle = row.isDisabled ? .none : .default
  38. }
  39. }
  40. /// Generic row type where a user must select a value among several options.
  41. open class SelectorRow<Cell: CellType>: OptionsRow<Cell>, PresenterRowType where Cell: BaseCell {
  42. /// Defines how the view controller will be presented, pushed, etc.
  43. open var presentationMode: PresentationMode<SelectorViewController<SelectorRow<Cell>>>?
  44. /// Will be called before the presentation occurs.
  45. open var onPresentCallback: ((FormViewController, SelectorViewController<SelectorRow<Cell>>) -> Void)?
  46. required public init(tag: String?) {
  47. super.init(tag: tag)
  48. }
  49. /**
  50. Extends `didSelect` method
  51. */
  52. open override func customDidSelect() {
  53. super.customDidSelect()
  54. guard let presentationMode = presentationMode, !isDisabled else { return }
  55. if let controller = presentationMode.makeController() {
  56. controller.row = self
  57. controller.title = selectorTitle ?? controller.title
  58. onPresentCallback?(cell.formViewController()!, controller)
  59. presentationMode.present(controller, row: self, presentingController: self.cell.formViewController()!)
  60. } else {
  61. presentationMode.present(nil, row: self, presentingController: self.cell.formViewController()!)
  62. }
  63. }
  64. /**
  65. Prepares the pushed row setting its title and completion callback.
  66. */
  67. open override func prepare(for segue: UIStoryboardSegue) {
  68. super.prepare(for: segue)
  69. guard let rowVC = segue.destination as Any as? SelectorViewController<SelectorRow<Cell>> else { return }
  70. rowVC.title = selectorTitle ?? rowVC.title
  71. rowVC.onDismissCallback = presentationMode?.onDismissCallback ?? rowVC.onDismissCallback
  72. onPresentCallback?(cell.formViewController()!, rowVC)
  73. rowVC.row = self
  74. }
  75. }