ButtonRow.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // ButtonRow.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. // MARK: ButtonCell
  27. open class ButtonCellOf<T: Equatable>: Cell<T>, CellType {
  28. required public init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  29. super.init(style: style, reuseIdentifier: reuseIdentifier)
  30. }
  31. required public init?(coder aDecoder: NSCoder) {
  32. super.init(coder: aDecoder)
  33. }
  34. open override func update() {
  35. super.update()
  36. selectionStyle = row.isDisabled ? .none : .default
  37. accessoryType = .none
  38. editingAccessoryType = accessoryType
  39. textLabel?.textAlignment = .center
  40. textLabel?.textColor = tintColor.withAlphaComponent(row.isDisabled ? 0.3 : 1.0)
  41. }
  42. open override func didSelect() {
  43. super.didSelect()
  44. row.deselect()
  45. }
  46. }
  47. public typealias ButtonCell = ButtonCellOf<String>
  48. // MARK: ButtonRow
  49. open class _ButtonRowOf<T: Equatable> : Row<ButtonCellOf<T>> {
  50. open var presentationMode: PresentationMode<UIViewController>?
  51. required public init(tag: String?) {
  52. super.init(tag: tag)
  53. displayValueFor = nil
  54. cellStyle = .default
  55. }
  56. open override func customDidSelect() {
  57. super.customDidSelect()
  58. if !isDisabled {
  59. if let presentationMode = presentationMode {
  60. if let controller = presentationMode.makeController() {
  61. presentationMode.present(controller, row: self, presentingController: self.cell.formViewController()!)
  62. } else {
  63. presentationMode.present(nil, row: self, presentingController: self.cell.formViewController()!)
  64. }
  65. }
  66. }
  67. }
  68. open override func customUpdateCell() {
  69. super.customUpdateCell()
  70. let leftAligmnment = presentationMode != nil
  71. cell.textLabel?.textAlignment = leftAligmnment ? .left : .center
  72. cell.accessoryType = !leftAligmnment || isDisabled ? .none : .disclosureIndicator
  73. cell.editingAccessoryType = cell.accessoryType
  74. cell.textLabel?.textColor = !leftAligmnment ? cell.tintColor.withAlphaComponent(isDisabled ? 0.3 : 1.0) : nil
  75. }
  76. open override func prepare(for segue: UIStoryboardSegue) {
  77. super.prepare(for: segue)
  78. (segue.destination as? RowControllerType)?.onDismissCallback = presentationMode?.onDismissCallback
  79. }
  80. }
  81. /// A generic row with a button. The action of this button can be anything but normally will push a new view controller
  82. public final class ButtonRowOf<T: Equatable> : _ButtonRowOf<T>, RowType {
  83. public required init(tag: String?) {
  84. super.init(tag: tag)
  85. }
  86. }
  87. /// A row with a button and String value. The action of this button can be anything but normally will push a new view controller
  88. public typealias ButtonRow = ButtonRowOf<String>