Files
Woka_Native_iOS/WOKA/Games/ViewModel/GamesListVM.swift
BilalKhanWDI 41e770f991 - Completed lingual file
- Completed the default top header set as per the selcection on user, first load will select 0th index
- Completed player for audio books
- Handled error response for audio listing
- AudioBook Details View added like fav
- Fixed the nav bar color issue.
- Finished games module
2024-07-04 19:48:15 +05:30

161 lines
6.5 KiB
Swift

//
// GamesListVM.swift
// WOKA
//
// Created by MacBook Pro on 04/07/24.
//
import UIKit
import Alamofire
class GamesListVM{
weak var vc : GamesListVC!
var gameData = [GamesListDM.GameDatum]()
var indexToLoad = 0
func initView(){
setupCell()
let color1 = #colorLiteral(red: 0.8, green: 0.6078431373, blue: 0.1098039216, alpha: 1)
let color2 = #colorLiteral(red: 0.8, green: 0.2901960784, blue: 0.1098039216, alpha: 1)
self.vc.view.applyGradient(colors: [color1,color2], startPoint: .Point.left.point , endPoint: .Point.right.point)
startShimmer()
getGamesListing()
}
func setupCell(){
vc.gamesListingTableView.register(UINib(nibName: K.CellIdentifier.WebSeries.webSeriesShowListingCell, bundle: nil), forCellReuseIdentifier: K.CellIdentifier.WebSeries.webSeriesShowListingCell)
vc.gamesListingTableView.delegate = vc.self
vc.gamesListingTableView.dataSource = vc.self
}
func startShimmer(){
vc.headerView.startShimmer()
vc.gamesLoadingView.startShimmer()
}
func stopShimmer(){
vc.headerView.stopShimmer()
vc.gamesLoadingView.stopShimmer()
}
func setHeaderData(){
let data = gameData[indexToLoad]
if let url = data.thumbnailPath{
self.vc.headerImage.imageURL(url, color: .white)
}
if AuthFunc.shareInstance.getDefaultLanguage() == .english{
let englishData = data.contentMoreDetails?.filter({$0.languageMasterID == 1}).first
vc.headerTitleLabel.text = englishData?.title
}else{
let hindiData = data.contentMoreDetails?.filter({$0.languageMasterID == 2}).first
vc.headerTitleLabel.text = hindiData?.title
}
}
func updateTableHeight(){
self.vc.tableHeight.constant = self.vc.gamesListingTableView.contentSize.height + 100
self.vc.gamesListingTableView.layoutIfNeeded()
self.vc.tableHeight.constant = self.vc.gamesListingTableView.contentSize.height
}
// MARK: - Get Games Data
func getGamesListing(){
let headers : HTTPHeaders = ["access-token" : AuthFunc.shareInstance.getAccessToken()]
NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Games.game_listing, method: .post,headers: headers) { [weak self](result : Result<BaseResponseModel<GamesListDM>, NetworkManager.APIError>) in
switch result{
case .success(let data):
guard let self else{
Utilities.dismissProgressHUD()
return
}
switch data.success{
case 0:
/*
Error
*/
Utilities.dismissProgressHUD()
vc.toast(msg: data.message ?? "Unrecognised error" , time: 2)
case 1:
Utilities.dismissProgressHUD()
guard let data = data.data?.gameData else{return}
self.gameData.removeAll()
self.gameData = data
self.vc.gamesListingTableView.reloadData()
self.vc.tableHeight.constant = self.vc.gamesListingTableView.contentSize.height + 100
self.vc.gamesListingTableView.layoutIfNeeded()
self.vc.tableHeight.constant = self.vc.gamesListingTableView.contentSize.height
self.setHeaderData()
self.stopShimmer()
default:
break
}
case .failure(let error):
guard let self else{
Utilities.dismissProgressHUD()
return
}
Utilities.dismissProgressHUD()
Utilities.alertWithBtnCompletion(title: "Error", msgBody: error.localizedDescription, okBtnStr: "Retry?", vc: self.vc) { isDone in
if isDone{
self.getGamesListing()
}
}
}
}
}
// MARK: - Update Fav Likes
func updateFavLikes(type : FavCellCLick, index : Int){
let data = gameData[index]
switch type {
case .favourite:
guard let isFav = data.markAsFavourite ,let postID = data.id,let postType = data.contentMoreDetails?.first?.postType else{return}
if isFav == true {
LikeFavCommonFunc.shareInstance.removeFavourite(postID: postID, postType: postType, categoryID: 0, vc: self.vc) { [unowned self] isDone in
if isDone{
gameData[index].markAsFavourite = false
vc.gamesListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none)
K.GVar.reloadMyList = true
}
}
}else{
LikeFavCommonFunc.shareInstance.addFavourite(postID: postID, postType: postType, categoryID: 0, vc: self.vc) { [unowned self] isDone in
if isDone{
gameData[index].markAsFavourite = true
vc.gamesListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none)
K.GVar.reloadMyList = true
}
}
}
return
case .liked:
guard let isLiked = data.isLiked ,let postID = data.id,let postType = data.contentMoreDetails?.first?.postType else{return}
if isLiked{
LikeFavCommonFunc.shareInstance.unlikePost(postID: postID, postType: postType, vc: self.vc) { [unowned self] isDone in
if isDone{
gameData[index].isLiked = false
gameData[index].likesCount! -= 1
vc.gamesListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none)
K.GVar.reloadMyList = true
}
}
}else{
LikeFavCommonFunc.shareInstance.likePost(postID: postID, postType: postType, vc: self.vc){ [unowned self] isDone in
if isDone{
gameData[index].isLiked = true
gameData[index].likesCount! += 1
vc.gamesListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none)
K.GVar.reloadMyList = true
}
}
}
}
}
}