// // MyListViewAllVM.swift // WOKA // // Created by MacBook Pro on 08/08/24. // import Foundation import Alamofire class MyListViewAllVM{ weak var vc : MyListViewAllVC! // var moduleType = Int() var postType = Int() var pageNo = 0 var catID = String() var favListingData : FavouriteListingDM.ResultData? // var webSeriesHindi = [FavouriteListingDM.ResultData.ShowDatum]() let refreshControl = UIRefreshControl() func initView(){ setupCell() Utilities.startProgressHUD() getFavouriteListing() updateData() } func setupCell(){ vc.tableView.register(UINib(nibName: K.CellIdentifier.Home.myListViewAllCell, bundle: nil), forCellReuseIdentifier: K.CellIdentifier.Home.myListViewAllCell) vc.tableView.delegate = vc.self vc.tableView.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 = ["post_type" : postType, "api_version" : "v2", "start" : pageNo, "limit" : "6", "category_id" : catID] NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Home.favourite_listing, method: .post,parameters : params, headers: headers) { [weak self](result : Result, NetworkManager.APIError>) in switch result{ case .success(let data): guard let self else{return} switch data.success{ case 0: Utilities.dismissProgressHUD() refreshControl.endRefreshing() // self.vc.toast(msg: data.message ?? "Unrecognised error" , time: 2) self.vc.loadMoreActivityIndicator.stopAnimating() self.vc.loadMoreActivityIndicator.hidesWhenStopped = true case 1: Utilities.dismissProgressHUD() refreshControl.endRefreshing() guard let totalCount = data.data?.result?.totalRecords ,let data = data.data?.result else{return} switch postType{ case 1: // webseries if catID == "1"{ // english if favListingData?.showData?.english == nil { favListingData = FavouriteListingDM.ResultData(totalRecords: nil, showData: FavouriteListingDM.ResultData.ShowData(hindi: [],english: []),videoData: [],gameData: [],singKaraokeData: [],audioData: []) } if let webSeriesData = data.showData?.english { favListingData?.showData?.english?.append(contentsOf: webSeriesData) } self.vc.tableView.reloadData() self.vc.tableHeight.constant = self.vc.tableView.contentSize.height + 100 self.vc.tableView.layoutIfNeeded() let webSeriesCount = self.favListingData?.showData?.english?.count ?? 0 self.vc.tableHeight.constant = CGFloat(webSeriesCount * 230) self.vc.loadMoreActivityIndicator.stopAnimating() self.vc.loadMoreActivityIndicator.hidesWhenStopped = true if webSeriesCount == totalCount{ self.vc.loadMoreBtn.isHidden = true }else{ self.vc.loadMoreBtn.isHidden = false } }else{// hindi if favListingData?.showData?.hindi == nil { favListingData = FavouriteListingDM.ResultData(totalRecords: nil, showData: FavouriteListingDM.ResultData.ShowData(hindi: [],english: []),videoData: [],gameData: [],singKaraokeData: [],audioData: []) } if let webSeriesData = data.showData?.hindi { favListingData?.showData?.hindi?.append(contentsOf: webSeriesData) } self.vc.tableView.reloadData() self.vc.tableHeight.constant = self.vc.tableView.contentSize.height + 100 self.vc.tableView.layoutIfNeeded() let webSeriesCount = self.favListingData?.showData?.hindi?.count ?? 0 self.vc.tableHeight.constant = CGFloat(webSeriesCount * 230) self.vc.loadMoreActivityIndicator.stopAnimating() self.vc.loadMoreActivityIndicator.hidesWhenStopped = true if webSeriesCount == totalCount{ self.vc.loadMoreBtn.isHidden = true }else{ self.vc.loadMoreBtn.isHidden = false } } case 6: // Games if let gameData = data.gameData{ if favListingData?.gameData == nil { favListingData = FavouriteListingDM.ResultData(totalRecords: nil, showData: FavouriteListingDM.ResultData.ShowData(hindi: [],english: []),videoData: [],gameData: [],singKaraokeData: [],audioData: []) } self.favListingData?.gameData?.append(contentsOf: gameData) self.vc.tableView.reloadData() self.vc.tableHeight.constant = self.vc.tableView.contentSize.height + 100 self.vc.tableView.layoutIfNeeded() let gameCount = self.favListingData?.gameData?.count ?? 0 self.vc.tableHeight.constant = CGFloat(gameCount * 230) self.vc.loadMoreActivityIndicator.stopAnimating() self.vc.loadMoreActivityIndicator.hidesWhenStopped = true if gameCount == totalCount{ self.vc.loadMoreBtn.isHidden = true }else{ self.vc.loadMoreBtn.isHidden = false } } case 7: if let audioData = data.audioData{ if favListingData?.audioData == nil { favListingData = FavouriteListingDM.ResultData(totalRecords: nil, showData: FavouriteListingDM.ResultData.ShowData(hindi: [],english: []),videoData: [],gameData: [],singKaraokeData: [],audioData: []) } self.favListingData?.audioData?.append(contentsOf: audioData) self.vc.tableView.reloadData() self.vc.tableHeight.constant = self.vc.tableView.contentSize.height + 100 self.vc.tableView.layoutIfNeeded() let audioCount = self.favListingData?.audioData?.count ?? 0 self.vc.tableHeight.constant = CGFloat(audioCount * 230) self.vc.loadMoreActivityIndicator.stopAnimating() self.vc.loadMoreActivityIndicator.hidesWhenStopped = true if audioCount == totalCount{ self.vc.loadMoreBtn.isHidden = true }else{ self.vc.loadMoreBtn.isHidden = false } } case 8: //KAraoke if let karaokeData = data.singKaraokeData{ if favListingData?.showData == nil { favListingData = FavouriteListingDM.ResultData(totalRecords: nil, showData: FavouriteListingDM.ResultData.ShowData(hindi: [],english: []),videoData: [],gameData: [],singKaraokeData: [],audioData: []) } self.favListingData?.singKaraokeData?.append(contentsOf: karaokeData) self.vc.tableView.reloadData() self.vc.tableHeight.constant = self.vc.tableView.contentSize.height + 100 self.vc.tableView.layoutIfNeeded() let karaokeCount = self.favListingData?.singKaraokeData?.count ?? 0 self.vc.tableHeight.constant = CGFloat(karaokeCount * 230) self.vc.loadMoreActivityIndicator.stopAnimating() self.vc.loadMoreActivityIndicator.hidesWhenStopped = true if karaokeCount == totalCount{ self.vc.loadMoreBtn.isHidden = true }else{ self.vc.loadMoreBtn.isHidden = false } } default: break } default: break } case .failure(let error): guard let self else{return} Utilities.dismissProgressHUD() refreshControl.endRefreshing() self.vc.toast(msg: error.localizedDescription , time: 2) } } } func updateData(){ // non mandatory (values: 1=series, 2=season, 3=episode, 4=video, 5=paint, 6=game, 7=audio, 8=kareoke video, 9=shop product, 10=parental video, 11=article) switch postType{ case 1: // webSeries self.vc.title = "WEB SERIES" case 6: // Games self.vc.title = "GAMES" case 7: // AUDIOBOOKS self.vc.title = "AUDIO BOOKS" case 8: //KAraoke self.vc.title = "KARAOKE" default: break } } }