Files
Woka_Native_iOS/WOKA/Home/ViewModel/MyListViewAllVM.swift

209 lines
11 KiB
Swift
Raw Normal View History

2024-08-08 18:47:13 +05:30
//
// MyListViewAllVM.swift
// WOKA
//
// Created by MacBook Pro on 08/08/24.
//
import Foundation
import Alamofire
class MyListViewAllVM{
weak var vc : MyListViewAllVC!
2024-08-08 19:50:19 +05:30
// var moduleType = Int()
2024-08-08 18:47:13 +05:30
var postType = Int()
var pageNo = 0
var catID = String()
2024-08-08 18:47:13 +05:30
var favListingData : FavouriteListingDM.ResultData?
2024-08-14 20:06:28 +05:30
// var webSeriesHindi = [FavouriteListingDM.ResultData.ShowDatum]()
let refreshControl = UIRefreshControl()
2024-08-08 18:47:13 +05:30
func initView(){
2024-08-08 19:50:19 +05:30
setupCell()
Utilities.startProgressHUD()
2024-08-08 18:47:13 +05:30
getFavouriteListing()
updateData()
2024-08-08 18:47:13 +05:30
}
2024-08-08 19:50:19 +05:30
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
}
2024-08-08 18:47:13 +05:30
// 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]
2024-08-08 18:47:13 +05:30
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:
Utilities.dismissProgressHUD()
refreshControl.endRefreshing()
// self.vc.toast(msg: data.message ?? "Unrecognised error" , time: 2)
self.vc.loadMoreActivityIndicator.stopAnimating()
self.vc.loadMoreActivityIndicator.hidesWhenStopped = true
2024-08-08 18:47:13 +05:30
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: [])
2024-08-08 18:47:13 +05:30
}
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
2024-08-08 18:47:13 +05:30
}
}
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
}
2024-08-08 18:47:13 +05:30
}
default:
break
2024-08-08 18:47:13 +05:30
}
2024-08-08 19:50:19 +05:30
2024-08-08 18:47:13 +05:30
default:
break
}
case .failure(let error):
guard let self else{return}
Utilities.dismissProgressHUD()
refreshControl.endRefreshing()
2024-08-08 18:47:13 +05:30
self.vc.toast(msg: error.localizedDescription , time: 2)
}
}
}
2024-08-08 19:50:19 +05:30
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"
2024-08-08 19:50:19 +05:30
case 8: //KAraoke
self.vc.title = "KARAOKE"
default:
break
}
}
2024-08-08 18:47:13 +05:30
}