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
192 lines
4.9 KiB
//
|
|
// AddressSearchViewController.swift
|
|
// GME Remit
|
|
//
|
|
// Created by InKwon Devik Kim on 22/04/2019.
|
|
//Copyright © 2019 Gobal Money Express Co. Ltd. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
protocol SearchAddressDelegate: class {
|
|
func searchAddress(viewController: SearchAddressViewController, selectedJuso: Juso)
|
|
}
|
|
|
|
class SearchAddressViewController: UIViewController {
|
|
|
|
// MARK: Properties
|
|
|
|
var presenter: SearchAddressModuleInterface?
|
|
weak var delegate: SearchAddressDelegate?
|
|
|
|
private var model: JusoResult? {
|
|
didSet {
|
|
guard
|
|
let juso = self.model?.juso,
|
|
let total = self.model?.common?.totalCount,
|
|
let totalCount = Int(total) else { return }
|
|
|
|
self.totalCount = totalCount
|
|
self.jusoList.append(contentsOf: juso)
|
|
self.tableView.reloadData()
|
|
|
|
self.moveToTop()
|
|
|
|
self.noResultLabel.text = self.totalCount == 0 ? "No results were found for your search." : ""
|
|
}
|
|
}
|
|
|
|
private var jusoList = [Juso]()
|
|
|
|
private var interval = 10
|
|
|
|
private var pageIndex = 1
|
|
|
|
private var totalCount = 0
|
|
|
|
private var isSearch = false
|
|
|
|
private var searchedText = "" {
|
|
didSet {
|
|
self.presenter?.fetchAddress(
|
|
pageIndex: self.pageIndex,
|
|
interval: self.interval,
|
|
keyword: self.searchedText
|
|
)
|
|
}
|
|
}
|
|
// MARK: IBOutlets
|
|
|
|
@IBOutlet private weak var tableView: UITableView!
|
|
@IBOutlet private weak var searchBar: UISearchBar!
|
|
@IBOutlet private weak var noResultLabel: UILabel!
|
|
|
|
// MARK: VC's Life cycle
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
self.setup()
|
|
self.searchBar.becomeFirstResponder()
|
|
}
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
super.viewWillAppear(animated)
|
|
self.title = "Search Address"
|
|
}
|
|
|
|
override func viewWillDisappear(_ animated: Bool) {
|
|
super.viewWillDisappear(animated)
|
|
|
|
self.view.endEditing(true)
|
|
}
|
|
|
|
// MARK: IBActions
|
|
|
|
@IBAction private func touchCloseButton(_ sender: UIBarButtonItem) {
|
|
self.dismiss(animated: true, completion: nil)
|
|
}
|
|
|
|
// MARK: Other Functions
|
|
|
|
private func setup() {
|
|
// all setup should be done here
|
|
self.tableView.delegate = self
|
|
self.tableView.dataSource = self
|
|
self.searchBar.delegate = self
|
|
|
|
// self.setLargeTitle()
|
|
// self.setSearchBarAppearance()
|
|
}
|
|
|
|
private func setSearchBarAppearance() {
|
|
let textFieldInsideUISearchBar = self.searchBar.value(forKey: "searchField") as? UITextField
|
|
textFieldInsideUISearchBar?.font = .sanfrancisco(.regular, size: 14)
|
|
|
|
let textFieldInsideUISearchBarLabel = textFieldInsideUISearchBar!
|
|
.value(forKey: "placeholderLabel") as? UILabel
|
|
textFieldInsideUISearchBarLabel?.font = .sanfrancisco(.regular, size: 14)
|
|
}
|
|
|
|
private func clear() {
|
|
self.totalCount = 0
|
|
self.pageIndex = 0
|
|
self.jusoList.removeAll()
|
|
}
|
|
|
|
private func moveToTop() {
|
|
if self.isSearch {
|
|
let indexPath = NSIndexPath(row: NSNotFound, section: 0)
|
|
self.tableView.scrollToRow(at: indexPath as IndexPath, at: .top, animated: false)
|
|
self.isSearch = false
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: AddressSearchViewInterface
|
|
extension SearchAddressViewController: SearchAddressViewInterface {
|
|
func setJusoModel(with model: JusoResult?) {
|
|
self.model = model
|
|
}
|
|
|
|
func failure(with error: Error) {
|
|
self.alertWithOk(message: error.localizedDescription)
|
|
}
|
|
|
|
func startLoading() {
|
|
self.showProgressHud()
|
|
}
|
|
|
|
func endLoading() {
|
|
self.hideProgressHud()
|
|
}
|
|
}
|
|
|
|
extension SearchAddressViewController: UITableViewDataSource {
|
|
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
return jusoList.count
|
|
}
|
|
|
|
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
guard let cell = tableView.dequeueReusableCell(
|
|
withIdentifier: "AddressTableViewCell"
|
|
) as? AddressTableViewCell else {
|
|
return UITableViewCell()
|
|
}
|
|
|
|
cell.setModel(with: self.jusoList[indexPath.row])
|
|
|
|
return cell
|
|
}
|
|
}
|
|
|
|
extension SearchAddressViewController: UITableViewDelegate {
|
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
self.delegate?.searchAddress(viewController: self, selectedJuso: self.jusoList[indexPath.row])
|
|
}
|
|
|
|
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
|
|
if indexPath.row + 1 == self.jusoList.count &&
|
|
indexPath.row + 1 < self.totalCount {
|
|
self.pageIndex += 1
|
|
self.presenter?.fetchAddress(
|
|
pageIndex: self.pageIndex,
|
|
interval: self.interval,
|
|
keyword: self.searchedText
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
extension SearchAddressViewController: UISearchBarDelegate {
|
|
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
|
|
self.clear()
|
|
|
|
guard let address = searchBar.text else {
|
|
return
|
|
}
|
|
|
|
self.isSearch = true
|
|
self.searchedText = address
|
|
self.searchBar.resignFirstResponder()
|
|
}
|
|
}
|