Files
Woka_Native_iOS/WOKA/WebSeries/Controller/WebSeriesVC.swift
2024-08-04 12:20:12 +05:30

370 lines
18 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 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 loadMoreBtn: LocalisedElementsButton!
@IBOutlet weak var loadMoreActivityIndicator: UIActivityIndicatorView!
var vm = WebSeriesVM()
override func viewDidLoad() {
super.viewDidLoad()
vm.vc = self
scrollView.delegate = self
vm.initView()
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
}
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)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
// Customize the navigation bar's appearance
self.navigationController?.setColor(color: .black)
}
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)
}
@IBAction func expandLanguageTapped(_ sender: UIButton) {
vm.dropDownModule.show()
self.expandBtn.setImage(UIImage(named: "SupportUpArrow"), for: .normal)
}
@IBAction func loadMoreBtnTapped(_ sender: LocalisedElementsButton) {
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)
}
}
}
// 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]
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)
// K.GVar.reloadMyList = 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?.firstIndex(where: {$0.id == postID}){
MyListDataTemp.shareInstance.favListingData?.showData?.remove(at: index)
K.GVar.myListSoftReload = true
}
}else{ // hindi
if let index = MyListDataTemp.shareInstance.webSeriesHindi.firstIndex(where: {$0.id == postID}){
MyListDataTemp.shareInstance.webSeriesHindi.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)
// K.GVar.reloadMyList = 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?.firstIndex(where: {$0.id == postID}){
MyListDataTemp.shareInstance.favListingData?.showData?[index].isLiked = false
MyListDataTemp.shareInstance.favListingData?.showData?[index].likesCount! -= 1
}
if let index = MyListDataTemp.shareInstance.webSeriesHindi.firstIndex(where: {$0.id == postID}){
MyListDataTemp.shareInstance.webSeriesHindi[index].isLiked = false
MyListDataTemp.shareInstance.webSeriesHindi[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?.firstIndex(where: {$0.id == postID}){
MyListDataTemp.shareInstance.favListingData?.showData?[index].isLiked = true
MyListDataTemp.shareInstance.favListingData?.showData?[index].likesCount! += 1
}
if let index = MyListDataTemp.shareInstance.webSeriesHindi.firstIndex(where: {$0.id == postID}){
MyListDataTemp.shareInstance.webSeriesHindi[index].isLiked = true
MyListDataTemp.shareInstance.webSeriesHindi[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]
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]
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 = 200.0 // 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 = maxHeaderHeight
} else {
// Calculate the new height for the header view, ensuring it doesn't go below the minimum height
newHeaderHeight = max(minHeaderHeight, 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()
}
}
}