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.

171 lines
4.8 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. // RewardViewController.swift
  3. // GME Remit
  4. //
  5. // Created by InKwon Devik Kim on 11/04/2019.
  6. //Copyright © 2019 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. class RewardViewController: UIViewController {
  10. // MARK: Properties
  11. var presenter: RewardModuleInterface?
  12. private var models: [RewardProduct]? {
  13. didSet {
  14. self.collectionView.reloadData()
  15. }
  16. }
  17. private var rewardPoint: String? {
  18. didSet {
  19. self.rewardPointLabel.text = self.rewardPoint
  20. }
  21. }
  22. private lazy var refreshControl = UIRefreshControl()
  23. // MARK: IBOutlets
  24. @IBOutlet weak var collectionView: UICollectionView!
  25. @IBOutlet weak var rewardPointLabel: UILabel!
  26. // MARK: VC's Life cycle
  27. override func viewDidLoad() {
  28. super.viewDidLoad()
  29. self.setup()
  30. }
  31. override func viewWillAppear(_ animated: Bool) {
  32. super.viewWillAppear(animated)
  33. self.navigationItem.title = "Reward"
  34. self.presenter?.viewIsReady()
  35. self.rewardPoint = UserDefaults.standard.string(forKey: UserKeys.rewardPoint)
  36. }
  37. override func viewWillDisappear(_ animated: Bool) {
  38. super.viewWillDisappear(animated)
  39. self.navigationItem.title = ""
  40. }
  41. // MARK: IBActions
  42. @IBAction func closeButtonTouch(_ sender: UIBarButtonItem) {
  43. self.dismiss(animated: true, completion: nil)
  44. }
  45. // MARK: Other Functions
  46. private func setup() {
  47. // all setup should be done here
  48. self.setDelegate()
  49. self.setCollectionViewLayout()
  50. self.addRefreshControlCollectionView()
  51. }
  52. private func setDelegate(){
  53. self.collectionView.delegate = self
  54. self.collectionView.dataSource = self
  55. }
  56. private func setCollectionViewLayout() {
  57. let cellSize = CGSize(
  58. width: (self.view.frame.width - 20) / 2 - 3.5,
  59. height: (self.view.frame.width - 20) / 2 - 3.5
  60. )
  61. let layout = UICollectionViewFlowLayout()
  62. layout.scrollDirection = .vertical
  63. layout.itemSize = cellSize
  64. layout.sectionInset = UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1)
  65. layout.minimumLineSpacing = 5.0
  66. layout.minimumInteritemSpacing = 5.0
  67. self.collectionView.setCollectionViewLayout(layout, animated: true)
  68. self.collectionView.reloadData()
  69. }
  70. private func addRefreshControlCollectionView() {
  71. let colorOption = [NSAttributedStringKey.foregroundColor : AppConstants.themWhiteColor];
  72. let title = NSAttributedString(string: "pull to refresh", attributes: colorOption)
  73. self.refreshControl.attributedTitle = title
  74. self.refreshControl.backgroundColor = AppConstants.themeBlueColor
  75. self.refreshControl.tintColor = AppConstants.themWhiteColor
  76. self.refreshControl.addTarget(self, action: #selector(self.refresh), for: .valueChanged)
  77. if #available(iOS 10.0, *) {
  78. self.collectionView.refreshControl = self.refreshControl
  79. } else {
  80. self.collectionView.addSubview(self.refreshControl)
  81. }
  82. }
  83. @objc
  84. private func refresh() {
  85. self.presenter?.viewIsReady()
  86. DispatchQueue.main.asyncAfter(deadline: .now() + 1.0){
  87. if #available(iOS 10.0, *) {
  88. self.collectionView.refreshControl?.endRefreshing()
  89. } else {
  90. self.refreshControl.endRefreshing()
  91. }
  92. }
  93. }
  94. }
  95. // MARK: RewardViewInterface
  96. extension RewardViewController: RewardViewInterface {
  97. func setRewardProducts(models: [RewardProduct]?) {
  98. self.models = models
  99. }
  100. func failure(error: Error) {
  101. self.alertWithOk(message: error.localizedDescription)
  102. }
  103. func goRedeemViewController(with model: RewardProduct?) {
  104. self.presenter?.goRedeemViewController(with: model)
  105. }
  106. func startLoading() {
  107. self.showProgressHud()
  108. }
  109. func endLoading() {
  110. self.hideProgressHud()
  111. }
  112. }
  113. extension RewardViewController: UICollectionViewDelegate {
  114. }
  115. extension RewardViewController: UICollectionViewDataSource {
  116. func collectionView(
  117. _ collectionView: UICollectionView,
  118. numberOfItemsInSection section: Int
  119. ) -> Int {
  120. return self.models?.count ?? 0
  121. }
  122. func collectionView(
  123. _ collectionView: UICollectionView,
  124. cellForItemAt indexPath: IndexPath
  125. ) -> UICollectionViewCell {
  126. let cell = collectionView.dequeueReusableCell(
  127. withReuseIdentifier: "RewardItemCollectionViewCell",
  128. for: indexPath
  129. ) as! RewardItemCollectionViewCell
  130. cell.setModel(with: self.models?[indexPath.row], delegate: self)
  131. return cell
  132. }
  133. }