// // GamesListVC.swift // WOKA // // Created by MacBook Pro on 04/07/24. // import UIKit import GoogleMobileAds 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 headerTitleHeight: NSLayoutConstraint! @IBOutlet weak var headerBtn: LocalisedElementsButton! @IBOutlet weak var headerImage: UIImageView! @IBOutlet weak var headerTitleLabel: UILabel! @IBOutlet weak var gamesLoadingView: ShimmerEffectView! @IBOutlet weak var noDataView: UIView! // @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) if self.isMovingFromParent { // Back button was pressed PersistentStorage.shared.addOthersCount() } } override func viewDidDisappear(_ animated: Bool) { super.viewDidDisappear(animated) // Customize the navigation bar's appearance self.navigationController?.setColor(color: .black) } override func viewDidAppear(_ animated: Bool) { /* Check if ads is there to map impressions */ if let adsData = AuthFunc.shareInstance.adsData{ // check if ads data contains ad for webseries if let gamesAd = adsData.result?.filter({$0.slug == AdsEnum.games.rawValue}).first, let adID = gamesAd.advertisement?.id{ PersistentStorage.shared.addAdsCount(adID: adID ,impressions: 1) } } } override func viewDidLayoutSubviews() { vm.updateTableHeight() } // MARK: - Tap Handler @IBAction func gameBtnTapped(_ sender: LocalisedElementsButton) { /* MAke logic for ads */ if let adsData = AuthFunc.shareInstance.adsData, let gamesAd = adsData.result?.filter({$0.slug == AdsEnum.games.rawValue}).first{ // URL maybe null so handle it if gamesAd.advertisement?.isActive == "1" && gamesAd.advertisement?.adLink == nil{ // it means local ad is active and url is null return } // check if ads data contains ad for webseries if let adLink = gamesAd.advertisement?.adLink, let adID = gamesAd.advertisement?.id{ PersistentStorage.shared.addAdsCount(adID: adID,clicks: 1) if let url = URL(string: adLink), UIApplication.shared.canOpenURL(url) { UIApplication.shared.open(url) } return } } let sb = UIStoryboard(name: K.StoryBoard.Games, bundle: nil) let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Games.gamesWebViewVC) as! GamesWebViewVC let gameData = vm.gameData[vm.indexToLoad] if let postID = gameData.id{ PersistentStorage.shared.addGamesCount(postID: postID) } vcPush.url = gameData.gameURL vcPush.orientation = gameData.screenOrientation vcPush.modalTransitionStyle = .crossDissolve vcPush.modalPresentationStyle = .fullScreen self.present(vcPush, animated: true) } // @IBAction func loadMoreBtnTapped(_ sender: LocalisedElementsButton) { // PersistentStorage.shared.addOthersCount() // if CheckReachability.reachability?.isReachable == false{ // self.toast(msg: K.ConstantString.noInternet , time: 2) // return // } // loadMoreBtn.isHidden = true // vm.pageNo += 1 // loadMoreActivityIndicator.startAnimating() // vm.getGamesListing(isBtnClick: true) // } @IBAction func retryBtnTapped(_ sender: LocalisedElementsButton) { /* make page 0 as its lazy loading, hide the no data stack as retry is tapped, start the shimmer and reload tableview so that both header and table shimmer matches, get the karaoke listing */ vm.pageNo = 0 noDataView.isHidden = true vm.startShimmer() vm.getGamesListing() } } // 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 HapticFeedbackGenerator.shared.simpleSelection() vm.updateFavLikes(type: type, index: indexPath.row) } return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { vm.indexToLoad = indexPath.row /* MAke logic for ads */ if let adsData = AuthFunc.shareInstance.adsData, let gamesAd = adsData.result?.filter({$0.slug == AdsEnum.games.rawValue}).first, (gamesAd.advertisement != nil || gamesAd.googleAd != nil){ // check if ads data contains ad for webseries }else{ vm.setHeaderData() } let data = vm.gameData[indexPath.row] if let postID = data.id{ PersistentStorage.shared.addGamesCount(postID: postID) } 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: - Google Ad Banner Delegate extension GamesListVC : GADBannerViewDelegate{ func bannerViewDidReceiveAd(_ bannerView: GADBannerView) { switch bannerView{ case vm.headerBannerView: headerView.stopShimmer() case vm.headerBannerBottomView: headerBtn.alpha = 0 headerBtn.isEnabled = false gamesLoadingView.stopShimmer() default: break } print("bannerViewDidReceiveAd") bannerView.backgroundColor = #colorLiteral(red: 0.01960784314, green: 0, blue: 0.2196078431, alpha: 1) } func bannerView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: Error) { switch bannerView{ case vm.headerBannerView: headerView.stopShimmer() vm.setHeaderData() case vm.headerBannerBottomView: headerTitleLabel.isHidden = true headerBtn.isHidden = true gamesLoadingView.stopShimmer() default: break } print("bannerView:didFailToReceiveAdWithError: \(error.localizedDescription)") } func bannerViewDidRecordImpression(_ bannerView: GADBannerView) { print("bannerViewDidRecordImpression") } func bannerViewWillPresentScreen(_ bannerView: GADBannerView) { print("bannerViewWillPresentScreen") } func bannerViewWillDismissScreen(_ bannerView: GADBannerView) { print("bannerViewWillDIsmissScreen") } func bannerViewDidDismissScreen(_ bannerView: GADBannerView) { print("bannerViewDidDismissScreen") } } // 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 = vm.maxHeaderHeight } else { // Calculate the new height for the header view, ensuring it doesn't go below the minimum height newHeaderHeight = max(minHeaderHeight, vm.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() } // dont do network call if the showdata count is same as the total count provided by api // also check if already the api call is done if vm.gameData.count == vm.totalDataCount || vm.isGameDataLoading == true {return} let contentHeight = scrollView.contentSize.height let height = scrollView.frame.size.height // Check if the user scrolled to the bottom if y > contentHeight - height - 50{ // check if internet is available if CheckReachability.reachability?.isReachable == false{ return } //start the acitvity indicator loadMoreActivityIndicator.startAnimating() vm.isGameDataLoading = true vm.getGamesListing(isBtnClick: true) } } }