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.

165 lines
3.9 KiB

  1. //
  2. // SelectCouponViewController.swift
  3. // GME Remit
  4. //
  5. // Created by InKwon Devik Kim on 30/05/2019.
  6. //Copyright © 2019 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. import PanModal
  10. protocol SelectCouponViewDelegate: class {
  11. func viewController(
  12. _ viewController: SelectCouponViewController,
  13. didSelect coupon: Coupon?
  14. )
  15. }
  16. class SelectCouponViewController: UIViewController {
  17. // MARK: Properties
  18. var presenter: SelectCouponModuleInterface?
  19. weak var delegate: SelectCouponViewDelegate?
  20. // MARK: Computed Properties
  21. var coupons: [Coupon]? {
  22. didSet {
  23. if (coupons?.count ?? 0) == 0 {
  24. noCouponMessageLabel.alpha = 1
  25. } else {
  26. noCouponMessageLabel.alpha = 0
  27. }
  28. tableView.reloadData()
  29. }
  30. }
  31. var selectedCoupon: Coupon? {
  32. didSet {
  33. let isUseCoupon = selectedCoupon != nil ? true : false
  34. couponButton.isEnabled = isUseCoupon
  35. couponButton.backgroundColor = isUseCoupon ? .themeRed : .lightGray
  36. }
  37. }
  38. // MARK: IBOutlets
  39. @IBOutlet weak var registCouponBackgroundView: UIView!
  40. @IBOutlet weak var registCouponTextField: UITextField!
  41. @IBOutlet weak var registCouponButton: UIButton!
  42. @IBOutlet weak var tableView: UITableView!
  43. @IBOutlet weak var couponButton: UIButton!
  44. @IBOutlet weak var noCouponMessageLabel: UILabel!
  45. // MARK: VC's Life cycle
  46. override func viewDidLoad() {
  47. super.viewDidLoad()
  48. setup()
  49. tableView.dataSource = self
  50. tableView.delegate = self
  51. couponButton.layer.cornerRadius = 5
  52. presenter?.fetchCoupons()
  53. }
  54. override func viewWillAppear(_ animated: Bool) {
  55. super.viewWillAppear(animated)
  56. title = "Coupons"
  57. }
  58. // MARK: IBActions
  59. @IBAction func touchUseCouponButton(_ sender: UIButton) {
  60. delegate?.viewController(self, didSelect: selectedCoupon)
  61. }
  62. }
  63. // MARK: SelectCouponViewInterface
  64. extension SelectCouponViewController: SelectCouponViewInterface {
  65. func setCoupons(didSelect coupon: Coupon?, with model: [Coupon]?) {
  66. selectedCoupon = coupon
  67. coupons = model
  68. }
  69. func error(with error: Error) {
  70. alert(type: .error, message: error.localizedDescription)
  71. }
  72. func startLoading() {
  73. showProgressHud()
  74. }
  75. func endLoading() {
  76. hideProgressHud()
  77. }
  78. }
  79. // MARK: Other Functions
  80. extension SelectCouponViewController {
  81. private func setup() {
  82. // all setup should be done here
  83. }
  84. }
  85. // MARK: - UITableViewDataSource
  86. extension SelectCouponViewController: UITableViewDataSource {
  87. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  88. return coupons?.count ?? 0
  89. }
  90. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  91. guard let cell = tableView.dequeueReusableCell(withIdentifier: "CouponCell") as? CouponCell else {
  92. return UITableViewCell()
  93. }
  94. cell.setModel(coupons?[indexPath.row])
  95. return cell
  96. }
  97. }
  98. extension SelectCouponViewController: UITableViewDelegate {
  99. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  100. guard let cell = tableView.cellForRow(at: indexPath) as? CouponCell else {
  101. return
  102. }
  103. if cell.isSelectedCoupon {
  104. cell.isSelectedCoupon = false
  105. selectedCoupon = nil
  106. } else {
  107. cell.isSelectedCoupon = true
  108. selectedCoupon = coupons?[indexPath.row]
  109. }
  110. }
  111. func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
  112. guard let cell = tableView.cellForRow(at: indexPath) as? CouponCell else {
  113. return
  114. }
  115. cell.isSelectedCoupon = false
  116. selectedCoupon = nil
  117. }
  118. func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
  119. print("Highlight idx: \(indexPath.row)")
  120. }
  121. }
  122. // MARK: - PanModalPresenter
  123. extension SelectCouponViewController: PanModalPresentable {
  124. var panScrollable: UIScrollView? {
  125. return self.tableView
  126. }
  127. var longFormHeight: PanModalHeight {
  128. return .maxHeight
  129. }
  130. }