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.

210 lines
5.6 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. layoutSegment.tintColor = .themeRed
  62. layoutSegment.backgroundColor = .clear
  63. collectionView.layer.cornerRadius = 5
  64. }
  65. private func setDelegate() {
  66. collectionView.dataSource = self
  67. }
  68. private func setMultiLanguage() {
  69. rewardPointTitleLabel.text = "reward_points_text".localized()
  70. rewardNoteLabel.text = "reward_note_text".localized()
  71. }
  72. private func setCollectionViewLayout(_ type: Int) {
  73. let width: CGFloat
  74. let height: CGFloat
  75. switch type {
  76. case 0:
  77. width = (view.frame.width - 30) / 2 - 3.5
  78. height = (view.frame.width - 30) / 2 - 3.5 + 30
  79. case 1:
  80. width = (view.frame.width - 30)
  81. height = (( width * 260 ) / 320) + 5
  82. default:
  83. width = (view.frame.width - 30) / 2 - 3.5
  84. height = (view.frame.width - 30) / 2 - 3.5 + 30
  85. }
  86. let cellSize = CGSize(
  87. width: width,
  88. height: height
  89. )
  90. let layout = UICollectionViewFlowLayout()
  91. layout.scrollDirection = .vertical
  92. layout.itemSize = cellSize
  93. layout.sectionInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
  94. layout.minimumLineSpacing = 5.0
  95. layout.minimumInteritemSpacing = 5.0
  96. collectionView.setCollectionViewLayout(layout, animated: true)
  97. collectionView.reloadData()
  98. collectionView.collectionViewLayout.invalidateLayout()
  99. }
  100. private func addRefreshControlCollectionView() {
  101. let colorOption = [NSAttributedString.Key.foregroundColor : UIColor.themeWhite]
  102. let title = NSAttributedString(string: "pull to refresh", attributes: colorOption)
  103. refreshControl.attributedTitle = title
  104. refreshControl.backgroundColor = .themeBlue
  105. refreshControl.tintColor = .themeWhite
  106. refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged)
  107. if #available(iOS 10.0, *) {
  108. collectionView.refreshControl = refreshControl
  109. } else {
  110. collectionView.addSubview(refreshControl)
  111. }
  112. }
  113. @objc
  114. private func refresh() {
  115. self.presenter?.viewIsReady()
  116. DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {[weak self] in
  117. guard let `self` = self else {return}
  118. if #available(iOS 10.0, *) {
  119. self.collectionView.refreshControl?.endRefreshing()
  120. } else {
  121. self.refreshControl.endRefreshing()
  122. }
  123. }
  124. }
  125. }
  126. // MARK: RewardViewInterface
  127. extension RewardViewController: RewardViewInterface {
  128. func setRewardProducts(models: [RewardProduct]?) {
  129. self.models = models
  130. }
  131. func failure(error: Error) {
  132. alertWithOk(message: error.localizedDescription)
  133. }
  134. func goRedeemViewController(with model: RewardProduct?) {
  135. presenter?.goRedeemViewController(with: model)
  136. }
  137. func startLoading() {
  138. showProgressHud()
  139. }
  140. func endLoading() {
  141. hideProgressHud()
  142. }
  143. }
  144. extension RewardViewController: UICollectionViewDataSource {
  145. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  146. return models?.count ?? 0
  147. }
  148. func collectionView(
  149. _ collectionView: UICollectionView,
  150. cellForItemAt indexPath: IndexPath
  151. ) -> UICollectionViewCell {
  152. guard let cell = collectionView.dequeueReusableCell(
  153. withReuseIdentifier: "RewardItemCollectionViewCell",
  154. for: indexPath
  155. ) as? RewardItemCollectionViewCell else {
  156. return UICollectionViewCell()
  157. }
  158. cell.setModel(with: models?[indexPath.row], delegate: self)
  159. return cell
  160. }
  161. }
  162. // MARK: - XLPagerTabStrip's IndicatorInfoProvider
  163. extension RewardViewController: IndicatorInfoProvider {
  164. func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo {
  165. return IndicatorInfo.init(title: "products_text".localized())
  166. }
  167. }