You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
4.9 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. //
  2. // AddressSearchViewController.swift
  3. // GME Remit
  4. //
  5. // Created by InKwon Devik Kim on 22/04/2019.
  6. //Copyright © 2019 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. protocol SearchAddressDelegate: class {
  10. func searchAddress(viewController: SearchAddressViewController, selectedJuso: Juso)
  11. }
  12. class SearchAddressViewController: UIViewController {
  13. // MARK: Properties
  14. var presenter: SearchAddressModuleInterface?
  15. weak var delegate: SearchAddressDelegate?
  16. private var model: JusoResult? {
  17. didSet {
  18. guard
  19. let juso = self.model?.juso,
  20. let total = self.model?.common?.totalCount,
  21. let totalCount = Int(total) else { return }
  22. self.totalCount = totalCount
  23. self.jusoList.append(contentsOf: juso)
  24. self.tableView.reloadData()
  25. self.moveToTop()
  26. self.noResultLabel.text = self.totalCount == 0 ? "No results were found for your search." : ""
  27. }
  28. }
  29. private var jusoList = [Juso]()
  30. private var interval = 10
  31. private var pageIndex = 1
  32. private var totalCount = 0
  33. private var isSearch = false
  34. private var searchedText = "" {
  35. didSet {
  36. self.presenter?.fetchAddress(
  37. pageIndex: self.pageIndex,
  38. interval: self.interval,
  39. keyword: self.searchedText
  40. )
  41. }
  42. }
  43. // MARK: IBOutlets
  44. @IBOutlet private weak var tableView: UITableView!
  45. @IBOutlet private weak var searchBar: UISearchBar!
  46. @IBOutlet private weak var noResultLabel: UILabel!
  47. // MARK: VC's Life cycle
  48. override func viewDidLoad() {
  49. super.viewDidLoad()
  50. self.setup()
  51. self.searchBar.becomeFirstResponder()
  52. }
  53. override func viewWillAppear(_ animated: Bool) {
  54. super.viewWillAppear(animated)
  55. self.title = "Search Address"
  56. }
  57. override func viewWillDisappear(_ animated: Bool) {
  58. super.viewWillDisappear(animated)
  59. self.view.endEditing(true)
  60. }
  61. // MARK: IBActions
  62. @IBAction private func touchCloseButton(_ sender: UIBarButtonItem) {
  63. self.dismiss(animated: true, completion: nil)
  64. }
  65. // MARK: Other Functions
  66. private func setup() {
  67. // all setup should be done here
  68. self.tableView.delegate = self
  69. self.tableView.dataSource = self
  70. self.searchBar.delegate = self
  71. // self.setLargeTitle()
  72. // self.setSearchBarAppearance()
  73. }
  74. private func setSearchBarAppearance() {
  75. let textFieldInsideUISearchBar = self.searchBar.value(forKey: "searchField") as? UITextField
  76. textFieldInsideUISearchBar?.font = .sanfrancisco(.regular, size: 14)
  77. let textFieldInsideUISearchBarLabel = textFieldInsideUISearchBar!
  78. .value(forKey: "placeholderLabel") as? UILabel
  79. textFieldInsideUISearchBarLabel?.font = .sanfrancisco(.regular, size: 14)
  80. }
  81. private func clear() {
  82. self.totalCount = 0
  83. self.pageIndex = 0
  84. self.jusoList.removeAll()
  85. }
  86. private func moveToTop() {
  87. if self.isSearch {
  88. let indexPath = NSIndexPath(row: NSNotFound, section: 0)
  89. self.tableView.scrollToRow(at: indexPath as IndexPath, at: .top, animated: false)
  90. self.isSearch = false
  91. }
  92. }
  93. }
  94. // MARK: AddressSearchViewInterface
  95. extension SearchAddressViewController: SearchAddressViewInterface {
  96. func setJusoModel(with model: JusoResult?) {
  97. self.model = model
  98. }
  99. func failure(with error: Error) {
  100. self.alertWithOk(message: error.localizedDescription)
  101. }
  102. func startLoading() {
  103. self.showProgressHud()
  104. }
  105. func endLoading() {
  106. self.hideProgressHud()
  107. }
  108. }
  109. extension SearchAddressViewController: UITableViewDataSource {
  110. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  111. return jusoList.count
  112. }
  113. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  114. guard let cell = tableView.dequeueReusableCell(
  115. withIdentifier: "AddressTableViewCell"
  116. ) as? AddressTableViewCell else {
  117. return UITableViewCell()
  118. }
  119. cell.setModel(with: self.jusoList[indexPath.row])
  120. return cell
  121. }
  122. }
  123. extension SearchAddressViewController: UITableViewDelegate {
  124. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  125. self.delegate?.searchAddress(viewController: self, selectedJuso: self.jusoList[indexPath.row])
  126. }
  127. func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
  128. if indexPath.row + 1 == self.jusoList.count &&
  129. indexPath.row + 1 < self.totalCount {
  130. self.pageIndex += 1
  131. self.presenter?.fetchAddress(
  132. pageIndex: self.pageIndex,
  133. interval: self.interval,
  134. keyword: self.searchedText
  135. )
  136. }
  137. }
  138. }
  139. extension SearchAddressViewController: UISearchBarDelegate {
  140. func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
  141. self.clear()
  142. guard let address = searchBar.text else {
  143. return
  144. }
  145. self.isSearch = true
  146. self.searchedText = address
  147. self.searchBar.resignFirstResponder()
  148. }
  149. }