Files
Woka_Native_iOS/WOKA/WebSeries/ViewModel/WebSeriesVM.swift
BilalKhanWDI 95495dc414 - Added native shimmer effect.
- Updated my list for handling the comma separated language.
- Added shimmer to manila , language , list table
- Added action for dropdown, if user clicks outside the dropdown it ill dismiss and change the arrow direction
- Handled shimmer with api call, removed the progress hud
- Updated Lingual file for webSeries VC
2024-06-20 19:51:39 +05:30

372 lines
16 KiB
Swift

//
// WebSeriesVM.swift
// WOKA
//
// Created by MacBook Pro on 18/06/24.
//
import Foundation
import Alamofire
class WebSeriesVM{
weak var vc : WebSeriesVC!
var continueWatchingData = [ContinueWatchingDM.ResultData]()
var categoryListingData = [CategoryListingDM.ResultData]()
var showData = [WebSeriesShowListDM.ShowDatum]()
let dropDownModule = DropDown()
var dataSource = [String]()
func initView(){
setupCell()
self.vc.tableHeight.constant = self.vc.showListingTableView.contentSize.height + 100
self.vc.showListingTableView.layoutIfNeeded()
self.vc.tableHeight.constant = self.vc.showListingTableView.contentSize.height
startShimmer()
vc.scrollView.indicatorStyle = .white // or .white
let color1 = #colorLiteral(red: 0.5921568627, green: 0.2588235294, blue: 0.8941176471, alpha: 1)
let color2 = #colorLiteral(red: 0.368627451, green: 0.1215686275, blue: 0.768627451, alpha: 1)
vc.title = "WEB SERIES".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue)
vc.view.applyGradient(colors: [color2, color1], startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint(x: 0.8, y: 0))
getContinueWatching()
getCategoryListing()
// for the first load always send hindi as it is default category
self.getShowListing(categoryID: 18)
vc.languageStack.addTapGesture { [weak self] in
guard let self else {return}
dropDownModule.show()
self.vc.expandBtn.setImage(UIImage(named: "SupportUpArrow"), for: .normal)
}
}
func startShimmer(){
vc.headerView.startShimmer()
vc.masilaTrailerView.startShimmer()
vc.videoLanguageView.startShimmer()
}
func stopShimmer(){
self.vc.headerView.stopShimmer()
self.vc.masilaTrailerView.stopShimmer()
self.vc.videoLanguageView.stopShimmer()
}
func setupCell(){
vc.continueWatchingCV.register(UINib(nibName: K.CellIdentifier.WebSeries.webSeriesCell, bundle: nil), forCellWithReuseIdentifier: K.CellIdentifier.WebSeries.webSeriesCell)
vc.continueWatchingCV.delegate = vc.self
vc.continueWatchingCV.dataSource = vc.self
vc.showListingTableView.register(UINib(nibName: K.CellIdentifier.WebSeries.webSeriesShowListingCell, bundle: nil), forCellReuseIdentifier: K.CellIdentifier.WebSeries.webSeriesShowListingCell)
vc.showListingTableView.delegate = vc.self
vc.showListingTableView.dataSource = vc.self
}
// MARK: - DropDown
func initDropDown(){
dropDownModule.anchorView = vc.languageStack
dropDownModule.dataSource = dataSource
dropDownModule.cornerRadius = 10
dropDownModule.bottomOffset = CGPoint(x: 0, y:(dropDownModule.anchorView?.plainView.bounds.height)!)
dropDownModule.topOffset = CGPoint(x: 0, y:-(dropDownModule.anchorView?.plainView.bounds.height)!)
dropDownModule.direction = .bottom
dropDownModule.textFont = FontCustom.shareInstance.customFont(fontName: .Exo2_Bold, size: 14)
dropDownModule.shadowColor = UIColor.appColor(.TextDarkBlue)!
dropDownModule.backgroundColor = .white
dropDownModule.shadowOffset = CGSize(width: 0, height: 1)
dropDownModule.separatorColor = UIColor.lightGray
dropDownModule.width = vc.languageStack.frame.width
dropDownModule.selectionBackgroundColor = UIColor.appColor(.TextDarkBlue)!
dropDownModule.selectedTextColor = .white
dropDownModule.selectRow(at: 0)
// handle if user clicks outside
dropDownModule.cancelAction = { [weak self] in
guard let self else{return}
vc.expandBtn.setImage(UIImage(named: "SupportBottomArrow"), for: .normal)
}
dropDownModule.selectionAction = { [weak self] (index: Int, item: String) in
guard let self else{return}
vc.languageLabel.text = item
vc.expandBtn.setImage(UIImage(named: "SupportBottomArrow"), for: .normal)
//if the language is changed call the showlisting api
if let id = categoryListingData[index].id {
getShowListing(categoryID: id)
}
}
}
// MARK: - Api Calls
func getContinueWatching(){
// Utilities.startProgressHUD()
let headers : HTTPHeaders = ["access-token" : AuthFunc.shareInstance.getAccessToken()]
let params : Parameters = ["post_type" : 3] // 3- webseries
NetworkManager.shareInstance.apiRequest(url: APIEndPoints.WebSeries.continue_watching, method: .post,parameters: params,headers : headers) { [weak self](result : Result<BaseResponseModel<ContinueWatchingDM>, 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)
self.vc.continueWatchingStack.isHidden = true
case 1:
Utilities.dismissProgressHUD()
guard let data = data.data?.result else{return}
if data.count == 0{
self.vc.continueWatchingStack.isHidden = true
}else{
self.vc.continueWatchingStack.isHidden = false
}
self.continueWatchingData = data
self.vc.continueWatchingCV.reloadData()
default:
break
}
case .failure(let error):
guard let self else{
Utilities.dismissProgressHUD()
return
}
Utilities.dismissProgressHUD()
self.vc.continueWatchingStack.isHidden = true
vc.toast(msg: error.localizedDescription , time: 2)
}
}
}
func getShowListing(categoryID : Int){
// Utilities.startProgressHUD()
let headers : HTTPHeaders = ["access-token" : AuthFunc.shareInstance.getAccessToken()]
let params : Parameters = ["category_id" : categoryID] // from category listing api , default will be 1
NetworkManager.shareInstance.apiRequest(url: APIEndPoints.WebSeries.watch_show_listing, method: .post,parameters: params,headers: headers) { [weak self](result : Result<BaseResponseModel<WebSeriesShowListDM>, 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?.showData else{return}
self.showData.removeAll()
self.showData = data
self.vc.showListingTableView.reloadData()
self.vc.tableHeight.constant = self.vc.showListingTableView.contentSize.height + 100
self.vc.showListingTableView.layoutIfNeeded()
self.vc.tableHeight.constant = self.vc.showListingTableView.contentSize.height
self.stopShimmer()
default:
break
}
case .failure(let error):
guard let self else{
Utilities.dismissProgressHUD()
return
}
Utilities.dismissProgressHUD()
vc.toast(msg: error.localizedDescription , time: 2)
}
}
}
func getCategoryListing(){
// Utilities.startProgressHUD()
let headers : HTTPHeaders = ["access-token" : AuthFunc.shareInstance.getAccessToken()]
let params : Parameters = ["module_id" : 7] // 7 - webseries
NetworkManager.shareInstance.apiRequest(url: APIEndPoints.WebSeries.category_listing, method: .post,parameters: params,headers : headers) { [weak self](result : Result<BaseResponseModel<CategoryListingDM>, 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.categoryListingData = data
dataSource = categoryListingData.map({$0.categoryName ?? "Unknown"})
initDropDown()
default:
break
}
case .failure(let error):
guard let self else{
Utilities.dismissProgressHUD()
return
}
Utilities.dismissProgressHUD()
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 : 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 = ["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 addFavourite(postID : Int, postType : Int, categoryID : 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,
"category_id" : categoryID]
NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Home.favourite_add, 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)
}
}
}
}