// // HomeCollectionTableViewCell.swift // GMERemittance // // Created by gme_2 on 27/09/2018. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved. // import UIKit import Localize_Swift struct MenuNotificationName { static let sendMoney = "SendMoeny" static let mobileRecharge = "mobileRecharge" static let todaysRate = "todaysRate" static let resend = "resend" static let transactionStatement = "transactionStatement" static let walletToWallet = "walletToWallet" static let collectionHeight = "height_height" } class HomeCollectionTableViewCell: UITableViewCell, UICollectionViewDelegateFlowLayout { struct StringConstants { let sendMoneyText = "send_money_text".localized() let todaysRateText = "todays_rate_title_text".localized() let transactionReportText = "transaction_report_title_text".localized() let walletStatement = "gme_wallet_statement_text".localized() } enum CollectionMenus: Int { case sendMoney = 0 case mobileRecharge case todaysRate case walletStatement case resend case walletToWallet } @IBOutlet weak var collectionView: UICollectionView! private var menus: [HomeCollectionModel]? var parentViewController: HomeViewController? override func awakeFromNib() { super.awakeFromNib() // Initialization code self.collectionView.delegate = self self.collectionView.dataSource = self } func setup() { collectionView.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.old, context: nil) NotificationCenter.default.addObserver(self, selector: #selector(configureLanguage), name: NSNotification.Name(LCLLanguageChangeNotification), object: nil) self.menus = self.configureMenu() } @objc func configureLanguage() { self.collectionView.reloadData() } private func configureMenu() -> [HomeCollectionModel] { let sendMoneyMenu = HomeCollectionModel() sendMoneyMenu.index = 0 let text = "send_money_text".localized() sendMoneyMenu.title = text sendMoneyMenu.icon = #imageLiteral(resourceName: "send-money") let mobileRechargeMenu = HomeCollectionModel() mobileRechargeMenu.index = 1 mobileRechargeMenu.title = "Mobile Recharge" mobileRechargeMenu.icon = #imageLiteral(resourceName: "mobile-recharge") let toadaysRateMenu = HomeCollectionModel() toadaysRateMenu.index = 2 toadaysRateMenu.title = "todays_rate_text".localized() toadaysRateMenu.icon = #imageLiteral(resourceName: "rate-today") let trackTransferMenu = HomeCollectionModel() trackTransferMenu.index = 3 trackTransferMenu.title = "gme_wallet_statement_text".localized() let walletImage = UIImage.init(named: "ic_menu_wallet") trackTransferMenu.icon = walletImage let transactionStatementMenu = HomeCollectionModel() transactionStatementMenu.index = 4 transactionStatementMenu.title = "resend_money_text".localized() let image = UIImage.init(named: "ic_menu_sendmoney") transactionStatementMenu.icon = image let walletToWalletMenu = HomeCollectionModel() walletToWalletMenu.index = 5 walletToWalletMenu.title = "Wallet to Wallet Transfer" walletToWalletMenu.icon = #imageLiteral(resourceName: "wallet-transfer") let menus = [sendMoneyMenu, toadaysRateMenu, transactionStatementMenu, trackTransferMenu] return menus } override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { if let observedObject = object as? UICollectionView, observedObject == collectionView { let height = self.collectionView.contentSize.height let finalHeight = height + CGFloat(20) NotificationCenter.default.post(name: self.getCollectionHeightNotificationName(), object: nil, userInfo: [MenuNotificationName.collectionHeight : finalHeight]) } } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let device = Utility.getDeviceModel() if device == .iphone5 { let itemsPerRow: CGFloat = 2 let paddingSpace: CGFloat = CGFloat(20 * (itemsPerRow + 1)) let availableWidth = frame.width - paddingSpace let widthPerItem = availableWidth / itemsPerRow return CGSize(width: widthPerItem, height: widthPerItem * 1.2) }else { let itemsPerRow: CGFloat = 2 let paddingSpace: CGFloat = CGFloat(25 * (itemsPerRow + 1)) let availableWidth = frame.width - paddingSpace let widthPerItem = availableWidth / itemsPerRow return CGSize(width: widthPerItem, height: widthPerItem * 1.1) } } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { if Utility.getDeviceModel() == .iphone5 { return UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10) }else { return UIEdgeInsets(top: 20, left: 15, bottom: 10, right: 15) } } } extension HomeCollectionTableViewCell: UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return menus?.count ?? 0 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "HomeCollectionCollectionViewCell", for: indexPath) as! HomeCollectionCollectionViewCell let menus = self.configureMenu() cell.model = menus.elementAt(index: indexPath.row) cell.setup() return cell } } extension HomeCollectionTableViewCell: UICollectionViewDelegate { func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { guard let index = self.menus?.elementAt(index: indexPath.row)?.index else {return} guard let _index = CollectionMenus.init(rawValue: index) else {return } let center = NotificationCenter.default switch _index { case .sendMoney: center.post(name: self.getSendMoneyNotificationName(), object: nil) case .mobileRecharge: center.post(name: self.getMobileRechargeNotificationName(), object: nil) case .todaysRate: center.post(name: self.getTodaysRateNotificationName(), object: nil) case .walletStatement: center.post(name: self.getTrackYourTransferNotificationName(), object: nil) case .resend: center.post(name: self.getResendNotificationName(), object: nil) case .walletToWallet: center.post(name: self.getWalletToWalletNotificationName(), object: nil) } } func getSendMoneyNotificationName() -> Notification.Name { return Notification.Name.init(rawValue: MenuNotificationName.sendMoney) } func getMobileRechargeNotificationName() -> Notification.Name { return Notification.Name.init(rawValue: MenuNotificationName.mobileRecharge) } func getTodaysRateNotificationName() -> Notification.Name { return Notification.Name.init(rawValue: MenuNotificationName.todaysRate) } func getTrackYourTransferNotificationName() -> Notification.Name { return Notification.Name.init(rawValue: MenuNotificationName.transactionStatement) } func getResendNotificationName() -> Notification.Name { return Notification.Name.init(rawValue: MenuNotificationName.resend) } func getWalletToWalletNotificationName() -> Notification.Name { return Notification.Name.init(rawValue: MenuNotificationName.walletToWallet) } func getCollectionHeightNotificationName() -> Notification.Name { return Notification.Name.init(rawValue: MenuNotificationName.collectionHeight) } }