// // 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(){ getFavouriteListing() } // 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" : "10"] 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() self.vc.toast(msg: data.message ?? "Unrecognised error" , time: 2) case 1: Utilities.dismissProgressHUD() guard let data = data.data?.result else{return} 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 } } default: break } case .failure(let error): guard let self else{return} Utilities.dismissProgressHUD() self.vc.toast(msg: error.localizedDescription , time: 2) } } } }