// // CouponBoxViewController.swift // GME Remit // // Created by Jeongbae Kong on 03/12/2019. //Copyright © 2019 Gobal Money Express Co. Ltd. All rights reserved. // import UIKit import XLPagerTabStrip protocol CouponBoxDelegate: class { func couponBoxView( _ viewController: CouponBoxViewController, didSelectModel model: CouponBoxModel? ) } class CouponBoxViewController: UIViewController { // MARK: Properties weak var delegate: CouponBoxDelegate? private var couponList: [CouponBoxModel]? { didSet { let message = "possessed_coupon_number_text".localized().replacingOccurrences( of: "xxx", with: "\(couponList?.count ?? 0)") couponCount.text = message couponCount.font = .sanfrancisco(.regular, size: 15) if couponList?.count != 0 { //쿠폰리스트가 있으면, noCouponView.isHidden = true } else { noCouponView.isHidden = false } tableView.reloadData() } } var presenter: CouponBoxModuleInterface? private lazy var refreshControl = UIRefreshControl() // MARK: Computed Properties // MARK: IBOutlets @IBOutlet weak var tableView: UITableView! @IBOutlet weak var noCouponView: UIView! @IBOutlet weak var couponCount: UILabel! @IBOutlet weak var couponGuidelineText: UIButton! @IBOutlet weak var couponCountStackView: UIStackView! @IBOutlet weak var noCouponMessageLabel: UILabel! // MARK: VC's Life cycle override func viewDidLoad() { super.viewDidLoad() setup() setUI() } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) presenter?.fetchCouponBox() } // MARK: IBActions @IBAction func couponGuidelineButton(_ sender: UIButton) { // presenter?.goCouponGuideline() } } // MARK: CouponBoxViewInterface extension CouponBoxViewController: CouponBoxViewInterface { func setModel(with model: [CouponBoxModel]?) { couponList = model } func setError(with error: Error) { alert(message: error.localizedDescription) } func startLoading() { showProgressHud() } func endLoading() { hideProgressHud() } } // MARK: Other Functions extension CouponBoxViewController { private func setup() { initDelegate() addRefreshControlCollectionView() noCouponMessageLabel.text = "no_coupon_text".localized() // all setup should be done here } private func initDelegate() { tableView.dataSource = self tableView.delegate = self } private func setUI() { couponGuidelineText.setTitle("coupon_guideline_title_text".localized(), for: .normal) couponGuidelineText.titleLabel?.font = .sanfrancisco(.regular, size: 13) couponGuidelineText.backgroundColor = .clear couponCountStackView.backgroundColor = .clear } private func addRefreshControlCollectionView() { let colorOption = [NSAttributedString.Key.foregroundColor : UIColor.themeWhite] let title = NSAttributedString(string: "pull to refresh", attributes: colorOption) refreshControl.attributedTitle = title refreshControl.backgroundColor = .themeBlue refreshControl.tintColor = .themeWhite refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged) if #available(iOS 10.0, *) { tableView.refreshControl = refreshControl } else { tableView.addSubview(refreshControl) } } @objc private func refresh() { self.presenter?.fetchCouponBox() DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {[weak self] in self?.tableView.refreshControl?.endRefreshing() } } } // MARK: - XLPagerTabStrip'`s IndicatorInfoProvider extension CouponBoxViewController: IndicatorInfoProvider { func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo { return IndicatorInfo.init(title: "couponbox_title_text".localized()) } } // MARK: - UITableViewDataSource extension CouponBoxViewController: UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return couponList?.count ?? 0 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { guard let cell = tableView.dequeueReusableCell(withIdentifier: "CouponCell") as? CouponCell else { return CouponBoxTableViewCell() } cell.setModel(couponList?[indexPath.row]) return cell } } extension CouponBoxViewController: UITableViewDelegate { func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { let result = UIView() // recreate insets from existing ones in the table view let insets = tableView.separatorInset let width = tableView.bounds.width - insets.left - insets.right let sepFrame = CGRect(x: insets.left, y: -0.5, width: width, height: 0.5) // create layer with separator, setting color let sep = CALayer() sep.frame = sepFrame sep.backgroundColor = tableView.separatorColor?.cgColor result.layer.addSublayer(sep) return result } }