Guest User

Untitled

a guest
Nov 21st, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. import Eureka
  2.  
  3.  
  4. open class _SearchablePushRow<Cell: CellType>: OptionsRow<Cell>, PresenterRowType where Cell: BaseCell {
  5.  
  6.  
  7. public typealias PresenterRow = SearchableSelectorViewController<SelectorRow<Cell>>
  8.  
  9. /// Defines how the view controller will be presented, pushed, etc.
  10. open var presentationMode: PresentationMode<PresenterRow>?
  11.  
  12. /// Will be called before the presentation occurs.
  13. open var onPresentCallback: ((FormViewController, PresenterRow) -> Void)?
  14.  
  15. required public init(tag: String?) {
  16. super.init(tag: tag)
  17. presentationMode = .show(controllerProvider: ControllerProvider.callback { return SearchableSelectorViewController<SelectorRow<Cell>> { _ in } }, onDismiss: { vc in
  18. let _ = vc.navigationController?.popViewController(animated: true) })
  19. }
  20.  
  21. /**
  22. Extends `didSelect` method
  23. */
  24. open override func customDidSelect() {
  25. super.customDidSelect()
  26. guard let presentationMode = presentationMode, !isDisabled else { return }
  27. if let controller = presentationMode.makeController() {
  28. controller.row = self
  29. controller.options = (self.optionsProvider?.optionsArray)!
  30. controller.title = selectorTitle ?? controller.title
  31. onPresentCallback?(cell.formViewController()!, controller)
  32. presentationMode.present(controller, row: self, presentingController: self.cell.formViewController()!)
  33. } else {
  34. presentationMode.present(nil, row: self, presentingController: self.cell.formViewController()!)
  35. }
  36. }
  37.  
  38. /**
  39. Prepares the pushed row setting its title and completion callback.
  40. */
  41. open override func prepare(for segue: UIStoryboardSegue) {
  42. super.prepare(for: segue)
  43. guard let rowVC = segue.destination as? PresenterRow else { return }
  44. rowVC.title = selectorTitle ?? rowVC.title
  45. rowVC.onDismissCallback = presentationMode?.onDismissCallback ?? rowVC.onDismissCallback
  46. onPresentCallback?(cell.formViewController()!, rowVC)
  47. rowVC.row = self
  48. }
  49. }
  50.  
  51.  
  52. /// A selector row where the user can pick an option from a pushed view controller
  53. public final class SearchablePushRow<T: Equatable> : _SearchablePushRow<PushSelectorCell<T>>, RowType {
  54. public required init(tag: String?) {
  55. super.init(tag: tag)
  56. }
  57. }
  58.  
  59. open class _SearchableSelectorViewController<Row: SelectableRowType, OptionsRow: OptionsProviderRow>: UITableViewController, UISearchResultsUpdating, TypedRowControllerType where Row: BaseRow, Row.Cell.Value == OptionsRow.OptionsProviderType.Option {
  60.  
  61. /// The row that pushed or presented this controller
  62. public var row: RowOf<Row.Cell.Value>!
  63. public var options: [Row.Cell.Value]!
  64.  
  65. /// A closure to be called when the controller disappears.
  66. public var onDismissCallback: ((UIViewController) -> Void)?
  67.  
  68.  
  69. // Search
  70. let searchController = UISearchController(searchResultsController: nil)
  71. var originalOptions = [Row.Cell.Value]()
  72. var currentOptions = [Row.Cell.Value]()
  73.  
  74. // I would like to use a more elegant solution to get the options array from optionsprovider
  75. // same code as in _SelectorViewController but producing a swift_dynamicCastUnknownClassUnconditional exception
  76. /*
  77. public var optionsProviderRow: OptionsRow {
  78. return row as! OptionsRow
  79. }
  80. */
  81.  
  82. required public init() {
  83. super.init(style: .grouped)
  84. searchController.searchResultsUpdater = self
  85. searchController.dimsBackgroundDuringPresentation = false
  86. searchController.hidesNavigationBarDuringPresentation = false
  87. self.definesPresentationContext = true
  88. }
  89.  
  90. convenience public init(_ callback: ((UIViewController) -> Void)?) {
  91. self.init()
  92. onDismissCallback = callback
  93. }
  94.  
  95. required public init?(coder aDecoder: NSCoder) {
  96. fatalError("init(coder:) has not been implemented")
  97. }
  98.  
  99. open override func viewDidLoad() {
  100. super.viewDidLoad()
  101.  
  102. tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
  103. tableView!.tableHeaderView = searchController.searchBar
  104.  
  105. originalOptions = options
  106. currentOptions = options
  107.  
  108. tableView.reloadData()
  109. }
  110.  
  111. open override func numberOfSections(in tableView: UITableView) -> Int {
  112. return 1
  113. }
  114.  
  115. open override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  116. return row?.title
  117. }
  118. open override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  119. return currentOptions.count
  120. }
  121. open override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  122. let option = currentOptions[indexPath.row]
  123. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
  124. cell.textLabel?.text = row.displayValueFor?(option)
  125. return cell
  126. }
  127. open override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  128. let option = self.currentOptions[indexPath.row]
  129. row.value = option
  130. onDismissCallback?(self)
  131. }
  132. open func updateSearchResults(for searchController: UISearchController) {
  133. if let query = searchController.searchBar.text {
  134. if query == "" {
  135. currentOptions = originalOptions
  136. } else {
  137. currentOptions = originalOptions.filter{
  138. (row.displayValueFor?($0)?.contains(query))!
  139. }
  140. }
  141. }
  142. tableView.reloadData()
  143. }
  144.  
  145. }
  146.  
  147. /// Selector Controller (used to select one option among a list)
  148. open class SearchableSelectorViewController<OptionsRow: OptionsProviderRow>: _SearchableSelectorViewController<ListCheckRow<OptionsRow.OptionsProviderType.Option>, OptionsRow> {
  149. }
Add Comment
Please, Sign In to add comment