Files
Woka_Native_iOS/WOKA/Home/ViewModel/MyListViewAllVM.swift
Bilal 1eb7727aa2 - Added no data and error handling in my orderdetails
- added lazy loading in myorders
- finalised karaoke with new key
- handled failure to show retry btn in karaoke
- made mylist view all, with api call, modified the api which will display all kind of data.
- made a common module for above
2024-08-09 21:04:55 +05:30

207 lines
10 KiB
Swift

//
// 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<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
case 1:
Utilities.dismissProgressHUD()
refreshControl.endRefreshing()
guard let totalCount = data.data?.result?.totalRecords ,let data = data.data?.result else{return}
switch postType{
case 0: // webSeriesHindi
if favListingData?.gameData == nil {
favListingData = FavouriteListingDM.ResultData(totalRecords: nil, showData: [], videoData: [], gameData: [], singKaraokeData: [], audioData: [])
}
if let webSeriesData = data.showData {
favListingData?.showData?.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?.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 1: // webSeriesEnglish
if favListingData?.gameData == nil {
favListingData = FavouriteListingDM.ResultData(totalRecords: nil, showData: [], videoData: [], gameData: [], singKaraokeData: [], audioData: [])
}
if let webSeriesData = data.showData {
favListingData?.showData?.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?.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: [], 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: [], 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: [], 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
}
}
}