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.

206 lines
5.5 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
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. import XLPagerTabStrip
  10. class RewardViewController: UIViewController {
  11. // MARK: Properties
  12. var presenter: RewardModuleInterface?
  13. private var models: [RewardProduct]? {
  14. didSet {
  15. collectionView.reloadData()
  16. }
  17. }
  18. private var rewardPoint: String? {
  19. didSet {
  20. rewardPointLabel.text = rewardPoint
  21. }
  22. }
  23. private lazy var refreshControl = UIRefreshControl()
  24. // MARK: IBOutlets
  25. @IBOutlet weak var collectionView: UICollectionView!
  26. @IBOutlet weak var rewardPointLabel: UILabel!
  27. @IBOutlet weak var rewardPointTitleLabel: UILabel!
  28. @IBOutlet weak var rewardNoteLabel: UILabel!
  29. @IBOutlet weak var rewardPointContainerView: UIView!
  30. @IBOutlet weak var layoutSegment: UISegmentedControl!
  31. // MARK: VC's Life cycle
  32. override func viewDidLoad() {
  33. super.viewDidLoad()
  34. setup()
  35. layoutSegment.addTarget(self, action: #selector(selectLayout(_:)), for: .valueChanged)
  36. }
  37. override func viewWillAppear(_ animated: Bool) {
  38. super.viewWillAppear(animated)
  39. title = "reward_group_title_text".localized()
  40. collectionView.setContentOffset(.zero, animated: false)
  41. rewardPoint = GMEDB.shared.user.string(.rewardPoint)?.likeCommaMoney()
  42. collectionView.reloadData()
  43. }
  44. override func viewWillDisappear(_ animated: Bool) {
  45. super.viewWillDisappear(animated)
  46. navigationItem.title = ""
  47. }
  48. // MARK: IBActions
  49. @objc func selectLayout(_ sender: UISegmentedControl) {
  50. setCollectionViewLayout(sender.selectedSegmentIndex)
  51. }
  52. // MARK: Other Functions
  53. private func setup() {
  54. // all setup should be done here
  55. setDelegate()
  56. setCollectionViewLayout(0)
  57. addRefreshControlCollectionView()
  58. setMultiLanguage()
  59. rewardPointContainerView.hero.id = "pointsView"
  60. presenter?.viewIsReady()
  61. }
  62. private func setDelegate(){
  63. collectionView.dataSource = self
  64. }
  65. private func setMultiLanguage() {
  66. rewardPointTitleLabel.text = "reward_points_text".localized()
  67. rewardNoteLabel.text = "reward_note_text".localized()
  68. }
  69. private func setCollectionViewLayout(_ type: Int) {
  70. let width: CGFloat
  71. let height: CGFloat
  72. switch type {
  73. case 0:
  74. width = (view.frame.width - 20) / 2 - 3.5
  75. height = (view.frame.width - 20) / 2 - 3.5 + 30
  76. case 1:
  77. width = (view.frame.width - 20)
  78. height = (( width * 260 ) / 320) + 5
  79. default:
  80. width = (view.frame.width - 20) / 2 - 3.5
  81. height = (view.frame.width - 20) / 2 - 3.5 + 30
  82. }
  83. let cellSize = CGSize(
  84. width: width,
  85. height: height
  86. )
  87. let layout = UICollectionViewFlowLayout()
  88. layout.scrollDirection = .vertical
  89. layout.itemSize = cellSize
  90. layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  91. layout.minimumLineSpacing = 5.0
  92. layout.minimumInteritemSpacing = 5.0
  93. collectionView.setCollectionViewLayout(layout, animated: true)
  94. collectionView.reloadData()
  95. collectionView.collectionViewLayout.invalidateLayout()
  96. }
  97. private func addRefreshControlCollectionView() {
  98. let colorOption = [NSAttributedString.Key.foregroundColor : AppConstants.themWhiteColor];
  99. let title = NSAttributedString(string: "pull to refresh", attributes: colorOption)
  100. refreshControl.attributedTitle = title
  101. refreshControl.backgroundColor = AppConstants.themeBlueColor
  102. refreshControl.tintColor = AppConstants.themWhiteColor
  103. refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged)
  104. if #available(iOS 10.0, *) {
  105. collectionView.refreshControl = refreshControl
  106. } else {
  107. collectionView.addSubview(refreshControl)
  108. }
  109. }
  110. @objc
  111. private func refresh() {
  112. self.presenter?.viewIsReady()
  113. DispatchQueue.main.asyncAfter(deadline: .now() + 1.0){[weak self] in
  114. guard let `self` = self else {return}
  115. if #available(iOS 10.0, *) {
  116. self.collectionView.refreshControl?.endRefreshing()
  117. } else {
  118. self.refreshControl.endRefreshing()
  119. }
  120. }
  121. }
  122. }
  123. // MARK: RewardViewInterface
  124. extension RewardViewController: RewardViewInterface {
  125. func setRewardProducts(models: [RewardProduct]?) {
  126. self.models = models
  127. }
  128. func failure(error: Error) {
  129. alertWithOk(message: error.localizedDescription)
  130. }
  131. func goRedeemViewController(with model: RewardProduct?) {
  132. presenter?.goRedeemViewController(with: model)
  133. }
  134. func startLoading() {
  135. showProgressHud()
  136. }
  137. func endLoading() {
  138. hideProgressHud()
  139. }
  140. }
  141. extension RewardViewController: UICollectionViewDataSource {
  142. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  143. return models?.count ?? 0
  144. }
  145. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  146. let cell = collectionView.dequeueReusableCell(
  147. withReuseIdentifier: "RewardItemCollectionViewCell",
  148. for: indexPath
  149. ) as! RewardItemCollectionViewCell
  150. cell.setModel(with: models?[indexPath.row], delegate: self)
  151. return cell
  152. }
  153. }
  154. // MARK: - XLPagerTabStrip's IndicatorInfoProvider
  155. extension RewardViewController: IndicatorInfoProvider {
  156. func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo {
  157. return IndicatorInfo.init(title: "products_text".localized())
  158. }
  159. }