ActionSheetRow.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // ActionSheetRow.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 AlertSelectorCell<T> : Cell<T>, CellType where T: Equatable {
  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 = .none
  36. editingAccessoryType = accessoryType
  37. selectionStyle = row.isDisabled ? .none : .default
  38. }
  39. open override func didSelect() {
  40. super.didSelect()
  41. row.deselect()
  42. }
  43. }
  44. open class _ActionSheetRow<Cell: CellType>: AlertOptionsRow<Cell>, PresenterRowType where Cell: BaseCell {
  45. public typealias ProviderType = SelectorAlertController<_ActionSheetRow<Cell>>
  46. public var onPresentCallback: ((FormViewController, ProviderType) -> Void)?
  47. lazy public var presentationMode: PresentationMode<ProviderType>? = {
  48. return .presentModally(controllerProvider: ControllerProvider.callback { [weak self] in
  49. let vc = SelectorAlertController<_ActionSheetRow<Cell>>(title: self?.selectorTitle, message: nil, preferredStyle: .actionSheet)
  50. if let popView = vc.popoverPresentationController {
  51. guard let cell = self?.cell, let tableView = cell.formViewController()?.tableView else { fatalError() }
  52. popView.sourceView = tableView
  53. popView.sourceRect = tableView.convert(cell.detailTextLabel?.frame ?? cell.textLabel?.frame ?? cell.contentView.frame, from: cell)
  54. }
  55. vc.row = self
  56. return vc
  57. },
  58. onDismiss: { [weak self] in
  59. $0.dismiss(animated: true)
  60. self?.cell?.formViewController()?.tableView?.reloadData()
  61. })
  62. }()
  63. public required init(tag: String?) {
  64. super.init(tag: tag)
  65. }
  66. open override func customDidSelect() {
  67. super.customDidSelect()
  68. if let presentationMode = presentationMode, !isDisabled {
  69. if let controller = presentationMode.makeController() {
  70. controller.row = self
  71. onPresentCallback?(cell.formViewController()!, controller)
  72. presentationMode.present(controller, row: self, presentingController: cell.formViewController()!)
  73. } else {
  74. presentationMode.present(nil, row: self, presentingController: cell.formViewController()!)
  75. }
  76. }
  77. }
  78. }
  79. /// An options row where the user can select an option from an ActionSheet
  80. public final class ActionSheetRow<T>: _ActionSheetRow<AlertSelectorCell<T>>, RowType where T: Equatable {
  81. required public init(tag: String?) {
  82. super.init(tag: tag)
  83. }
  84. }