// // GamesListVC.swift // WOKA // // Created by MacBook Pro on 04/07/24. // import UIKit class GamesListVC: UIViewController { @IBOutlet weak var scrollView: UIScrollView! @IBOutlet weak var headerHeight: NSLayoutConstraint! @IBOutlet weak var gamesListingTableView: UITableView! @IBOutlet weak var tableHeight: NSLayoutConstraint! @IBOutlet weak var headerView: ShimmerEffectView! @IBOutlet weak var headerImage: UIImageView! @IBOutlet weak var headerTitleLabel: UILabel! @IBOutlet weak var gamesLoadingView: ShimmerEffectView! @IBOutlet weak var loadMoreBtn: LocalisedElementsButton! @IBOutlet weak var loadMoreActivityIndicator: UIActivityIndicatorView! var vm = GamesListVM() override func viewDidLoad() { super.viewDidLoad() vm.vc = self vm.initView() scrollView.delegate = self self.title = "Games".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue) navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default) navigationController?.navigationBar.shadowImage = UIImage() } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) navigationController?.setNavigationBarHidden(false, animated: animated) self.navigationController?.setColor(color: .white) } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) self.navigationController?.setNavigationBarHidden(true, animated: animated) } override func viewDidDisappear(_ animated: Bool) { super.viewDidDisappear(animated) // Customize the navigation bar's appearance self.navigationController?.setColor(color: .black) } override func viewDidLayoutSubviews() { vm.updateTableHeight() } // MARK: - Tap Handler @IBAction func gameBtnTapped(_ sender: LocalisedElementsButton) { let sb = UIStoryboard(name: K.StoryBoard.Games, bundle: nil) let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Games.gamesWebViewVC) as! GamesWebViewVC //https://wokastaging.in/wokagames/Games/html/WokaLudo/index.php let gameData = vm.gameData[vm.indexToLoad] vcPush.url = gameData.gameURL vcPush.orientation = gameData.screenOrientation vcPush.modalTransitionStyle = .crossDissolve vcPush.modalPresentationStyle = .fullScreen self.present(vcPush, animated: true) } @IBAction func loadMoreBtnTapped(_ sender: LocalisedElementsButton) { loadMoreBtn.isHidden = true vm.pageNo += 1 loadMoreActivityIndicator.startAnimating() vm.getGamesListing(isBtnClick: true) } } // MARK: - TableView DataSource , Delegates extension GamesListVC : TableViewSRC{ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return vm.gameData.count == 0 ? 2 : vm.gameData.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: K.CellIdentifier.WebSeries.webSeriesShowListingCell) as! WebSeriesShowListingCell if vm.gameData.count == 0{ cell.showShimmer() }else{ let data = vm.gameData[indexPath.row] cell.setGameData(data: data) cell.stopShimmer() } cell.btnTapped = { [self] (type) -> Void in vm.updateFavLikes(type: type, index: indexPath.row) } return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { /* Updated the top header data */ vm.indexToLoad = indexPath.row vm.setHeaderData() let data = vm.gameData[indexPath.row] let sb = UIStoryboard(name: K.StoryBoard.Games, bundle: nil) let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Games.gamesDetailVC) as! GamesDetailVC vcPush.modalPresentationStyle = .overCurrentContext vcPush.modalTransitionStyle = .crossDissolve vcPush.gameIndex = indexPath.row vcPush.gameData = data vcPush.delegate = self self.present(vcPush, animated: true) } } extension GamesListVC : ReloadSeriesFavLike{ func updateRows(index: Int, type: FavCellCLick, isFav: Bool?, isLike: Bool?, id: Int?) { if let isFav{ switch isFav{ case true: vm.gameData[index].markAsFavourite = true gamesListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none) case false: vm.gameData[index].markAsFavourite = false gamesListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none) } } if let isLike{ switch isLike{ case true: vm.gameData[index].isLiked = true vm.gameData[index].likesCount! += 1 gamesListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none) case false: vm.gameData[index].isLiked = false vm.gameData[index].likesCount! -= 1 gamesListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none) } } } } // MARK: - Animating scrollView extension GamesListVC: UIScrollViewDelegate { func scrollViewDidScroll(_ scrollView: UIScrollView) { // Get the current vertical offset of the scroll view let y = scrollView.contentOffset.y // Define the height range for the header view let minHeaderHeight: CGFloat = 0.0 // Height at which the header becomes invisible let maxHeaderHeight: CGFloat = 200.0 // Maximum height when fully visible // Calculate the new height for the header view based on the scroll position let newHeaderHeight: CGFloat if y < 0 { // When scrolling up beyond the top, ensure the header view is fully expanded newHeaderHeight = maxHeaderHeight } else { // Calculate the new height for the header view, ensuring it doesn't go below the minimum height newHeaderHeight = max(minHeaderHeight, maxHeaderHeight - y) } // Update the header view's height constraint with the new height headerHeight.constant = newHeaderHeight // Animate the layout changes to smoothly transition the header height UIView.animate(withDuration: 0.3) { self.view.layoutIfNeeded() } } }