// // WebSeriesVC.swift // WOKA // // Created by MacBook Pro on 18/06/24. // import UIKit 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! 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) } // MARK: - Tap Handler @IBAction func expandLanguageTapped(_ sender: UIButton) { vm.dropDownModule.show() self.expandBtn.setImage(UIImage(named: "SupportUpArrow"), for: .normal) } } // 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()){ vm.removeFavourite(postID: postID, postType: postType, categoryID: categoryID, index: index) { [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 } } }else{ vm.addFavourite(postID: postID, postType: postType, categoryID: categoryID, index: index) { [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 } } } return } // if isFav == true && data.favouriteCategoryIDS?.intValue == 0{ // means this is fav // vm.removeFavourite(postID: postID, postType: postType, categoryID: categoryID, index: index) { [unowned self] isDone in // if isDone{ // vm.showData[index].markAsFavourite = false // showListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none) // K.GVar.reloadMyList = true // } // } // }else if isFav == true && data.favouriteCategoryIDS?.intValue == categoryID{ // vm.removeFavourite(postID: postID, postType: postType, categoryID: categoryID, index: index) { [unowned self] isDone in // if isDone{ // vm.showData[index].markAsFavourite = false // showListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none) // K.GVar.reloadMyList = true // } // } // }else{ // vm.addFavourite(postID: postID, postType: postType, categoryID: categoryID, index: index) { [unowned self] isDone in // if isDone{ // vm.showData[index].markAsFavourite = true // showListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none) // K.GVar.reloadMyList = true // } // } // } // if isFav{ // vm.removeFavourite(postID: postID, postType: postType, categoryID: categoryID, index: index) { [unowned self] isDone in // if isDone{ // vm.showData[index].markAsFavourite = false // showListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none) // K.GVar.reloadMyList = true // } // } // }else{ // vm.addFavourite(postID: postID, postType: postType, categoryID: categoryID, index: index) { [unowned self] isDone in // if isDone{ // vm.showData[index].markAsFavourite = true // showListingTableView.reloadRows(at: [IndexPath(row: index, section: 0)],with: .none) // K.GVar.reloadMyList = true // } // } // } case .liked: guard let isLiked = data.isLiked ,let postID = data.id,let postType = data.contentMoreDetails?.first?.postType else{return} if isLiked{ vm.unlikePost(postID: postID, postType: postType, index: index) { [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) } } }else{ vm.likePost(postID: postID, postType: postType, index: index){ [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) } } } } } 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 if let selectedIndex = vm.dropDownModule.indexForSelectedRow{ let categoryID = vm.categoryListingData[selectedIndex] vcPush.vm.categoryID = categoryID.id } self.navigationController?.pushViewController(vcPush, animated: true) } } // 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) { print(indexPath.row) } } // 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() } } }