2024-07-04 19:48:15 +05:30
|
|
|
//
|
|
|
|
|
// GamesListVC.swift
|
|
|
|
|
// WOKA
|
|
|
|
|
//
|
|
|
|
|
// Created by MacBook Pro on 04/07/24.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import UIKit
|
2024-09-05 20:01:07 +05:30
|
|
|
import GoogleMobileAds
|
2024-07-04 19:48:15 +05:30
|
|
|
|
|
|
|
|
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!
|
2024-09-04 20:17:33 +05:30
|
|
|
@IBOutlet weak var headerTitleHeight: NSLayoutConstraint!
|
|
|
|
|
@IBOutlet weak var headerBtn: LocalisedElementsButton!
|
2024-07-04 19:48:15 +05:30
|
|
|
@IBOutlet weak var headerImage: UIImageView!
|
|
|
|
|
@IBOutlet weak var headerTitleLabel: UILabel!
|
|
|
|
|
@IBOutlet weak var gamesLoadingView: ShimmerEffectView!
|
|
|
|
|
|
2024-09-06 00:19:48 +05:30
|
|
|
@IBOutlet weak var noDataView: UIView!
|
2024-07-15 20:10:33 +05:30
|
|
|
@IBOutlet weak var loadMoreBtn: LocalisedElementsButton!
|
|
|
|
|
@IBOutlet weak var loadMoreActivityIndicator: UIActivityIndicatorView!
|
|
|
|
|
|
2024-07-04 19:48:15 +05:30
|
|
|
var vm = GamesListVM()
|
|
|
|
|
|
2024-09-05 20:01:07 +05:30
|
|
|
private var interstitial: GADInterstitialAd?
|
|
|
|
|
private var rewardedAd: GADRewardedAd?
|
|
|
|
|
|
2024-07-04 19:48:15 +05:30
|
|
|
override func viewDidLoad() {
|
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
vm.vc = self
|
|
|
|
|
vm.initView()
|
|
|
|
|
scrollView.delegate = self
|
2024-09-03 20:20:17 +05:30
|
|
|
self.title = "GAMES".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
|
2024-07-04 19:48:15 +05:30
|
|
|
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
|
|
|
|
|
navigationController?.navigationBar.shadowImage = UIImage()
|
2024-09-05 20:01:07 +05:30
|
|
|
|
|
|
|
|
Task {
|
|
|
|
|
do {
|
|
|
|
|
interstitial = try await GADInterstitialAd.load(
|
2024-09-11 19:39:34 +05:30
|
|
|
withAdUnitID: "ca-app-pub-5699008063638916/8126400123", request: GADRequest())
|
2024-09-05 20:01:07 +05:30
|
|
|
interstitial?.fullScreenContentDelegate = self
|
|
|
|
|
|
|
|
|
|
rewardedAd = try await GADRewardedAd.load(
|
|
|
|
|
withAdUnitID: "ca-app-pub-3940256099942544/1712485313", request: GADRequest())
|
|
|
|
|
rewardedAd?.fullScreenContentDelegate = self
|
|
|
|
|
} catch {
|
|
|
|
|
print("Failed to load interstitial ad with error: \(error.localizedDescription)")
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-04 19:48:15 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
2024-08-12 19:58:58 +05:30
|
|
|
|
|
|
|
|
if self.isMovingFromParent {
|
|
|
|
|
// Back button was pressed
|
2024-08-13 20:02:41 +05:30
|
|
|
PersistentStorage.shared.addOthersCount()
|
2024-08-12 19:58:58 +05:30
|
|
|
}
|
2024-07-04 19:48:15 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func viewDidDisappear(_ animated: Bool) {
|
|
|
|
|
super.viewDidDisappear(animated)
|
|
|
|
|
// Customize the navigation bar's appearance
|
|
|
|
|
self.navigationController?.setColor(color: .black)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-04 20:17:33 +05:30
|
|
|
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.forPage == AdsEnum.games.rawValue}).first, let adID = gamesAd.id{
|
|
|
|
|
PersistentStorage.shared.addAdsCount(adID: adID ,impressions: 1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-04 19:48:15 +05:30
|
|
|
override func viewDidLayoutSubviews() {
|
|
|
|
|
vm.updateTableHeight()
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-15 20:10:33 +05:30
|
|
|
// MARK: - Tap Handler
|
|
|
|
|
|
2024-07-04 19:53:25 +05:30
|
|
|
@IBAction func gameBtnTapped(_ sender: LocalisedElementsButton) {
|
2024-08-12 19:58:58 +05:30
|
|
|
|
2024-09-04 20:17:33 +05:30
|
|
|
/*
|
|
|
|
|
MAke logic for ads
|
|
|
|
|
*/
|
|
|
|
|
if let adsData = AuthFunc.shareInstance.adsData{
|
|
|
|
|
// check if ads data contains ad for webseries
|
|
|
|
|
if let gamesAd = adsData.result?.filter({$0.forPage == AdsEnum.games.rawValue}).first, let adLink = gamesAd.adLink, let adID = gamesAd.id{
|
|
|
|
|
PersistentStorage.shared.addAdsCount(adID: adID,clicks: 1)
|
|
|
|
|
if let url = URL(string: adLink), UIApplication.shared.canOpenURL(url) {
|
|
|
|
|
UIApplication.shared.open(url)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-04 19:53:25 +05:30
|
|
|
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]
|
2024-08-12 19:58:58 +05:30
|
|
|
|
|
|
|
|
if let postID = gameData.id{
|
|
|
|
|
PersistentStorage.shared.addGamesCount(postID: postID)
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-04 19:53:25 +05:30
|
|
|
vcPush.url = gameData.gameURL
|
|
|
|
|
vcPush.orientation = gameData.screenOrientation
|
|
|
|
|
vcPush.modalTransitionStyle = .crossDissolve
|
|
|
|
|
vcPush.modalPresentationStyle = .fullScreen
|
|
|
|
|
self.present(vcPush, animated: true)
|
|
|
|
|
}
|
2024-07-15 20:10:33 +05:30
|
|
|
|
|
|
|
|
@IBAction func loadMoreBtnTapped(_ sender: LocalisedElementsButton) {
|
2024-08-12 19:58:58 +05:30
|
|
|
PersistentStorage.shared.addOthersCount()
|
|
|
|
|
|
2024-07-15 20:10:33 +05:30
|
|
|
loadMoreBtn.isHidden = true
|
|
|
|
|
vm.pageNo += 1
|
|
|
|
|
loadMoreActivityIndicator.startAnimating()
|
|
|
|
|
vm.getGamesListing(isBtnClick: true)
|
|
|
|
|
}
|
2024-09-06 00:19:48 +05:30
|
|
|
|
|
|
|
|
@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()
|
|
|
|
|
}
|
2024-07-04 19:48:15 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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) {
|
2024-09-06 00:19:48 +05:30
|
|
|
vm.indexToLoad = indexPath.row
|
|
|
|
|
|
2024-07-04 19:48:15 +05:30
|
|
|
/*
|
2024-09-06 00:19:48 +05:30
|
|
|
MAke logic for ads
|
2024-07-04 19:48:15 +05:30
|
|
|
*/
|
2024-09-06 00:19:48 +05:30
|
|
|
if let adsData = AuthFunc.shareInstance.adsData, adsData.result?.filter({$0.forPage == AdsEnum.games.rawValue}).first != nil{
|
|
|
|
|
// check if ads data contains ad for webseries
|
|
|
|
|
}else{
|
|
|
|
|
vm.setHeaderData()
|
|
|
|
|
}
|
2024-07-04 19:48:15 +05:30
|
|
|
|
|
|
|
|
let data = vm.gameData[indexPath.row]
|
2024-09-05 20:01:07 +05:30
|
|
|
|
2024-08-12 19:58:58 +05:30
|
|
|
if let postID = data.id{
|
|
|
|
|
PersistentStorage.shared.addGamesCount(postID: postID)
|
|
|
|
|
}
|
2024-09-05 20:01:07 +05:30
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
This is only for test
|
|
|
|
|
*/
|
|
|
|
|
if data.title == "WOKA Ludo"{
|
2024-09-06 00:19:48 +05:30
|
|
|
// guard let interstitial = interstitial else {
|
|
|
|
|
// return print("Ad wasn't ready.")
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// // The UIViewController parameter is an optional.
|
|
|
|
|
// interstitial.present(fromRootViewController: nil)
|
2024-09-05 20:01:07 +05:30
|
|
|
guard let rewardedAd = rewardedAd else {
|
|
|
|
|
return print("Ad wasn't ready.")
|
2024-09-06 00:19:48 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The UIViewController parameter is an optional.
|
2024-09-05 20:01:07 +05:30
|
|
|
rewardedAd.present(fromRootViewController: nil) {
|
|
|
|
|
let reward = rewardedAd.adReward
|
|
|
|
|
print("Reward received with currency \(reward.amount), amount \(reward.amount.doubleValue)")
|
|
|
|
|
// TODO: Reward the user.
|
2024-09-06 00:19:48 +05:30
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if data.title == "WOKA Carrom"{
|
|
|
|
|
guard let interstitial = interstitial else {
|
|
|
|
|
return print("Ad wasn't ready.")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The UIViewController parameter is an optional.
|
|
|
|
|
interstitial.present(fromRootViewController: nil)
|
2024-09-05 20:01:07 +05:30
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-07-04 19:48:15 +05:30
|
|
|
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{
|
|
|
|
|
|
2024-08-02 23:22:06 +05:30
|
|
|
func updateRows(index: Int, type: FavCellCLick, isFav: Bool?, isLike: Bool?, id: Int?) {
|
2024-07-04 19:48:15 +05:30
|
|
|
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
|
2024-09-04 20:17:33 +05:30
|
|
|
// let maxHeaderHeight: CGFloat = 200.0 // Maximum height when fully visible
|
2024-07-04 19:48:15 +05:30
|
|
|
|
|
|
|
|
// 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
|
2024-09-04 20:17:33 +05:30
|
|
|
newHeaderHeight = vm.maxHeaderHeight
|
2024-07-04 19:48:15 +05:30
|
|
|
} else {
|
|
|
|
|
// Calculate the new height for the header view, ensuring it doesn't go below the minimum height
|
2024-09-04 20:17:33 +05:30
|
|
|
newHeaderHeight = max(minHeaderHeight, vm.maxHeaderHeight - y)
|
2024-07-04 19:48:15 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-05 20:01:07 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
extension GamesListVC : GADFullScreenContentDelegate{
|
|
|
|
|
/// Tells the delegate that the ad failed to present full screen content.
|
|
|
|
|
func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
|
|
|
|
|
print("Ad did fail to present full screen content.")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Tells the delegate that the ad will present full screen content.
|
|
|
|
|
func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
|
|
|
|
|
print("Ad will present full screen content.")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Tells the delegate that the ad dismissed full screen content.
|
|
|
|
|
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
|
|
|
|
|
print("Ad did dismiss full screen content.")
|
2024-09-06 00:19:48 +05:30
|
|
|
let data = vm.gameData[vm.indexToLoad]
|
2024-09-05 20:01:07 +05:30
|
|
|
|
|
|
|
|
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
|
2024-09-06 00:19:48 +05:30
|
|
|
vcPush.gameIndex = vm.indexToLoad
|
2024-09-05 20:01:07 +05:30
|
|
|
vcPush.gameData = data
|
|
|
|
|
vcPush.delegate = self
|
|
|
|
|
self.present(vcPush, animated: true)
|
|
|
|
|
|
|
|
|
|
Task {
|
|
|
|
|
do {
|
|
|
|
|
interstitial = try await GADInterstitialAd.load(
|
|
|
|
|
withAdUnitID: "ca-app-pub-3940256099942544/4411468910", request: GADRequest())
|
|
|
|
|
interstitial?.fullScreenContentDelegate = self
|
|
|
|
|
|
|
|
|
|
rewardedAd = try await GADRewardedAd.load(
|
|
|
|
|
withAdUnitID: "ca-app-pub-3940256099942544/1712485313", request: GADRequest())
|
|
|
|
|
rewardedAd?.fullScreenContentDelegate = self
|
|
|
|
|
} catch {
|
|
|
|
|
print("Failed to load interstitial ad with error: \(error.localizedDescription)")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|