419 lines
19 KiB
Swift
419 lines
19 KiB
Swift
//
|
|
// MyListVM.swift
|
|
// WOKA
|
|
//
|
|
// Created by MacBook Pro on 13/06/24.
|
|
//
|
|
|
|
import Foundation
|
|
import Alamofire
|
|
import GoogleMobileAds
|
|
|
|
enum CollectionSelectionMyList{
|
|
case webSeriesCV
|
|
case webSeriesHindiCV
|
|
case audioBooksCV
|
|
case karaokeCV
|
|
case gamesCV
|
|
}
|
|
|
|
class MyListVM{
|
|
weak var vc : MyListVC!
|
|
|
|
let refreshControl = UIRefreshControl()
|
|
let feedbackGenerator = UIImpactFeedbackGenerator(style: .light)
|
|
|
|
var selectedCollection : CollectionSelectionMyList?
|
|
var bottomBanner = GADBannerView()
|
|
|
|
func initView(){
|
|
NotificationCenter.default.addObserver(self, selector: #selector(languageChanged), name: .languageDidChange, object: nil)
|
|
NotificationCenter.default.addObserver(self, selector: #selector(self.viewPush(notification:)), name: NSNotification.Name(rawValue: K.NotificationCenterReloads.myListPush), object: nil)
|
|
|
|
if AuthFunc.shareInstance.getUserType() == 3{ // handle guest
|
|
vc.guestLoginView.isHidden = false
|
|
vc.guestLoginStack.isHidden = false
|
|
vc.scrollView.isHidden = true
|
|
startShimmer(load: false)
|
|
return
|
|
}
|
|
|
|
/*
|
|
First check if webSeries ad is present via slug, then check for Local Ads, if not then check google ads.
|
|
*/
|
|
if let adsData = AuthFunc.shareInstance.adsData, let myListAd = adsData.result?.filter({$0.slug == AdsEnum.myList.rawValue}).first{
|
|
|
|
// check if ads data contains LocalAD for webseries
|
|
if let advertisement = myListAd.advertisement,let bannerImage = advertisement.bannerImage{
|
|
|
|
let imageView = UIImageView()
|
|
|
|
imageView.imageURL(bannerImage, color: .textDarkBlue,type: .ads) { [weak self] isDone in
|
|
guard let self else{return}
|
|
// if image gives error hide the ad banner
|
|
if isDone == false{
|
|
vc.adView.isHidden = true
|
|
}else{
|
|
let height = UIScreen.main.bounds.width * 0.192
|
|
vc.adHeight.constant = height
|
|
// Set the image you want to display
|
|
imageView.imageURL(bannerImage, color: .white)
|
|
|
|
// Enable auto-layout
|
|
imageView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
// Add the UIImageView to the view
|
|
vc.adView.addSubview(imageView)
|
|
|
|
// Set UIImageView to match the size of the parent UIView
|
|
NSLayoutConstraint.activate([
|
|
imageView.leadingAnchor.constraint(equalTo: vc.adView.leadingAnchor),
|
|
imageView.trailingAnchor.constraint(equalTo: vc.adView.trailingAnchor),
|
|
imageView.topAnchor.constraint(equalTo: vc.adView.topAnchor),
|
|
imageView.bottomAnchor.constraint(equalTo: vc.adView.bottomAnchor)
|
|
])
|
|
imageView.contentMode = .scaleAspectFit
|
|
UIView.animate(withDuration: 0.2, animations: { [weak self] in
|
|
guard let self else{return}
|
|
vc.adView.alpha = 1
|
|
})
|
|
|
|
vc.adView.addTapGesture {
|
|
if let adID = myListAd.id{
|
|
PersistentStorage.shared.addAdsCount(adID: adID ,clicks: 1)
|
|
}
|
|
if let adLink = myListAd.advertisement?.adLink ,let url = URL(string: adLink), UIApplication.shared.canOpenURL(url) {
|
|
UIApplication.shared.open(url)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
}else if myListAd.googleAd != nil{
|
|
//setup google banner ads.
|
|
vc.adHeight.constant = 90
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3, execute: {
|
|
AdReusable.sharedInstance.setupBannerAd(bannerView: self.bottomBanner, in: self.vc.adView, adUnitID: K.GoogleAdIDs.myListNotifications, viewController: self.vc, height: 6,width: 0)
|
|
})
|
|
}
|
|
}
|
|
|
|
setupCell()
|
|
startShimmer(load: true)
|
|
getFavouriteListing()
|
|
|
|
|
|
vc.karaokeViewBtn.roundCorner()
|
|
vc.audioBooksBtn.roundCorner()
|
|
vc.gamesBtn.roundCorner()
|
|
vc.webSeriesBtn.roundCorner()
|
|
vc.webSeriesHindiBtn.roundCorner()
|
|
}
|
|
|
|
func startShimmer(load : Bool){
|
|
DispatchQueue.main.async { [weak self] in
|
|
guard let self else{return}
|
|
self.vc.shimmerView.forEach { shimmer in
|
|
if load{
|
|
shimmer.gradientColorOne = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
|
|
shimmer.gradientColorTwo = #colorLiteral(red: 0.6, green: 0.8862745098, blue: 1, alpha: 1)
|
|
shimmer.startShimmer()
|
|
}else{
|
|
shimmer.stopShimmer()
|
|
|
|
}
|
|
}
|
|
}
|
|
vc.shimmerStack.isHidden = !load
|
|
}
|
|
|
|
@objc func viewPush(notification: Notification){
|
|
if let userInfo = notification.userInfo, let action = userInfo["action"] as? TopViewPush {
|
|
switch action {
|
|
case .webseries:
|
|
let sb = UIStoryboard(name: K.StoryBoard.webSeries, bundle: nil)
|
|
let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.WebSeries.webSeriesVC) as! WebSeriesVC
|
|
vc.navigationController?.pushViewController(vcPush, animated: true)
|
|
case .audioBooks:
|
|
let sb = UIStoryboard(name: K.StoryBoard.audioBooks, bundle: nil)
|
|
let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.AudioBooks.audioBookHomeVC) as! AudioBookHomeVC
|
|
vc.navigationController?.pushViewController(vcPush, animated: true)
|
|
case .games:
|
|
let sb = UIStoryboard(name: K.StoryBoard.Games, bundle: nil)
|
|
let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Games.gamesListVC) as! GamesListVC
|
|
vc.navigationController?.pushViewController(vcPush, animated: true)
|
|
case .karaoke:
|
|
let sb = UIStoryboard(name: K.StoryBoard.Karaoke, bundle: nil)
|
|
let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Karaoke.karaokeListingVC) as! KaraokeListingVC
|
|
vc.navigationController?.pushViewController(vcPush, animated: true)
|
|
case .shop:
|
|
let sb = UIStoryboard(name: K.StoryBoard.shop, bundle: nil)
|
|
let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Shop.shopListingVC) as! ShopListingVC
|
|
vc.navigationController?.pushViewController(vcPush, animated: true)
|
|
case .liveTV:
|
|
break
|
|
case .blogs:
|
|
let sb = UIStoryboard(name: K.StoryBoard.theme, bundle: nil)
|
|
let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Theme.blogsVC) as! BlogsVC
|
|
vc.navigationController?.pushViewController(vcPush, animated: true)
|
|
// playLiveTV()
|
|
case .radio:
|
|
let sb = UIStoryboard(name: K.StoryBoard.wokaFM, bundle: nil)
|
|
let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.WokaFM.wokaFMVC) as! WokaFMVC
|
|
vcPush.modalPresentationStyle = .overCurrentContext
|
|
vcPush.modalTransitionStyle = .crossDissolve
|
|
vc.present(vcPush, animated: true)
|
|
}
|
|
}
|
|
}
|
|
|
|
func addGradient(){
|
|
let color1 = #colorLiteral(red: 0.6745098039, green: 0.6235294118, blue: 0.1725490196, alpha: 1)
|
|
let color2 = #colorLiteral(red: 0.5450980392, green: 0.6745098039, blue: 0.1725490196, alpha: 1)
|
|
|
|
vc.gradientView.applyGradient(colors: [color2, color1], startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint(x: 0.8, y: 0))
|
|
}
|
|
|
|
@objc func languageChanged(){
|
|
self.reloadCollections()
|
|
}
|
|
|
|
func setupCell(){
|
|
vc.webSeriesCV.register(UINib(nibName: K.CellIdentifier.Home.favouriteCell, bundle: nil), forCellWithReuseIdentifier: K.CellIdentifier.Home.favouriteCell)
|
|
vc.webSeriesCV.delegate = vc.self
|
|
vc.webSeriesCV.dataSource = vc.self
|
|
|
|
vc.webSeriesHindiCV.register(UINib(nibName: K.CellIdentifier.Home.favouriteCell, bundle: nil), forCellWithReuseIdentifier: K.CellIdentifier.Home.favouriteCell)
|
|
vc.webSeriesHindiCV.delegate = vc.self
|
|
vc.webSeriesHindiCV.dataSource = vc.self
|
|
|
|
vc.audioBooksCV.register(UINib(nibName: K.CellIdentifier.Home.favouriteCell, bundle: nil), forCellWithReuseIdentifier: K.CellIdentifier.Home.favouriteCell)
|
|
vc.audioBooksCV.delegate = vc.self
|
|
vc.audioBooksCV.dataSource = vc.self
|
|
|
|
vc.karaokeCV.register(UINib(nibName: K.CellIdentifier.Home.favouriteCell, bundle: nil), forCellWithReuseIdentifier: K.CellIdentifier.Home.favouriteCell)
|
|
vc.karaokeCV.delegate = vc.self
|
|
vc.karaokeCV.dataSource = vc.self
|
|
|
|
vc.gamesCV.register(UINib(nibName: K.CellIdentifier.Home.favouriteCell, bundle: nil), forCellWithReuseIdentifier: K.CellIdentifier.Home.favouriteCell)
|
|
vc.gamesCV.delegate = vc.self
|
|
vc.gamesCV.dataSource = vc.self
|
|
}
|
|
|
|
// MARK: - Get Favourite Listing
|
|
|
|
func getFavouriteListing(){
|
|
|
|
let headers : HTTPHeaders = ["Accept-Language" : AuthFunc.shareInstance.languageSelected == .english ? "English" : "Hindi",
|
|
"access-token": AuthFunc.shareInstance.getAccessToken()]
|
|
let params : Parameters = ["api_version" : "v2"]
|
|
NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Home.favourite_listing, method: .post,parameters : params, headers: headers) { [weak self](result : Result<BaseResponseModel<FavouriteListingDM>, NetworkManager.APIError>) in
|
|
switch result{
|
|
case .success(let data):
|
|
guard let self else{return}
|
|
switch data.success{
|
|
case 0:
|
|
self.refreshControl.endRefreshing()
|
|
Utilities.dismissProgressHUD()
|
|
self.startShimmer(load: false)
|
|
self.vc.toast(msg: data.message ?? "Unrecognised error" , time: 2)
|
|
// MyListDataTemp.shareInstance.webSeriesHindi.removeAll()
|
|
MyListDataTemp.shareInstance.favListingData = nil
|
|
// make this true if mylist is loaded once
|
|
MyListDataTemp.shareInstance.isDatafetched = true
|
|
|
|
if MyListDataTemp.shareInstance.favListingData?.showData == nil {
|
|
MyListDataTemp.shareInstance.favListingData = FavouriteListingDM.ResultData(totalRecords: nil, showData: FavouriteListingDM.ResultData.ShowData(hindi: [],english: []),videoData: [],gameData: [],singKaraokeData: [],audioData: [])
|
|
}
|
|
reloadCollections()
|
|
checkNil()
|
|
case 1:
|
|
self.refreshControl.endRefreshing()
|
|
Utilities.dismissProgressHUD()
|
|
self.startShimmer(load: false)
|
|
guard let data = data.data?.result else{return}
|
|
|
|
MyListDataTemp.shareInstance.favListingData = nil
|
|
MyListDataTemp.shareInstance.favListingData = data
|
|
MyListDataTemp.shareInstance.isDatafetched = true
|
|
|
|
reloadCollections()
|
|
feedbackGenerator.impactOccurred()
|
|
checkNil()
|
|
checkViewAll()
|
|
default:
|
|
break
|
|
}
|
|
case .failure(let error):
|
|
guard let self else{return}
|
|
self.refreshControl.endRefreshing()
|
|
Utilities.dismissProgressHUD()
|
|
self.startShimmer(load: false)
|
|
checkNil()
|
|
if MyListDataTemp.shareInstance.favListingData?.showData == nil {
|
|
MyListDataTemp.shareInstance.favListingData = FavouriteListingDM.ResultData(totalRecords: nil, showData: FavouriteListingDM.ResultData.ShowData(hindi: [],english: []),videoData: [],gameData: [],singKaraokeData: [],audioData: [])
|
|
}
|
|
self.vc.toast(msg: error.localizedDescription , time: 2)
|
|
}
|
|
}
|
|
}
|
|
|
|
func checkViewAll(){
|
|
if let favListing = MyListDataTemp.shareInstance.favListingData{
|
|
|
|
if let karaokeCount = favListing.singKaraokeData?.count, karaokeCount >= 3 {
|
|
vc.karaokeViewBtn.isHidden = false
|
|
}else{
|
|
vc.karaokeViewBtn.isHidden = true
|
|
}
|
|
|
|
if let audioBooksCount = favListing.audioData?.count , audioBooksCount >= 3{
|
|
vc.audioBooksBtn.isHidden = false
|
|
}else{
|
|
vc.audioBooksBtn.isHidden = true
|
|
}
|
|
|
|
if let gamesCount = favListing.gameData?.count , gamesCount >= 3{
|
|
vc.gamesBtn.isHidden = false
|
|
}else{
|
|
vc.gamesBtn.isHidden = true
|
|
}
|
|
|
|
if let webbSeriesEnglishCount = favListing.showData?.english?.count , webbSeriesEnglishCount >= 3{
|
|
vc.webSeriesBtn.isHidden = false
|
|
}else{
|
|
vc.webSeriesBtn.isHidden = true
|
|
}
|
|
|
|
if let hindiSeriesCount = favListing.showData?.hindi?.count , hindiSeriesCount >= 3{
|
|
vc.webSeriesHindiBtn.isHidden = false
|
|
}else{
|
|
vc.webSeriesHindiBtn.isHidden = true
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Like , unlike
|
|
|
|
func likePost(postID : Int, postType : Int, index : Int , onCompletion : @escaping (Bool) -> Void){
|
|
Utilities.startProgressHUD()
|
|
let headers : HTTPHeaders = ["Accept-Language" : AuthFunc.shareInstance.languageSelected == .english ? "English" : "Hindi",
|
|
"access-token": AuthFunc.shareInstance.getAccessToken()]
|
|
|
|
let params : Parameters = ["post_id" : postID,
|
|
"post_type" : postType]
|
|
|
|
NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Home.post_like, method: .post, parameters: params, headers: headers) { [weak self](result : Result<CommonResponseModel, NetworkManager.APIError>) in
|
|
switch result{
|
|
case .success(let data):
|
|
guard let self else{return}
|
|
switch data.success{
|
|
case 0:
|
|
Utilities.dismissProgressHUD()
|
|
self.vc.toast(msg: data.message ?? "Unrecognised error" , time: 2)
|
|
onCompletion(false)
|
|
case 1:
|
|
Utilities.dismissProgressHUD()
|
|
onCompletion(true)
|
|
default:
|
|
Utilities.dismissProgressHUD()
|
|
onCompletion(false)
|
|
}
|
|
case .failure(let error):
|
|
guard let self else{return}
|
|
Utilities.dismissProgressHUD()
|
|
self.vc.toast(msg: error.localizedDescription , time: 2)
|
|
onCompletion(false)
|
|
}
|
|
}
|
|
}
|
|
|
|
func unlikePost(postID : Int, postType : Int , index : Int, onCompletion : @escaping (Bool) -> Void){
|
|
Utilities.startProgressHUD()
|
|
let headers : HTTPHeaders = ["Accept-Language" : AuthFunc.shareInstance.languageSelected == .english ? "English" : "Hindi",
|
|
"access-token": AuthFunc.shareInstance.getAccessToken()]
|
|
|
|
let params : Parameters = ["post_id" : postID,
|
|
"post_type" : postType]
|
|
NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Home.post_unlike, method: .post, parameters: params, headers: headers) { [weak self](result : Result<BaseResponseModel<CommonResponseModel>, NetworkManager.APIError>) in
|
|
switch result{
|
|
case .success(let data):
|
|
guard let self else{return}
|
|
switch data.success{
|
|
case 0:
|
|
Utilities.dismissProgressHUD()
|
|
self.vc.toast(msg: data.message ?? "Unrecognised error" , time: 2)
|
|
onCompletion(false)
|
|
case 1:
|
|
Utilities.dismissProgressHUD()
|
|
onCompletion(true)
|
|
default:
|
|
Utilities.dismissProgressHUD()
|
|
onCompletion(false)
|
|
}
|
|
case .failure(let error):
|
|
guard let self else{return}
|
|
Utilities.dismissProgressHUD()
|
|
self.vc.toast(msg: error.localizedDescription , time: 2)
|
|
onCompletion(false)
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// MARK: - Remove Favourite
|
|
|
|
func removeFavourite(postID : Int, postType : Int, categoryID : String, index : Int , onCompletion : @escaping (Bool) -> Void){
|
|
Utilities.startProgressHUD()
|
|
let headers : HTTPHeaders = ["Accept-Language" : AuthFunc.shareInstance.languageSelected == .english ? "English" : "Hindi",
|
|
"access-token": AuthFunc.shareInstance.getAccessToken()]
|
|
|
|
let params : Parameters = ["id" : postID,
|
|
"post_type" : postType,
|
|
"category_id" : categoryID]
|
|
NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Home.favourite_remove, method: .post, parameters: params, headers: headers) { [weak self](result : Result<CommonResponseModel, NetworkManager.APIError>) in
|
|
switch result{
|
|
case .success(let data):
|
|
guard let self else{return}
|
|
switch data.success{
|
|
case 0:
|
|
Utilities.dismissProgressHUD()
|
|
self.vc.toast(msg: data.message ?? "Unrecognised error" , time: 2)
|
|
onCompletion(false)
|
|
case 1:
|
|
Utilities.dismissProgressHUD()
|
|
onCompletion(true)
|
|
default:
|
|
Utilities.dismissProgressHUD()
|
|
onCompletion(false)
|
|
}
|
|
case .failure(let error):
|
|
guard let self else{return}
|
|
Utilities.dismissProgressHUD()
|
|
self.vc.toast(msg: error.localizedDescription , time: 2)
|
|
onCompletion(false)
|
|
}
|
|
}
|
|
}
|
|
|
|
func checkNil(){
|
|
|
|
guard let data = MyListDataTemp.shareInstance.favListingData else{return}
|
|
|
|
if data.showData?.english?.count == 0 && data.singKaraokeData?.count == 0 && data.gameData?.count == 0 && data.audioData?.count == 0 && data.showData?.hindi?.count == 0{
|
|
self.vc.noDataStack.isHidden = false
|
|
}else{
|
|
self.vc.noDataStack.isHidden = true
|
|
}
|
|
}
|
|
|
|
func reloadCollections(){
|
|
vc.webSeriesCV.reloadData()
|
|
vc.webSeriesHindiCV.reloadData()
|
|
vc.audioBooksCV.reloadData()
|
|
vc.karaokeCV.reloadData()
|
|
vc.gamesCV.reloadData()
|
|
}
|
|
}
|