Files
Woka_Native_iOS/WOKA/Home/ViewModel/MyListVM.swift
BilalKhanWDI 1d6add0322 - Added DropDown
- Added api for show listing , made data model, updated the dropdown
- Added api for category, made data model
- Made the dynamic height tableview to show all the shows
- On fav add remove , will refresh the mylist screen
- Added default load on hindi
- On category selection change, updating the show list data
- Handled the fav category id as int, string , string seeprated with “,”
- Modifying my list with bookmark id with above
2024-06-19 20:07:47 +05:30

242 lines
11 KiB
Swift

//
// MyListVM.swift
// WOKA
//
// Created by MacBook Pro on 13/06/24.
//
import Foundation
import Alamofire
class MyListVM{
weak var vc : MyListVC!
var favListingData : FavouriteListingDM.ResultData?
var webSeriesHindi = [FavouriteListingDM.ResultData.ShowDatum]()
let refreshControl = UIRefreshControl()
let feedbackGenerator = UIImpactFeedbackGenerator(style: .light)
func initView(){
setupCell()
Utilities.startProgressHUD()
getFavouriteListing()
NotificationCenter.default.addObserver(self, selector: #selector(languageChanged), name: .languageDidChange, object: nil)
}
@objc func languageChanged(){
self.reloadCollections()
}
func setupCell(){
vc.webSeriesCV.register(UINib(nibName: K.CellIdentifier.Home.favouriteCell, bundle: nil), forCellWithReuseIdentifier: K.CellIdentifier.Home.favouriteCell)
vc.webSeriesCV.delegate = vc.self
vc.webSeriesCV.dataSource = vc.self
vc.webSeriesHindiCV.register(UINib(nibName: K.CellIdentifier.Home.favouriteCell, bundle: nil), forCellWithReuseIdentifier: K.CellIdentifier.Home.favouriteCell)
vc.webSeriesHindiCV.delegate = vc.self
vc.webSeriesHindiCV.dataSource = vc.self
vc.audioBooksCV.register(UINib(nibName: K.CellIdentifier.Home.favouriteCell, bundle: nil), forCellWithReuseIdentifier: K.CellIdentifier.Home.favouriteCell)
vc.audioBooksCV.delegate = vc.self
vc.audioBooksCV.dataSource = vc.self
vc.karaokeCV.register(UINib(nibName: K.CellIdentifier.Home.favouriteCell, bundle: nil), forCellWithReuseIdentifier: K.CellIdentifier.Home.favouriteCell)
vc.karaokeCV.delegate = vc.self
vc.karaokeCV.dataSource = vc.self
vc.gamesCV.register(UINib(nibName: K.CellIdentifier.Home.favouriteCell, bundle: nil), forCellWithReuseIdentifier: K.CellIdentifier.Home.favouriteCell)
vc.gamesCV.delegate = vc.self
vc.gamesCV.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()]
NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Home.favourite_listing, method: .post, 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:
self.refreshControl.endRefreshing()
Utilities.dismissProgressHUD()
self.vc.toast(msg: data.message ?? "Unrecognised error" , time: 2)
reloadCollections()
checkNil()
case 1:
self.refreshControl.endRefreshing()
Utilities.dismissProgressHUD()
guard let data = data.data?.result else{return}
favListingData = nil
webSeriesHindi.removeAll()
favListingData = data
if var hindiData = favListingData?.showData{
/*
Taking out the hindi series , 1-> English , 18-> Hindi
And the saving it to hindi series
*/
webSeriesHindi = hindiData.compactMap { $0 }.filter { ($0.bookmarkCategoryIDS?.components(separatedBy: ",").first == "18" || $0.bookmarkCategoryIDS?.components(separatedBy: ",").last == "18") }
// Deleting the hindi series from main (those with category ID "18")
for (index, element) in favListingData!.showData!.enumerated() {
let bookMarkCatID = element.bookmarkCategoryIDS?.components(separatedBy: ",")
if bookMarkCatID?.count == 2{
hindiData[index-1].bookmarkCategoryIDS = "1"
}else{
hindiData.remove(at: index-1)
}
}
// hindiData.removeAll { $0.bookmarkCategoryIDS?.components(separatedBy: ",").count == 1 ? $0.bookmarkCategoryIDS? }
// Updating the showData with the filtered list
favListingData?.showData = hindiData
}
reloadCollections()
feedbackGenerator.impactOccurred()
checkNil()
default:
break
}
case .failure(let error):
guard let self else{return}
self.refreshControl.endRefreshing()
Utilities.dismissProgressHUD()
checkNil()
self.vc.toast(msg: error.localizedDescription , time: 2)
}
}
}
// MARK: - Like , unlike
func likePost(postID : Int, postType : Int, index : Int , onCompletion : @escaping (Bool) -> Void){
Utilities.startProgressHUD()
let headers : HTTPHeaders = ["Accept-Language" : AuthFunc.shareInstance.languageSelected == .english ? "English" : "Hindi",
"access-token": AuthFunc.shareInstance.getAccessToken()]
let params : Parameters = ["post_id" : postID,
"post_type" : postType]
NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Home.post_like, method: .post, parameters: params, headers: headers) { [weak self](result : Result<CommonResponseModel, 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)
onCompletion(false)
case 1:
Utilities.dismissProgressHUD()
onCompletion(true)
default:
Utilities.dismissProgressHUD()
onCompletion(false)
}
case .failure(let error):
guard let self else{return}
Utilities.dismissProgressHUD()
self.vc.toast(msg: error.localizedDescription , time: 2)
onCompletion(false)
}
}
}
func unlikePost(postID : Int, postType : Int , index : Int, onCompletion : @escaping (Bool) -> Void){
Utilities.startProgressHUD()
let headers : HTTPHeaders = ["Accept-Language" : AuthFunc.shareInstance.languageSelected == .english ? "English" : "Hindi",
"access-token": AuthFunc.shareInstance.getAccessToken()]
let params : Parameters = ["post_id" : postID,
"post_type" : postType]
NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Home.post_unlike, method: .post, parameters: params, headers: headers) { [weak self](result : Result<BaseResponseModel<CommonResponseModel>, 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)
onCompletion(false)
case 1:
Utilities.dismissProgressHUD()
onCompletion(true)
default:
Utilities.dismissProgressHUD()
onCompletion(false)
}
case .failure(let error):
guard let self else{return}
Utilities.dismissProgressHUD()
self.vc.toast(msg: error.localizedDescription , time: 2)
onCompletion(false)
}
}
}
// MARK: - Remove Favourite
func removeFavourite(postID : Int, postType : Int, categoryID : String, index : Int , onCompletion : @escaping (Bool) -> Void){
Utilities.startProgressHUD()
let headers : HTTPHeaders = ["Accept-Language" : AuthFunc.shareInstance.languageSelected == .english ? "English" : "Hindi",
"access-token": AuthFunc.shareInstance.getAccessToken()]
let params : Parameters = ["id" : postID,
"post_type" : postType,
"category_id" : categoryID]
NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Home.favourite_remove, method: .post, parameters: params, headers: headers) { [weak self](result : Result<CommonResponseModel, 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)
onCompletion(false)
case 1:
Utilities.dismissProgressHUD()
onCompletion(true)
default:
Utilities.dismissProgressHUD()
onCompletion(false)
}
case .failure(let error):
guard let self else{return}
Utilities.dismissProgressHUD()
self.vc.toast(msg: error.localizedDescription , time: 2)
onCompletion(false)
}
}
}
func checkNil(){
guard let data = favListingData else{
self.vc.noDataStack.isHidden = false
return
}
if data.showData?.count == 0 && data.singKaraokeData?.count == 0 && data.gameData?.count == 0 && data.audioData?.count == 0 && webSeriesHindi.count == 0{
self.vc.noDataStack.isHidden = false
}else{
self.vc.noDataStack.isHidden = true
}
}
func reloadCollections(){
vc.webSeriesCV.reloadData()
vc.webSeriesHindiCV.reloadData()
vc.audioBooksCV.reloadData()
vc.karaokeCV.reloadData()
vc.gamesCV.reloadData()
}
}