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.

203 lines
8.3 KiB

6 years ago
6 years ago
  1. //
  2. // HomeCollectionTableViewCell.swift
  3. // GMERemittance
  4. //
  5. // Created by gme_2 on 27/09/2018.
  6. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. import Localize_Swift
  10. struct MenuNotificationName {
  11. static let sendMoney = "SendMoeny"
  12. static let mobileRecharge = "mobileRecharge"
  13. static let todaysRate = "todaysRate"
  14. static let resend = "resend"
  15. static let transactionStatement = "transactionStatement"
  16. static let walletToWallet = "walletToWallet"
  17. static let collectionHeight = "height_height"
  18. }
  19. class HomeCollectionTableViewCell: UITableViewCell, UICollectionViewDelegateFlowLayout {
  20. struct StringConstants {
  21. let sendMoneyText = "send_money_text".localized()
  22. let todaysRateText = "todays_rate_title_text".localized()
  23. let transactionReportText = "transaction_report_title_text".localized()
  24. let walletStatement = "gme_wallet_statement_text".localized()
  25. }
  26. enum CollectionMenus: Int {
  27. case sendMoney = 0
  28. case mobileRecharge
  29. case todaysRate
  30. case walletStatement
  31. case resend
  32. case walletToWallet
  33. }
  34. @IBOutlet weak var collectionView: UICollectionView!
  35. private var menus: [HomeCollectionModel]?
  36. var parentViewController: HomeViewController?
  37. override func awakeFromNib() {
  38. super.awakeFromNib()
  39. // Initialization code
  40. self.collectionView.delegate = self
  41. self.collectionView.dataSource = self
  42. }
  43. func setup() {
  44. collectionView.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.old, context: nil)
  45. NotificationCenter.default.addObserver(self, selector: #selector(configureLanguage), name: NSNotification.Name(LCLLanguageChangeNotification), object: nil)
  46. self.menus = self.configureMenu()
  47. }
  48. @objc func configureLanguage() {
  49. self.collectionView.reloadData()
  50. }
  51. private func configureMenu() -> [HomeCollectionModel] {
  52. let sendMoneyMenu = HomeCollectionModel()
  53. sendMoneyMenu.index = 0
  54. let text = "send_money_text".localized()
  55. sendMoneyMenu.title = text
  56. sendMoneyMenu.icon = #imageLiteral(resourceName: "send-money")
  57. let mobileRechargeMenu = HomeCollectionModel()
  58. mobileRechargeMenu.index = 1
  59. mobileRechargeMenu.title = "Mobile Recharge"
  60. mobileRechargeMenu.icon = #imageLiteral(resourceName: "mobile-recharge")
  61. let toadaysRateMenu = HomeCollectionModel()
  62. toadaysRateMenu.index = 2
  63. toadaysRateMenu.title = "todays_rate_text".localized()
  64. toadaysRateMenu.icon = #imageLiteral(resourceName: "rate-today")
  65. let trackTransferMenu = HomeCollectionModel()
  66. trackTransferMenu.index = 3
  67. trackTransferMenu.title = "gme_wallet_statement_text".localized()
  68. let walletImage = UIImage.init(named: "ic_menu_wallet")
  69. trackTransferMenu.icon = walletImage
  70. let transactionStatementMenu = HomeCollectionModel()
  71. transactionStatementMenu.index = 4
  72. transactionStatementMenu.title = "resend_money_text".localized()
  73. let image = UIImage.init(named: "ic_menu_sendmoney")
  74. transactionStatementMenu.icon = image
  75. let walletToWalletMenu = HomeCollectionModel()
  76. walletToWalletMenu.index = 5
  77. walletToWalletMenu.title = "Wallet to Wallet Transfer"
  78. walletToWalletMenu.icon = #imageLiteral(resourceName: "wallet-transfer")
  79. let menus = [sendMoneyMenu, toadaysRateMenu, transactionStatementMenu, trackTransferMenu]
  80. return menus
  81. }
  82. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  83. if let observedObject = object as? UICollectionView, observedObject == collectionView {
  84. let height = self.collectionView.contentSize.height
  85. let finalHeight = height + CGFloat(20)
  86. NotificationCenter.default.post(name: self.getCollectionHeightNotificationName(), object: nil, userInfo: [MenuNotificationName.collectionHeight : finalHeight])
  87. }
  88. }
  89. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  90. let device = Utility.getDeviceModel()
  91. if device == .iphone5 {
  92. let itemsPerRow: CGFloat = 2
  93. let paddingSpace: CGFloat = CGFloat(20 * (itemsPerRow + 1))
  94. let availableWidth = frame.width - paddingSpace
  95. let widthPerItem = availableWidth / itemsPerRow
  96. return CGSize(width: widthPerItem, height: widthPerItem * 1.2)
  97. }else {
  98. let itemsPerRow: CGFloat = 2
  99. let paddingSpace: CGFloat = CGFloat(25 * (itemsPerRow + 1))
  100. let availableWidth = frame.width - paddingSpace
  101. let widthPerItem = availableWidth / itemsPerRow
  102. return CGSize(width: widthPerItem, height: widthPerItem * 1.1)
  103. }
  104. }
  105. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
  106. if Utility.getDeviceModel() == .iphone5 {
  107. return UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
  108. }else {
  109. return UIEdgeInsets(top: 20, left: 15, bottom: 10, right: 15)
  110. }
  111. }
  112. }
  113. extension HomeCollectionTableViewCell: UICollectionViewDataSource {
  114. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  115. return menus?.count ?? 0
  116. }
  117. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  118. let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "HomeCollectionCollectionViewCell", for: indexPath) as! HomeCollectionCollectionViewCell
  119. let menus = self.configureMenu()
  120. cell.model = menus.elementAt(index: indexPath.row)
  121. cell.setup()
  122. return cell
  123. }
  124. }
  125. extension HomeCollectionTableViewCell: UICollectionViewDelegate {
  126. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  127. guard let index = self.menus?.elementAt(index: indexPath.row)?.index else {return}
  128. guard let _index = CollectionMenus.init(rawValue: index) else {return }
  129. let center = NotificationCenter.default
  130. switch _index {
  131. case .sendMoney:
  132. center.post(name: self.getSendMoneyNotificationName(), object: nil)
  133. case .mobileRecharge:
  134. center.post(name: self.getMobileRechargeNotificationName(), object: nil)
  135. case .todaysRate:
  136. center.post(name: self.getTodaysRateNotificationName(), object: nil)
  137. case .walletStatement:
  138. center.post(name: self.getTrackYourTransferNotificationName(), object: nil)
  139. case .resend:
  140. center.post(name: self.getResendNotificationName(), object: nil)
  141. case .walletToWallet:
  142. center.post(name: self.getWalletToWalletNotificationName(), object: nil)
  143. }
  144. }
  145. func getSendMoneyNotificationName() -> Notification.Name {
  146. return Notification.Name.init(rawValue: MenuNotificationName.sendMoney)
  147. }
  148. func getMobileRechargeNotificationName() -> Notification.Name {
  149. return Notification.Name.init(rawValue: MenuNotificationName.mobileRecharge)
  150. }
  151. func getTodaysRateNotificationName() -> Notification.Name {
  152. return Notification.Name.init(rawValue: MenuNotificationName.todaysRate)
  153. }
  154. func getTrackYourTransferNotificationName() -> Notification.Name {
  155. return Notification.Name.init(rawValue: MenuNotificationName.transactionStatement)
  156. }
  157. func getResendNotificationName() -> Notification.Name {
  158. return Notification.Name.init(rawValue: MenuNotificationName.resend)
  159. }
  160. func getWalletToWalletNotificationName() -> Notification.Name {
  161. return Notification.Name.init(rawValue: MenuNotificationName.walletToWallet)
  162. }
  163. func getCollectionHeightNotificationName() -> Notification.Name {
  164. return Notification.Name.init(rawValue: MenuNotificationName.collectionHeight)
  165. }
  166. }