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

131 lines
5.6 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 favListingData : FavouriteListingDM.ResultData?
var webSeriesHindi = [FavouriteListingDM.ResultData.ShowDatum]()
func initView(){
setupCell()
getFavouriteListing()
}
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(){
Utilities.startProgressHUD()
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" : "10"]
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()
self.vc.toast(msg: data.message ?? "Unrecognised error" , time: 2)
case 1:
Utilities.dismissProgressHUD()
guard let data = data.data?.result else{return}
self.favListingData = data
if var hindiData = favListingData?.showData{
/*
Taking out the hindi series , 1-> English , 18-> Hindi
And the saving it to hindi series, also modify the bookmark category ids
*/
webSeriesHindi = hindiData.compactMap { $0 }.filter {
($0.bookmarkCategoryIDS?.components(separatedBy: ",").first == "18" || $0.bookmarkCategoryIDS?.components(separatedBy: ",").last == "18")
}.map { element -> FavouriteListingDM.ResultData.ShowDatum in
var modifiedElement = element
modifiedElement.bookmarkCategoryIDS = "18"
modifiedElement.categoryMasterID = "18"
return modifiedElement
}
if let data = favListingData?.showData{
var indicesToRemove = [Int]()
// Deleting the hindi series from main (those with category ID "18")
for (index, element) in data.enumerated() {
if let bookMarkCatID = element.bookmarkCategoryIDS?.components(separatedBy: ","){
if bookMarkCatID.count > 1{ // means multiple language
/*
if its greater than one , its for sure we have extracted the hindi episode
*/
hindiData[index].bookmarkCategoryIDS = "1"
}else{ // means single language
if bookMarkCatID.first == "18"{
indicesToRemove.append(index)
}
}
}
}
// Remove items in reverse order to avoid index shifting issues
for index in indicesToRemove.reversed() {
hindiData.remove(at: index)
}
}
// Updating the showData with the filtered list
// MyListDataTemp.shareInstance.favListingData?.showData = hindiData.map({$0.categoryMasterID = 1})
favListingData?.showData = hindiData.map { item in
var modifiedItem = item
modifiedItem.categoryMasterID = "1"
return modifiedItem
}
}
self.vc.tableView.reloadData()
default:
break
}
case .failure(let error):
guard let self else{return}
Utilities.dismissProgressHUD()
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 8: //KAraoke
self.vc.title = "KARAOKE"
default:
break
}
}
}