// // SelectCouponViewController.swift // GME Remit // // Created by InKwon Devik Kim on 30/05/2019. //Copyright © 2019 Gobal Money Express Co. Ltd. All rights reserved. // import UIKit import PanModal protocol SelectCouponViewDelegate: class { func viewController( _ viewController: SelectCouponViewController, didSelect coupon: Coupon? ) } class SelectCouponViewController: UIViewController { // MARK: Properties var presenter: SelectCouponModuleInterface? weak var delegate: SelectCouponViewDelegate? // MARK: Computed Properties var coupons: [Coupon]? { didSet { if (coupons?.count ?? 0) == 0 { noCouponMessageLabel.alpha = 1 } else { noCouponMessageLabel.alpha = 0 } tableView.reloadData() } } var selectedCoupon: Coupon? { didSet { let isUseCoupon = selectedCoupon != nil ? true : false couponButton.isEnabled = isUseCoupon couponButton.backgroundColor = isUseCoupon ? .themeRed : .lightGray } } // MARK: IBOutlets @IBOutlet weak var registCouponBackgroundView: UIView! @IBOutlet weak var registCouponTextField: UITextField! @IBOutlet weak var registCouponButton: UIButton! @IBOutlet weak var tableView: UITableView! @IBOutlet weak var couponButton: UIButton! @IBOutlet weak var noCouponMessageLabel: UILabel! // MARK: VC's Life cycle override func viewDidLoad() { super.viewDidLoad() setup() tableView.dataSource = self tableView.delegate = self couponButton.layer.cornerRadius = 5 presenter?.fetchCoupons() } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) title = "Coupons" } // MARK: IBActions @IBAction func touchUseCouponButton(_ sender: UIButton) { delegate?.viewController(self, didSelect: selectedCoupon) } } // MARK: SelectCouponViewInterface extension SelectCouponViewController: SelectCouponViewInterface { func setCoupons(didSelect coupon: Coupon?, with model: [Coupon]?) { selectedCoupon = coupon coupons = model } func error(with error: Error) { alert(type: .error, message: error.localizedDescription) } func startLoading() { showProgressHud() } func endLoading() { hideProgressHud() } } // MARK: Other Functions extension SelectCouponViewController { private func setup() { // all setup should be done here } } // MARK: - UITableViewDataSource extension SelectCouponViewController: UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return coupons?.count ?? 0 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { guard let cell = tableView.dequeueReusableCell(withIdentifier: "CouponCell") as? CouponCell else { return UITableViewCell() } cell.setModel(coupons?[indexPath.row]) return cell } } extension SelectCouponViewController: UITableViewDelegate { func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { guard let cell = tableView.cellForRow(at: indexPath) as? CouponCell else { return } if cell.isSelectedCoupon { cell.isSelectedCoupon = false selectedCoupon = nil } else { cell.isSelectedCoupon = true selectedCoupon = coupons?[indexPath.row] } } func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { guard let cell = tableView.cellForRow(at: indexPath) as? CouponCell else { return } cell.isSelectedCoupon = false selectedCoupon = nil } func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) { print("Highlight idx: \(indexPath.row)") } } // MARK: - PanModalPresenter extension SelectCouponViewController: PanModalPresentable { var panScrollable: UIScrollView? { return self.tableView } var longFormHeight: PanModalHeight { return .maxHeight } }