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.

201 lines
8.3 KiB

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 trackYourTransfer = "trackYourTransfer"
  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 trackYourTransfer
  31. case transationStatement
  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. trackTransferMenu.icon = #imageLiteral(resourceName: "track-transfer")
  69. let transactionStatementMenu = HomeCollectionModel()
  70. transactionStatementMenu.index = 4
  71. transactionStatementMenu.title = "transaction_report_text".localized()
  72. transactionStatementMenu.icon = #imageLiteral(resourceName: "transaction-statement")
  73. let walletToWalletMenu = HomeCollectionModel()
  74. walletToWalletMenu.index = 5
  75. walletToWalletMenu.title = "Wallet to Wallet Transfer"
  76. walletToWalletMenu.icon = #imageLiteral(resourceName: "wallet-transfer")
  77. let menus = [sendMoneyMenu, toadaysRateMenu, transactionStatementMenu, trackTransferMenu]
  78. return menus
  79. }
  80. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  81. if let observedObject = object as? UICollectionView, observedObject == collectionView {
  82. let height = self.collectionView.contentSize.height
  83. let finalHeight = height + CGFloat(20)
  84. NotificationCenter.default.post(name: self.getCollectionHeightNotificationName(), object: nil, userInfo: [MenuNotificationName.collectionHeight : finalHeight])
  85. }
  86. }
  87. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  88. let device = Utility.getDeviceModel()
  89. if device == .iphone5 {
  90. let itemsPerRow: CGFloat = 2
  91. let paddingSpace: CGFloat = CGFloat(20 * (itemsPerRow + 1))
  92. let availableWidth = frame.width - paddingSpace
  93. let widthPerItem = availableWidth / itemsPerRow
  94. return CGSize(width: widthPerItem, height: widthPerItem * 1.2)
  95. }else {
  96. let itemsPerRow: CGFloat = 2
  97. let paddingSpace: CGFloat = CGFloat(25 * (itemsPerRow + 1))
  98. let availableWidth = frame.width - paddingSpace
  99. let widthPerItem = availableWidth / itemsPerRow
  100. return CGSize(width: widthPerItem, height: widthPerItem * 1.1)
  101. }
  102. }
  103. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
  104. if Utility.getDeviceModel() == .iphone5 {
  105. return UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
  106. }else {
  107. return UIEdgeInsets(top: 20, left: 15, bottom: 10, right: 15)
  108. }
  109. }
  110. }
  111. extension HomeCollectionTableViewCell: UICollectionViewDataSource {
  112. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  113. return menus?.count ?? 0
  114. }
  115. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  116. let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "HomeCollectionCollectionViewCell", for: indexPath) as! HomeCollectionCollectionViewCell
  117. let menus = self.configureMenu()
  118. cell.model = menus.elementAt(index: indexPath.row)
  119. cell.setup()
  120. return cell
  121. }
  122. }
  123. extension HomeCollectionTableViewCell: UICollectionViewDelegate {
  124. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  125. guard let index = self.menus?.elementAt(index: indexPath.row)?.index else {return}
  126. guard let _index = CollectionMenus.init(rawValue: index) else {return }
  127. let center = NotificationCenter.default
  128. switch _index {
  129. case .sendMoney:
  130. center.post(name: self.getSendMoneyNotificationName(), object: nil)
  131. case .mobileRecharge:
  132. center.post(name: self.getMobileRechargeNotificationName(), object: nil)
  133. case .todaysRate:
  134. center.post(name: self.getTodaysRateNotificationName(), object: nil)
  135. case .trackYourTransfer:
  136. center.post(name: self.getTrackYourTransferNotificationName(), object: nil)
  137. case .transationStatement:
  138. center.post(name: self.getTransactionStatementNotificationName(), object: nil)
  139. case .walletToWallet:
  140. center.post(name: self.getWalletToWalletNotificationName(), object: nil)
  141. }
  142. }
  143. func getSendMoneyNotificationName() -> Notification.Name {
  144. return Notification.Name.init(rawValue: MenuNotificationName.sendMoney)
  145. }
  146. func getMobileRechargeNotificationName() -> Notification.Name {
  147. return Notification.Name.init(rawValue: MenuNotificationName.mobileRecharge)
  148. }
  149. func getTodaysRateNotificationName() -> Notification.Name {
  150. return Notification.Name.init(rawValue: MenuNotificationName.todaysRate)
  151. }
  152. func getTrackYourTransferNotificationName() -> Notification.Name {
  153. return Notification.Name.init(rawValue: MenuNotificationName.trackYourTransfer)
  154. }
  155. func getTransactionStatementNotificationName() -> Notification.Name {
  156. return Notification.Name.init(rawValue: MenuNotificationName.transactionStatement)
  157. }
  158. func getWalletToWalletNotificationName() -> Notification.Name {
  159. return Notification.Name.init(rawValue: MenuNotificationName.walletToWallet)
  160. }
  161. func getCollectionHeightNotificationName() -> Notification.Name {
  162. return Notification.Name.init(rawValue: MenuNotificationName.collectionHeight)
  163. }
  164. }