Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. class HomeVC: UIViewController, HomeVCProtocol {
  2.  
  3. @IBOutlet weak var tableView: UITableView!
  4. @IBOutlet weak var searchBar: UISearchBar!
  5.  
  6. let kCloseCellHeight: CGFloat = 145
  7. let kOpenCellHeight: CGFloat = 390
  8. var cellHeights: [CGFloat] = []
  9. var items = [ItemHome]()
  10.  
  11. var filteredItems = [ItemHome]()
  12. var searchActive : Bool = false
  13.  
  14. var presenter: HomePresenterProtocol?
  15.  
  16. override func viewDidLoad() {
  17. super.viewDidLoad()
  18. tableView.delegate = self
  19. tableView.dataSource = self
  20. searchBar.delegate = self
  21. presenter = HomePresenter(view: self)
  22.  
  23.  
  24. presenter?.performGetAvailableItems()
  25.  
  26. setupSearchBar()
  27. }
  28.  
  29. private func setupSearchBar() {
  30. let textFieldInsideSearchBar = searchBar.value(forKey: "searchField") as! UITextField
  31. textFieldInsideSearchBar.textColor = UIColor.white
  32. }
  33.  
  34. private func setupTable(kRowsCount: Int) {
  35. cellHeights = Array(repeating: kCloseCellHeight, count: kRowsCount)
  36. tableView.estimatedRowHeight = kCloseCellHeight
  37. tableView.rowHeight = UITableViewAutomaticDimension
  38. }
  39.  
  40. func dismissKeyboard() {
  41. view.endEditing(true)
  42. }
  43.  
  44. //MARK: Protocols
  45. func showLoading() {
  46. showLoadingIndicator()
  47. }
  48.  
  49. func hideLoading() {
  50. hideLoadingIndicator()
  51. }
  52.  
  53. func onShowError(error: AppError) {
  54. showError(message: error.description)
  55. }
  56.  
  57. func onGetAvailableItemsSuccess(homeItems: [ItemHome]) {
  58. setupTable(kRowsCount: homeItems.count)
  59. items = homeItems
  60. tableView.reloadData()
  61. }
  62.  
  63. func onShowFilteredItems(homeItems: [ItemHome]) {
  64. setupTable(kRowsCount: homeItems.count)
  65. filteredItems = homeItems
  66. tableView.reloadData()
  67. }
  68.  
  69. func onShowFilteringNoResult() {
  70.  
  71. }
  72.  
  73. }
  74.  
  75. // MARK: - TableView
  76. extension HomeVC: UITableViewDelegate, UITableViewDataSource {
  77.  
  78. func numberOfSections(in tableView: UITableView) -> Int {
  79. return 1
  80. }
  81.  
  82. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  83. if isFiltering() {
  84. return filteredItems.count
  85. } else {
  86. return items.count
  87. }
  88. }
  89.  
  90. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  91. guard let cell = tableView.dequeueReusableCell(withIdentifier: "FoldingCell", for: indexPath) as? HomeItemCell else {
  92. return UITableViewCell()
  93. }
  94.  
  95. let homeItem: ItemHome
  96.  
  97. if isFiltering() {
  98. homeItem = filteredItems[indexPath.row]
  99. } else {
  100. homeItem = items[indexPath.row]
  101. }
  102.  
  103. cell.config(itemHome: homeItem)
  104.  
  105. let durations: [TimeInterval] = [0.26, 0.2, 0.2]
  106. cell.durationsForExpandedState = durations
  107. cell.durationsForCollapsedState = durations
  108. return cell
  109. }
  110.  
  111. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  112. return cellHeights[indexPath.row]
  113. }
  114.  
  115. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  116.  
  117. dismissKeyboard()
  118.  
  119. let cell = tableView.cellForRow(at: indexPath) as! FoldingCell
  120.  
  121. if cell.isAnimating() {
  122. return
  123. }
  124.  
  125. var duration = 0.0
  126. let cellIsCollapsed = cellHeights[indexPath.row] == kCloseCellHeight
  127. if cellIsCollapsed {
  128. cellHeights[indexPath.row] = kOpenCellHeight
  129. cell.unfold(true, animated: true, completion: nil)
  130. duration = 0.5
  131. } else {
  132. cellHeights[indexPath.row] = kCloseCellHeight
  133. cell.unfold(false, animated: true, completion: nil)
  134. duration = 0.8
  135. }
  136.  
  137. UIView.animate(withDuration: duration, delay: 0, options: .curveEaseOut, animations: { () -> Void in
  138. tableView.beginUpdates()
  139. tableView.endUpdates()
  140. }, completion: nil)
  141. }
  142.  
  143. func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
  144.  
  145. print("WILL DISPLAY \(cellHeights.count)")
  146. guard case let cell as HomeItemCell = cell else {
  147. return
  148. }
  149.  
  150. cell.backgroundColor = .clear
  151.  
  152. if cellHeights[indexPath.row] == kCloseCellHeight {
  153. cell.unfold(false, animated: false, completion: nil)
  154. } else {
  155. cell.unfold(true, animated: false, completion: nil)
  156. }
  157. }
  158. }
  159.  
  160. extension HomeVC: UISearchBarDelegate {
  161.  
  162. func searchBarIsEmpty() -> Bool {
  163. return searchBar.text?.isEmpty ?? true
  164. }
  165.  
  166. func isFiltering() -> Bool {
  167. return searchActive && !searchBarIsEmpty()
  168. }
  169.  
  170.  
  171. func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
  172. searchActive = true;
  173. }
  174.  
  175. func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
  176. searchActive = false;
  177. }
  178.  
  179.  
  180. func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
  181. presenter?.filterContentForSearchText(searchText, items: items)
  182. }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement