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.

195 lines
6.8 KiB

6 years ago
  1. //
  2. // RewardViewController.swift
  3. // GMERemittance
  4. //
  5. // Created by Sujal on 3/14/18.
  6. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. import SDWebImage
  10. class RewardViewController: UIViewController {
  11. @IBOutlet weak var rewardCollectionView: UICollectionView!
  12. @IBOutlet weak var labelRewardPoint: UILabel!
  13. let itemsPerRow: CGFloat = 2
  14. let insetValue: CGFloat = 7
  15. var availablePoints: String!
  16. static var notificationCode: String = ""
  17. private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
  18. private var rewardviewmodel = RewardViewModel()
  19. public var fromNotification: Int?
  20. public static var rewardConnectionTimeOutCheck = 0
  21. override func viewDidAppear(_ animated: Bool) {
  22. setUpNetworkListener()
  23. setUpAnotherLoginListener(genericviewmodel: rewardviewmodel)
  24. }
  25. override func viewDidLoad() {
  26. super.viewDidLoad()
  27. if RewardViewController.notificationCode == "p700" {
  28. setUpNavBar(id: 207, title: "Reward")
  29. }else {
  30. setUpNavBar(id: 201, title: "Reward")
  31. }
  32. setUpListener()
  33. availablePoints = UserDefaults.standard.object(forKey: "com.gmeremit.rewardPoint") as! String
  34. labelRewardPoint.text = availablePoints
  35. rewardCollectionView.delegate = self
  36. rewardCollectionView.dataSource = self
  37. showActivityIndicator(activityIndicator: activityIndicator)
  38. disableUserInteractions()
  39. rewardviewmodel.rewardConnectionTimeOut.value = nil
  40. /**
  41. connection timeout
  42. */
  43. rewardviewmodel.rewardConnectionTimeOut.bind { [unowned self] in
  44. guard $0 != nil else {
  45. return
  46. }
  47. self.enableUserInteractions()
  48. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  49. if self.fromNotification != 0{
  50. if RewardViewController.rewardConnectionTimeOutCheck == 0{
  51. RewardViewController.rewardConnectionTimeOutCheck = RewardViewController.rewardConnectionTimeOutCheck+1
  52. self.popUpMessage(value: 37)
  53. }
  54. }
  55. }
  56. rewardviewmodel.fetchRewards()
  57. }
  58. /**
  59. Internet Check
  60. */
  61. func setUpNetworkListener() {
  62. rewardviewmodel.internetConnection.bind { [unowned self] in
  63. guard $0 != nil else {
  64. return
  65. }
  66. self.enableUserInteractions()
  67. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  68. self.popUpMessage(value: 15)
  69. }
  70. }
  71. /**
  72. Update reward data in view
  73. */
  74. func setUpListener() {
  75. rewardviewmodel.rewardsAvailable.bind { [unowned self] in
  76. guard $0 != nil else {
  77. return
  78. }
  79. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  80. self.enableUserInteractions()
  81. if $0 == 0 {
  82. self.popUpMessageError(value: 10, message: self.rewardviewmodel.getErrorMessage())
  83. } else if $0 == 1 {
  84. self.rewardCollectionView.reloadData()
  85. }
  86. self.rewardviewmodel.rewardsAvailable.value = nil
  87. }
  88. }
  89. override func didReceiveMemoryWarning() {
  90. super.didReceiveMemoryWarning()
  91. // Dispose of any resources that can be recreated.
  92. }
  93. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  94. let redeemViewController
  95. = segue.destination as! RedeemViewController
  96. redeemViewController.rewardviewmodel = self.rewardviewmodel
  97. }
  98. }
  99. extension RewardViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  100. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  101. return rewardviewmodel.getRewardCount()
  102. }
  103. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  104. let rewardCell = collectionView.dequeueReusableCell(withReuseIdentifier: "rewardCollectionViewCell", for: indexPath) as! RewardCollectionViewCell
  105. if let rewardImageString = rewardviewmodel.getRewardImage(index: indexPath.row) {
  106. if let rewardImageUrl = URL(string: rewardImageString) {
  107. rewardCell.imageViewReward.sd_setImage(with: rewardImageUrl, placeholderImage: nil, options: [.progressiveDownload,.scaleDownLargeImages], completed: nil)
  108. }
  109. }
  110. rewardCell.labelRewardName.text = rewardviewmodel.getRewardName(index: indexPath.row)
  111. let redeemPoint = rewardviewmodel.getRedeemPoint(index: indexPath.row)
  112. rewardCell.labelRewardPoint.text = redeemPoint + " " + "Points"
  113. if Float(redeemPoint)! <= Float(availablePoints)! {
  114. rewardCell.buttonRedeem.backgroundColor = UIColor(hex: 0xec1c24)
  115. } else {
  116. rewardCell.buttonRedeem.isUserInteractionEnabled = false
  117. }
  118. rewardCell.delegate = self
  119. return rewardCell
  120. }
  121. func collectionView(_ collectionView: UICollectionView,
  122. layout collectionViewLayout: UICollectionViewLayout,
  123. sizeForItemAt indexPath: IndexPath) -> CGSize {
  124. let widthPerItem = (collectionView.frame.width - insetValue * 2 - insetValue) / itemsPerRow
  125. return CGSize(width: widthPerItem, height: 1.1 * widthPerItem)
  126. }
  127. func collectionView(_ collectionView: UICollectionView,
  128. layout collectionViewLayout: UICollectionViewLayout,
  129. insetForSectionAt section: Int) -> UIEdgeInsets {
  130. return UIEdgeInsets(top: insetValue, left: insetValue, bottom: insetValue, right: insetValue)
  131. }
  132. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  133. return insetValue
  134. }
  135. func collectionView(_ collectionView: UICollectionView,
  136. layout collectionViewLayout: UICollectionViewLayout,
  137. minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  138. return insetValue
  139. }
  140. }
  141. extension RewardViewController: RedeemRewardDelegate {
  142. func redeemReward(_ sender: RewardCollectionViewCell) {
  143. guard let indexPath = rewardCollectionView.indexPath(for: sender) else {
  144. return
  145. }
  146. rewardviewmodel.setSelectedRewardIndex(index: indexPath.row)
  147. }
  148. }