Files
Woka_Native_iOS/WOKA/WebSeries/Controller/WebSeriesVC.swift
BilalKhanWDI d136a59680 - Tc 55 fixed
- Fixed an issue where the guest user was going to Home Screen when not connected to network.
- Also now if the user is guest, we will check if the live stream urls are fetched or not
- TC 57,58, 56
- TC 50
- Added hindi language to shop and cart and remove alert
2024-09-03 20:20:17 +05:30

441 lines
22 KiB
Swift

//
// WebSeriesVC.swift
// WOKA
//
// Created by MacBook Pro on 18/06/24.
//
import UIKit
protocol ReloadSeriesFavLike{
func updateRows(index : Int, type : FavCellCLick, isFav : Bool? , isLike : Bool?, id : Int?)
}
class WebSeriesVC: UIViewController {
@IBOutlet weak var headerView: ShimmerEffectView!
@IBOutlet weak var headerViewLabel: UILabel!
@IBOutlet weak var headerViewLabelHeight: NSLayoutConstraint!
@IBOutlet weak var headerViewImage: UIImageView!
@IBOutlet weak var headerBtn: LocalisedElementsButton!
@IBOutlet weak var masilaTrailerView: ShimmerEffectView!
@IBOutlet weak var videoLanguageView: ShimmerEffectView!
@IBOutlet weak var topLabel: UILabel!
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var headerViewTopConstraint: NSLayoutConstraint!
@IBOutlet weak var languageLabel: UILabel!
@IBOutlet weak var languageStack: UIStackView!
@IBOutlet weak var expandBtn: UIButton!
@IBOutlet weak var headerHeight: NSLayoutConstraint!
@IBOutlet weak var continueWatchingCV: UICollectionView!
@IBOutlet weak var showListingTableView: UITableView!
@IBOutlet weak var tableHeight: NSLayoutConstraint!
@IBOutlet weak var continueWatchingStack: UIStackView!
@IBOutlet weak var noDataView: UIView!
@IBOutlet weak var loadMoreBtn: LocalisedElementsButton!
@IBOutlet weak var loadMoreActivityIndicator: UIActivityIndicatorView!
var vm = WebSeriesVM()
private var isFirstLoad = true
override func viewDidLoad() {
super.viewDidLoad()
vm.vc = self
scrollView.delegate = self
vm.initView()
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
isFirstLoad = false
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(false, animated: animated)
self.navigationController?.setColor(color: .white)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.setNavigationBarHidden(true, animated: animated)
if self.isMovingFromParent {
// Back button was pressed
PersistentStorage.shared.addOthersCount()
}
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
// Customize the navigation bar's appearance
self.navigationController?.setColor(color: .black)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if !isFirstLoad{
if K.GVar.reloadContinueWebSeries{
K.GVar.reloadContinueWebSeries = false
vm.getContinueWatching()
}
}else{
K.GVar.reloadContinueWebSeries = false
}
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
vm.updateTableHeight()
}
// MARK: - Tap Handler
@IBAction func playTrailer(_ sender: LocalisedElementsButton) {
let item = JwPlayerItemCreate(url: APIEndPoints.StaticURLs.masilaUrl, poster: nil, titles: "Masila")
JWPlayerManager.shared.presentPlayer(from: self, playerItems: [item], contentType: .trailer, videoIDs: [0])
PersistentStorage.shared.addTrailerCount()
}
@IBAction func expandLanguageTapped(_ sender: UIButton) {
vm.dropDownModule.show()
PersistentStorage.shared.addOthersCount()
self.expandBtn.setImage(UIImage(named: "SupportUpArrow"), for: .normal)
}
@IBAction func loadMoreBtnTapped(_ sender: LocalisedElementsButton) {
PersistentStorage.shared.addOthersCount()
loadMoreBtn.isHidden = true
vm.pageNo += 1
loadMoreActivityIndicator.startAnimating()
if let index = vm.dropDownModule.indexForSelectedRow, let categoryIndex = vm.categoryListingData[index].id{
vm.getShowListing(categoryID: categoryIndex, isBtnClick: true)
}
PersistentStorage.shared.addOthersCount()
}
@IBAction func retryBtnTapped(_ sender: LocalisedElementsButton) {
PersistentStorage.shared.addOthersCount()
noDataView.isHidden = true
// for the first load always send hindi as it is default category
vm.startShimmer()
// showListingTableView.reloadData()
vm.getShowListing(categoryID: 18)
self.vm.getCategoryListing()
}
}
// MARK: - TableView DataSource , Delegates
extension WebSeriesVC : TableViewSRC{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return vm.showData.count == 0 ? 2 : vm.showData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: K.CellIdentifier.WebSeries.webSeriesShowListingCell) as! WebSeriesShowListingCell
if vm.showData.count == 0{
cell.showShimmer()
}else{
let data = vm.showData[indexPath.row]
let categoryID = vm.categoryListingData[vm.dropDownModule.indexForSelectedRow ?? 0].id ?? 0
cell.setData(data: data,selectedCategory: categoryID)
cell.stopShimmer()
}
cell.btnTapped = { [self] (type) -> Void in
updateFavLikes(type: type, index: indexPath.row)
}
return cell
}
func updateFavLikes(type : FavCellCLick, index : Int){
let data = vm.showData[index]
if let postID = data.id, let categoryID = vm.categoryListingData[vm.dropDownModule.indexForSelectedRow ?? 0].id{
PersistentStorage.shared.addWebSeries(catID: categoryID, postID: postID, postType: .series)
}
let categoryID = vm.categoryListingData[vm.dropDownModule.indexForSelectedRow ?? 0].id ?? 0
switch type {
case .favourite:
guard let isFav = data.markAsFavourite ,let postID = data.id,let postType = data.contentMoreDetails?.first?.postType else{return}
if let categoryIds = data.favouriteCategoryIDS?.rawValue {
let components = categoryIds.components(separatedBy: ",")
if isFav == true && (components.first == categoryID.toString() || components.last == categoryID.toString()){
LikeFavCommonFunc.shareInstance.removeFavourite(postID: postID, postType: postType, categoryID: categoryID, vc: self) { [unowned self] isDone in
if isDone{
vm.showData[index].markAsFavourite = false
vm.showData[index].favouriteCategoryIDS = ValueWrapper.stringValue("")
showListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none)
if categoryID == 18{ // hindi
K.GVar.reloadHindiWebseries = true
}else{ // english
K.GVar.reloadEnglishWebseries = true
}
// Update for mylist
// if let id = vm.categoryListingData[vm.dropDownModule.indexForSelectedRow ?? 0].id{
// if id == 1{ // english
// if let index = MyListDataTemp.shareInstance.favListingData?.showData?.english?.firstIndex(where: {$0.id == postID}){
// MyListDataTemp.shareInstance.favListingData?.showData?.english?.remove(at: index)
// K.GVar.myListSoftReload = true
// }
// }else{ // hindi
// if let index = MyListDataTemp.shareInstance.favListingData?.showData?.hindi?.firstIndex(where: {$0.id == postID}){
// MyListDataTemp.shareInstance.favListingData?.showData?.hindi?.remove(at: index)
// K.GVar.myListSoftReload = true
// }
// }
// }
}
}
}else{
LikeFavCommonFunc.shareInstance.addFavourite(postID: postID, postType: postType, categoryID: categoryID, vc: self) { [unowned self] isDone in
if isDone{
vm.showData[index].markAsFavourite = true
vm.showData[index].favouriteCategoryIDS = ValueWrapper.stringValue(categoryID.toString())
showListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none)
if categoryID == 18{ // hindi
K.GVar.reloadHindiWebseries = true
}else{ // english
K.GVar.reloadEnglishWebseries = true
}
// Update for mylist
// if let id = vm.categoryListingData[vm.dropDownModule.indexForSelectedRow ?? 0].id{
// if id == 1{ // english
// if !MyListDataTemp.shareInstance.isDatafetched {return}
// let showData = vm.showData[index]
// MyListDataTemp.shareInstance.favListingData?.showData?.append(FavouriteListingDM.ResultData.ShowDatum(id: showData.id, title: showData.title, description: showData.description, thumbnailPath: showData.thumbnailPath, showType: showData.showType, totalSeasons: showData.totalSeasons, totalEpisodes: showData.totalEpisodes, ageRangeMasterID: showData.ageRangeMasterID, genderMasterID: showData.genderMasterID, categoryMasterID: "1", contentMoreDetails: showData.contentMoreDetails, markAsFavourite: showData.markAsFavourite, isLiked: showData.isLiked, likesCount: showData.likesCount, viewsCount: showData.viewsCount, bookmarkCount: showData.bookmarkCount, bookmarkCategoryIDS: "1"))
// K.GVar.myListSoftReload = true
// }else{ // hindi
// // return if the data is not fetched. IT means user has not loaded mylist yet
// if !MyListDataTemp.shareInstance.isDatafetched {return}
// let showData = vm.showData[index]
// MyListDataTemp.shareInstance.webSeriesHindi.append(FavouriteListingDM.ResultData.ShowDatum(id: showData.id, title: showData.title, description: showData.description, thumbnailPath: showData.thumbnailPath, showType: showData.showType, totalSeasons: showData.totalSeasons, totalEpisodes: showData.totalEpisodes, ageRangeMasterID: showData.ageRangeMasterID, genderMasterID: showData.genderMasterID, categoryMasterID: "18", contentMoreDetails: showData.contentMoreDetails, markAsFavourite: showData.markAsFavourite, isLiked: showData.isLiked, likesCount: showData.likesCount, viewsCount: showData.viewsCount, bookmarkCount: showData.bookmarkCount, bookmarkCategoryIDS: "18"))
// K.GVar.myListSoftReload = 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) { [unowned self] isDone in
if isDone{
vm.showData[index].isLiked = false
vm.showData[index].likesCount! -= 1
showListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none)
K.GVar.myListSoftReload = true
if let index = MyListDataTemp.shareInstance.favListingData?.showData?.english?.firstIndex(where: {$0.id == postID}){
MyListDataTemp.shareInstance.favListingData?.showData?.english?[index].isLiked = false
MyListDataTemp.shareInstance.favListingData?.showData?.english?[index].likesCount! -= 1
}
if let index = MyListDataTemp.shareInstance.favListingData?.showData?.hindi?.firstIndex(where: {$0.id == postID}){
MyListDataTemp.shareInstance.favListingData?.showData?.hindi?[index].isLiked = false
MyListDataTemp.shareInstance.favListingData?.showData?.hindi?[index].likesCount! -= 1
}
}
}
}else{
LikeFavCommonFunc.shareInstance.likePost(postID: postID, postType: postType, vc: self){ [unowned self] isDone in
if isDone{
vm.showData[index].isLiked = true
vm.showData[index].likesCount! += 1
showListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none)
// Soft reload for webseries english
K.GVar.myListSoftReload = true
if let index = MyListDataTemp.shareInstance.favListingData?.showData?.english?.firstIndex(where: {$0.id == postID}){
MyListDataTemp.shareInstance.favListingData?.showData?.english?[index].isLiked = true
MyListDataTemp.shareInstance.favListingData?.showData?.english?[index].likesCount! += 1
}
if let index = MyListDataTemp.shareInstance.favListingData?.showData?.english?.firstIndex(where: {$0.id == postID}){
MyListDataTemp.shareInstance.favListingData?.showData?.english?[index].isLiked = true
MyListDataTemp.shareInstance.favListingData?.showData?.english?[index].likesCount! += 1
}
}
}
}
// K.GVar.reloadMyList = true
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if vm.showData.count == 0 {return}
let showData = vm.showData[indexPath.row]
if let postID = showData.id, let categoryID = vm.categoryListingData[vm.dropDownModule.indexForSelectedRow ?? 0].id{
PersistentStorage.shared.addWebSeries(catID: categoryID, postID: postID, postType: .series)
}
let sb = UIStoryboard(name: K.StoryBoard.webSeries, bundle: nil)
let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.WebSeries.webSeriesSeasonVC) as! WebSeriesSeasonVC
vcPush.vm.showData = showData
vcPush.vm.indexSelected = indexPath.row
vcPush.likeFavDelegate = self
if let selectedIndex = vm.dropDownModule.indexForSelectedRow{
let categoryID = vm.categoryListingData[selectedIndex]
vcPush.vm.categoryID = categoryID.id
}
self.navigationController?.pushViewController(vcPush, animated: true)
}
}
extension WebSeriesVC : ReloadSeriesFavLike{
// if like or fav is done in webseriesseason vc, dont need to do api call in this screen.
func updateRows(index: Int, type: FavCellCLick, isFav: Bool?, isLike: Bool?, id: Int?) {
if let isFav{
let categoryID = vm.categoryListingData[vm.dropDownModule.indexForSelectedRow ?? 0].id ?? 0
switch isFav{
case true:
vm.showData[index].markAsFavourite = true
vm.showData[index].favouriteCategoryIDS = ValueWrapper.stringValue(categoryID.toString())
showListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none)
case false:
vm.showData[index].markAsFavourite = false
vm.showData[index].favouriteCategoryIDS = ValueWrapper.stringValue("")
showListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none)
}
}
if let isLike{
switch isLike{
case true:
vm.showData[index].isLiked = true
vm.showData[index].likesCount! += 1
showListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none)
case false:
vm.showData[index].isLiked = false
vm.showData[index].likesCount! -= 1
showListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none)
}
}
}
}
// MARK: - CollectionView Delegate and Data Source
extension WebSeriesVC : CollectionViewSRC{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return vm.continueWatchingData.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: K.CellIdentifier.WebSeries.webSeriesCell, for: indexPath) as! WebSeriesCell
let data = vm.continueWatchingData[indexPath.row]
cell.setData(data: data)
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let data = vm.continueWatchingData[indexPath.row]
if let postID = data.id, let categoryID = vm.categoryListingData[vm.dropDownModule.indexForSelectedRow ?? 0].id{
PersistentStorage.shared.addWebSeries(catID: categoryID, postID: postID, postType: .series)
}
let sb = UIStoryboard(name: K.StoryBoard.webSeries, bundle: nil)
let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.WebSeries.continueWatchingVC) as! ContinueWatchingVC
vcPush.modalPresentationStyle = .overCurrentContext
vcPush.modalTransitionStyle = .crossDissolve
vcPush.watchData = data
if let selectedIndex = vm.dropDownModule.indexForSelectedRow{
let categoryID = vm.categoryListingData[selectedIndex]
vcPush.categoryID = categoryID.id
}
self.present(vcPush, animated: true)
}
}
// MARK: - Collection Flow Layout
extension WebSeriesVC : UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 5
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
let inset: CGFloat = 10
return UIEdgeInsets(top: 0, left: inset, bottom: 0, right: inset)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0 // Space between cells
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let widthPerItem = collectionView.frame.width - 30 // Adjust to your desired width
return CGSize(width: widthPerItem, height: 230)
}
}
// MARK: - Animating scrollView
extension WebSeriesVC: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
// Get the current vertical offset of the scroll view
let y = scrollView.contentOffset.y
// Define the height range for the header view
let minHeaderHeight: CGFloat = 0.0 // Height at which the header becomes invisible
// let maxHeaderHeight: CGFloat = self.headerView.frame.height // Maximum height when fully visible
// Calculate the new height for the header view based on the scroll position
let newHeaderHeight: CGFloat
if y < 0 {
// When scrolling up beyond the top, ensure the header view is fully expanded
newHeaderHeight = vm.maxHeaderHeight
} else {
// Calculate the new height for the header view, ensuring it doesn't go below the minimum height
newHeaderHeight = max(minHeaderHeight, vm.maxHeaderHeight - y)
}
// Update the header view's height constraint with the new height
headerHeight.constant = newHeaderHeight
// Animate the layout changes to smoothly transition the header height
UIView.animate(withDuration: 0.3) {
self.view.layoutIfNeeded()
}
// if headerHeight.constant == maxHeaderHeight {
// UIView.transition(with: headerViewImage, duration: 0.3, options: .transitionCrossDissolve, animations: {
// self.headerViewImage.contentMode = .scaleToFill
// }, completion: nil)
// } else {
// UIView.transition(with: headerViewImage, duration: 0.3, options: .transitionCrossDissolve, animations: {
// self.headerViewImage.contentMode = .scaleAspectFill
// }, completion: nil)
// }
}
}