Files
Woka_Native_iOS/WOKA/WebSeries/ViewModel/WebSeriesSeasonVM.swift
Bilal 1f650bd2b7 - added api for fetching the episode as per the selected category
- made the category collection cells dynamic
- handled the category selection will call the episode api
- solved activity indicator view at the splash start-up
- handled the app lifecycle for live tv app going in foreground and background
- handled play pause while the live tv view apperas and disappears
- Made the likes and favourites dynamic on episode screen
2024-06-21 19:35:40 +05:30

206 lines
8.5 KiB
Swift

//
// WebSeriesSeasonVM.swift
// WOKA
//
// Created by MacBook Pro on 20/06/24.
//
import Foundation
import Alamofire
class WebSeriesSeasonVM{
weak var vc : WebSeriesSeasonVC!
// var watchShowID : Int?
var categoryID : Int?
var episodeSelectedCateogory : Int?
var seasonListingData = [SeasonListingDM.Result]()
var seasonEpisodeData = [SeasonEpisodeListingDM.ResultData]()
var showData : WebSeriesShowListDM.ShowDatum?
func initView(){
getSeasonListing()
setupCell()
setShowData()
vc.shareStack.addTapGesture {
print("share")
}
}
func setShowData(){
guard let showData else{return}
vc.totalLikes.text = showData.likesCount?.toString() ?? "0"
if let like = showData.isLiked{
switch like{
case true:
vc.likeIcon.image = UIImage(systemName: "hand.thumbsup.fill")
vc.likeLabel.text = "Liked"
case false:
vc.likeIcon.image = UIImage(systemName: "hand.thumbsup")
vc.likeLabel.text = "Like"
}
}
if let favourite = showData.markAsFavourite{
if let categoryIds = showData.favouriteCategoryIDS?.rawValue { // if string, it means category is selected for multiple language
let components = categoryIds.components(separatedBy: ",")
if favourite == true && (components.first == categoryID?.toString() || components.last == categoryID?.toString()){
vc.addIcon.image = UIImage(systemName: "heart.fill")
vc.addLabel.text = "Added"
}else{
vc.addIcon.image = UIImage(systemName: "heart")
vc.addLabel.text = "Add"
}
return
}
if favourite == true && showData.favouriteCategoryIDS?.intValue == categoryID{
vc.addIcon.image = UIImage(systemName: "heart.fill")
vc.addLabel.text = "Added"
}else{
vc.addIcon.image = UIImage(systemName: "heart")
vc.addLabel.text = "Add"
}
// switch favourite{
// case true:
// favBtn.setImage(UIImage(systemName: "heart.fill"), for: .normal)
// case false:
// favBtn.setImage(UIImage(systemName: "heart"), for: .normal)
//
// }
}
}
func setupCell(){
vc.categoryCV.register(UINib(nibName: K.CellIdentifier.WebSeries.seasonCategoryCell, bundle: nil), forCellWithReuseIdentifier: K.CellIdentifier.WebSeries.seasonCategoryCell)
vc.categoryCV.delegate = vc.self
vc.categoryCV.dataSource = vc.self
vc.episodeTableView.register(UINib(nibName: K.CellIdentifier.WebSeries.webSeriesEpisodeCell, bundle: nil), forCellReuseIdentifier: K.CellIdentifier.WebSeries.webSeriesEpisodeCell)
vc.episodeTableView.delegate = vc.self
vc.episodeTableView.dataSource = vc.self
}
// MARK: - Api Calls
func getSeasonListing(){
Utilities.startProgressHUD()
guard let watchShowID = showData?.id, let categoryID else{return}
let headers : HTTPHeaders = ["access-token" : AuthFunc.shareInstance.getAccessToken()]
let params : Parameters = ["watch_show_id" : watchShowID,
"category_id" : categoryID]
NetworkManager.shareInstance.apiRequest(url: APIEndPoints.WebSeries.season_listing, method: .post,parameters: params,headers : headers) { [weak self](result : Result<BaseResponseModel<SeasonListingDM>, 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?.result else{return}
self.seasonListingData = data
setSeasonData()
episodeSelectedCateogory = seasonListingData.first?.id
getSeasonEpisode()
self.vc.categoryCV.reloadData()
default:
break
}
case .failure(let error):
guard let self else{
Utilities.dismissProgressHUD()
return
}
Utilities.dismissProgressHUD()
vc.toast(msg: error.localizedDescription , time: 2)
}
}
}
func setSeasonData(){
guard let data = seasonListingData.first else{return}
vc.seasonImage.imageURL(data.thumbnailPath!, color: UIColor.appColor(.TextDarkBlue)!)
if AuthFunc.shareInstance.getDefaultLanguage() == .english{
let englishData = data.seasonMoreDetails?.filter({$0.languageMasterID == 1}).first
vc.seasonTitle.text = englishData?.title
if let desc = englishData?.description?.replacingOccurrences(of: "<br>", with: "").htmlToAttributedString{
let sizeText = NSMutableAttributedString(attributedString: desc)
sizeText.setFontFace(font: FontCustom.shareInstance.customFont(fontName: .Exo2_Regular, size: 15),color: UIColor.appColor(.TextDarkBlue)!)
self.vc.seasonDesc.attributedText = sizeText
}
}else{
let hindiData = data.seasonMoreDetails?.filter({$0.languageMasterID == 2}).first
vc.seasonTitle.text = hindiData?.title
if let desc = hindiData?.description?.replacingOccurrences(of: "<br>", with: "").htmlToAttributedString{
let sizeText = NSMutableAttributedString(attributedString: desc)
sizeText.setFontFace(font: FontCustom.shareInstance.customFont(fontName: .Exo2_Regular, size: 15),color: UIColor.appColor(.TextDarkBlue)!)
self.vc.seasonDesc.attributedText = sizeText
}
}
vc.seasonDate.text = data.releaseYear?.toString()
vc.seasonEpisodes.text = data.noOfEpisodes?.toString()
vc.seasonMediaType.text = data.mediaType
}
func getSeasonEpisode(){
Utilities.startProgressHUD()
guard let watchShowID = showData?.id, let episodeSelectedCateogory else{return}
let headers : HTTPHeaders = ["access-token" : AuthFunc.shareInstance.getAccessToken()]
let params : Parameters = ["watch_show_id" : watchShowID,
"season_id" : episodeSelectedCateogory]
NetworkManager.shareInstance.apiRequest(url: APIEndPoints.WebSeries.season_episode_listing, method: .post,parameters: params,headers : headers) { [weak self](result : Result<BaseResponseModel<SeasonEpisodeListingDM>, 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?.result else{return}
self.seasonEpisodeData.removeAll()
self.seasonEpisodeData = data
self.vc.episodeTableView.reloadData()
self.vc.tableHeight.constant = self.vc.episodeTableView.contentSize.height + 100
self.vc.episodeTableView.layoutIfNeeded()
self.vc.tableHeight.constant = self.vc.episodeTableView.contentSize.height
default:
break
}
case .failure(let error):
guard let self else{
Utilities.dismissProgressHUD()
return
}
Utilities.dismissProgressHUD()
vc.toast(msg: error.localizedDescription , time: 2)
}
}
}
}