From 241f8c9395469e5287874f4f3a3c0283f1f53010 Mon Sep 17 00:00:00 2001 From: gme_2 Date: Thu, 27 Sep 2018 17:09:37 +0900 Subject: [PATCH] height adjusted according to the menu height --- .../Cell/HomeCollectionTableViewCell.swift | 44 +++++++++++-------- .../Home/User Interface/View/Home.storyboard | 6 +-- .../View/HomeViewController.swift | 32 ++++++++++++-- 3 files changed, 57 insertions(+), 25 deletions(-) diff --git a/GMERemittance/Module/Home/User Interface/View/Cell/HomeCollectionTableViewCell.swift b/GMERemittance/Module/Home/User Interface/View/Cell/HomeCollectionTableViewCell.swift index 7dcafa4d..fa0ce3db 100644 --- a/GMERemittance/Module/Home/User Interface/View/Cell/HomeCollectionTableViewCell.swift +++ b/GMERemittance/Module/Home/User Interface/View/Cell/HomeCollectionTableViewCell.swift @@ -15,6 +15,7 @@ struct MenuNotificationName { static let trackYourTransfer = "trackYourTransfer" static let transactionStatement = "transactionStatement" static let walletToWallet = "walletToWallet" + static let collectionHeight = "height_height" } class HomeCollectionTableViewCell: UITableViewCell, UICollectionViewDelegateFlowLayout { @@ -38,6 +39,7 @@ class HomeCollectionTableViewCell: UITableViewCell, UICollectionViewDelegateFlow func setup() { + collectionView.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.old, context: nil) self.configureMenu() self.collectionView.delegate = self self.collectionView.dataSource = self @@ -77,18 +79,37 @@ class HomeCollectionTableViewCell: UITableViewCell, UICollectionViewDelegateFlow self.menus = [sendMoneyMenu, mobileRechargeMenu, toadaysRateMenu, trackTransferMenu, transactionStatementMenu, walletToWalletMenu] } + 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 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) + return CGSize(width: widthPerItem, height: widthPerItem * 1.2) } - - } +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 + cell.model = self.menus?.elementAt(index: indexPath.row) + cell.setup() + return cell + } +} extension HomeCollectionTableViewCell: UICollectionViewDelegate { func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { @@ -133,22 +154,9 @@ extension HomeCollectionTableViewCell: UICollectionViewDelegate { func getWalletToWalletNotificationName() -> Notification.Name { return Notification.Name.init(rawValue: MenuNotificationName.walletToWallet) } -} - -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 - cell.model = self.menus?.elementAt(index: indexPath.row) - cell.setup() - return cell + func getCollectionHeightNotificationName() -> Notification.Name { + return Notification.Name.init(rawValue: MenuNotificationName.collectionHeight) } } - - diff --git a/GMERemittance/Module/Home/User Interface/View/Home.storyboard b/GMERemittance/Module/Home/User Interface/View/Home.storyboard index 407ae946..cae5eb54 100644 --- a/GMERemittance/Module/Home/User Interface/View/Home.storyboard +++ b/GMERemittance/Module/Home/User Interface/View/Home.storyboard @@ -35,11 +35,11 @@ - + - + @@ -152,7 +152,7 @@ - + diff --git a/GMERemittance/Module/Home/User Interface/View/HomeViewController.swift b/GMERemittance/Module/Home/User Interface/View/HomeViewController.swift index fe169367..111936b2 100644 --- a/GMERemittance/Module/Home/User Interface/View/HomeViewController.swift +++ b/GMERemittance/Module/Home/User Interface/View/HomeViewController.swift @@ -25,7 +25,11 @@ class HomeViewController: UIViewController, UICollectionViewDelegateFlowLayout { var presenter: HomeModuleInterface? var sections: [Sections] = [.balance, .collection] - + var menuHeight: CGFloat? { + didSet { + self.tableView.reloadData() + } + } // MARK: VC's Life cycle override func viewDidLoad() { @@ -50,10 +54,11 @@ class HomeViewController: UIViewController, UICollectionViewDelegateFlowLayout { setupDelegates() configureViews() setupNotifications() - self.setupPicturedNavBar() } + + private func setupNotifications() { let center = NotificationCenter.default center.addObserver(self, selector: #selector(self.showSendMoney), name: self.getSendMoneyNotificationName(), object: nil) @@ -62,6 +67,7 @@ class HomeViewController: UIViewController, UICollectionViewDelegateFlowLayout { center.addObserver(self, selector: #selector(self.showTrackYourTransfer), name: self.getTrackYourTransferNotificationName(), object: nil) center.addObserver(self, selector: #selector(self.showTransactionStatement), name: self.getTransactionStatementNotificationName(), object: nil) center.addObserver(self, selector: #selector(self.showWalletToWallet), name: self.getWalletToWalletNotificationName(), object: nil) + center.addObserver(self, selector: #selector(self.setupHeight(sender:)), name: self.getCollectionHeightNotificationName(), object: nil) } private func setupDelegates() { @@ -82,10 +88,18 @@ class HomeViewController: UIViewController, UICollectionViewDelegateFlowLayout { self.verificationNoticeView.isHidden = true self.verificationNoticeView.alpha = 0 showUnverifiedNotice() + setNavBar() } + func setNavBar() { + let selector = #selector(self.showSideMenu) + self.setupPicturedNavBar(sideMenuAction: selector) + } - + @objc func showSideMenu() { + sideMenuController?.showLeftViewAnimated() + } + private func isUserVerified() -> Bool { return false let val = (UserDefaults.standard.object(forKey: UserKeys.verified) as? Bool ) ?? false @@ -132,6 +146,12 @@ class HomeViewController: UIViewController, UICollectionViewDelegateFlowLayout { @objc private func showWalletToWallet() { self.alert(message: "This feature is coming soon") } + + @objc private func setupHeight(sender: Notification) { + if let height = sender.userInfo?[MenuNotificationName.collectionHeight] as? CGFloat { + self.menuHeight = height + } + } } // MARK: HomeViewInterface @@ -152,7 +172,7 @@ extension HomeViewController: UITableViewDelegate { let totalHeight = view.frame.height let ramainingBalanceCellHeight: CGFloat = 100 let finalHeight = totalHeight - ramainingBalanceCellHeight - return finalHeight + return self.menuHeight ?? finalHeight } } } @@ -215,4 +235,8 @@ extension HomeViewController { func getWalletToWalletNotificationName() -> Notification.Name { return Notification.Name.init(rawValue: MenuNotificationName.walletToWallet) } + + func getCollectionHeightNotificationName() -> Notification.Name { + return Notification.Name.init(rawValue: MenuNotificationName.collectionHeight) + } }