// // MyListVC.swift // // Created by Bilal on 19/05/2024. // import UIKit class MyListVC: UIViewController { @IBOutlet weak var webSeriesCV: UICollectionView! @IBOutlet weak var webSeriesEnglishStack: UIStackView! @IBOutlet weak var webSeriesHindiCV: UICollectionView! @IBOutlet weak var webSeriesHindiStack: UIStackView! @IBOutlet weak var audioBooksCV: UICollectionView! @IBOutlet weak var audioBooksStack: UIStackView! @IBOutlet weak var karaokeCV: UICollectionView! @IBOutlet weak var karaokeStack: UIStackView! @IBOutlet weak var gamesCV: UICollectionView! @IBOutlet weak var gamesStack: UIStackView! @IBOutlet weak var noDataStack: UIStackView! @IBOutlet weak var scrollView: UIScrollView! var vm = MyListVM() override func viewDidLoad() { super.viewDidLoad() vm.vc = self vm.initView() vm.refreshControl.attributedTitle = NSAttributedString(string: "Refreshing...",attributes: [.foregroundColor: UIColor.appColor(.TextDarkBlue)!]) vm.refreshControl.tintColor = UIColor.appColor(.TextDarkBlue)! vm.refreshControl.addTarget(self, action: #selector(self.refresh(_:)), for: .valueChanged) scrollView.addSubview(vm.refreshControl) } @objc func refresh(_ sender: AnyObject) { Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { [unowned self] _ in vm.getFavouriteListing() } } override func viewWillAppear(_ animated: Bool) { } override func viewWillDisappear(_ animated: Bool) { } override func viewDidAppear(_ animated: Bool) { if K.GVar.reloadMyList{ K.GVar.reloadMyList = false vm.getFavouriteListing() } } @IBAction func sideBarBtnTapped(_ sender: UIButton) { self.sideMenuController?.revealMenu() } @IBAction func backBtntapped(_ sender: UIButton) { self.tabBarController?.selectedIndex = 0 } } // MARK: - CollectionView Delegate extension MyListVC : CollectionViewSRC{ func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { switch collectionView{ case webSeriesCV: if vm.favListingData?.showData?.count == 0 || vm.favListingData == nil{ webSeriesEnglishStack.isHidden = true }else{ webSeriesEnglishStack.isHidden = false } return vm.favListingData?.showData?.count ?? 0 case audioBooksCV: if vm.favListingData?.audioData?.count == 0 || vm.favListingData == nil{ audioBooksStack.isHidden = true }else{ audioBooksStack.isHidden = false } return vm.favListingData?.audioData?.count ?? 0 case karaokeCV: if vm.favListingData?.singKaraokeData?.count == 0 || vm.favListingData == nil{ karaokeStack.isHidden = true }else{ karaokeStack.isHidden = false } return vm.favListingData?.singKaraokeData?.count ?? 0 case gamesCV: if vm.favListingData?.gameData?.count == 0 || vm.favListingData == nil{ gamesStack.isHidden = true }else{ gamesStack.isHidden = false } return vm.favListingData?.gameData?.count ?? 0 case webSeriesHindiCV: if vm.webSeriesHindi.count == 0 || vm.favListingData == nil{ webSeriesHindiStack.isHidden = true }else{ webSeriesHindiStack.isHidden = false } return vm.webSeriesHindi.count default: return 0 } } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: K.CellIdentifier.Home.favouriteCell, for: indexPath) as! FavouriteCell switch collectionView{ case webSeriesCV: if let data = vm.favListingData?.showData?[indexPath.row]{ cell.setData(data: data) } case webSeriesHindiCV: let data = vm.webSeriesHindi[indexPath.row] cell.setData(data: data) case audioBooksCV: if let data = vm.favListingData?.audioData?[indexPath.row]{ cell.setOtherData(data: data) } case karaokeCV: if let data = vm.favListingData?.singKaraokeData?[indexPath.row]{ cell.setOtherData(data: data) } case gamesCV: if let data = vm.favListingData?.gameData?[indexPath.row]{ cell.setOtherData(data: data) } default: if let data = vm.favListingData?.showData?[indexPath.row]{ cell.setData(data: data) } } cell.btnTapped = { [self] (type) -> Void in updateFavLikes(cv: collectionView, type: type, index: indexPath.row) } return cell } func updateFavLikes(cv : UICollectionView , type : FavCellCLick, index : Int){ switch cv{ case webSeriesCV: guard let data = vm.favListingData?.showData?[index] else{return} switch type { case .favourite: guard let isFav = data.markAsFavourite ,let postID = data.id,let postType = data.contentMoreDetails?.first?.postType,let categoryID = data.bookmarkCategoryIDS else{return} if isFav{ vm.removeFavourite(postID: postID, postType: postType, categoryID: categoryID, index: index) { [unowned self] isDone in if isDone{ vm.favListingData?.showData?.remove(at: index) webSeriesCV.reloadData() } } } 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.favListingData?.showData?[index].isLiked = false vm.favListingData?.showData?[index].likesCount! -= 1 webSeriesCV.reloadItems(at: [IndexPath(row: index, section: 0)]) } } }else{ vm.likePost(postID: postID, postType: postType, index: index){ [unowned self] isDone in if isDone{ vm.favListingData?.showData?[index].isLiked = true vm.favListingData?.showData?[index].likesCount! += 1 webSeriesCV.reloadItems(at: [IndexPath(row: index, section: 0)]) } } } } case webSeriesHindiCV: let data = vm.webSeriesHindi[index] switch type { case .favourite: guard let isFav = data.markAsFavourite ,let postID = data.id,let postType = data.contentMoreDetails?.first?.postType,let categoryID = data.bookmarkCategoryIDS else{return} if isFav{ vm.removeFavourite(postID: postID, postType: postType, categoryID: categoryID, index: index) { [unowned self] isDone in if isDone{ vm.webSeriesHindi.remove(at: index) webSeriesHindiCV.reloadData() vm.checkNil() } } } 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.webSeriesHindi[index].isLiked = false vm.webSeriesHindi[index].likesCount! -= 1 webSeriesHindiCV.reloadItems(at: [IndexPath(row: index, section: 0)]) vm.checkNil() } } }else{ vm.likePost(postID: postID, postType: postType, index: index){ [unowned self] isDone in if isDone{ vm.webSeriesHindi[index].isLiked = true vm.webSeriesHindi[index].likesCount! += 1 webSeriesHindiCV.reloadItems(at: [IndexPath(row: index, section: 0)]) vm.checkNil() } } } } case audioBooksCV: guard let data = vm.favListingData?.audioData?[index] else{return} switch type { case .favourite: guard let isFav = data.markAsFavourite ,let postID = data.id,let postType = data.contentMoreDetails?.first?.postType else{return} if isFav{ vm.removeFavourite(postID: postID, postType: postType, categoryID: "", index: index) { [unowned self] isDone in if isDone{ vm.favListingData?.audioData?.remove(at: index) audioBooksCV.reloadData() vm.checkNil() } } } 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.favListingData?.audioData?[index].isLiked = false vm.favListingData?.audioData?[index].likesCount! -= 1 audioBooksCV.reloadItems(at: [IndexPath(row: index, section: 0)]) vm.checkNil() } } }else{ vm.likePost(postID: postID, postType: postType, index: index){ [unowned self] isDone in if isDone{ vm.favListingData?.audioData?[index].isLiked = true vm.favListingData?.audioData?[index].likesCount! += 1 audioBooksCV.reloadItems(at: [IndexPath(row: index, section: 0)]) vm.checkNil() } } } } case karaokeCV: guard let data = vm.favListingData?.singKaraokeData?[index] else{return} switch type { case .favourite: guard let isFav = data.markAsFavourite ,let postID = data.id,let postType = data.contentMoreDetails?.first?.postType else{return} if isFav{ vm.removeFavourite(postID: postID, postType: postType, categoryID: "", index: index) { [unowned self] isDone in if isDone{ vm.favListingData?.singKaraokeData?.remove(at: index) karaokeCV.reloadData() vm.checkNil() } } } 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.favListingData?.singKaraokeData?[index].isLiked = false vm.favListingData?.singKaraokeData?[index].likesCount! -= 1 karaokeCV.reloadItems(at: [IndexPath(row: index, section: 0)]) vm.checkNil() } } }else{ vm.likePost(postID: postID, postType: postType, index: index){ [unowned self] isDone in if isDone{ vm.favListingData?.singKaraokeData?[index].isLiked = true vm.favListingData?.singKaraokeData?[index].likesCount! += 1 karaokeCV.reloadItems(at: [IndexPath(row: index, section: 0)]) vm.checkNil() } } } } case gamesCV: guard let data = vm.favListingData?.gameData?[index] else{return} switch type { case .favourite: guard let isFav = data.markAsFavourite ,let postID = data.id,let postType = data.contentMoreDetails?.first?.postType else{return} if isFav{ vm.removeFavourite(postID: postID, postType: postType, categoryID: "", index: index) { [unowned self] isDone in if isDone{ vm.favListingData?.gameData?.remove(at: index) gamesCV.reloadData() vm.checkNil() } } } 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.favListingData?.gameData?[index].isLiked = false vm.favListingData?.gameData?[index].likesCount! -= 1 gamesCV.reloadItems(at: [IndexPath(row: index, section: 0)]) vm.checkNil() } } }else{ vm.likePost(postID: postID, postType: postType, index: index){ [unowned self] isDone in if isDone{ vm.favListingData?.gameData?[index].isLiked = true vm.favListingData?.gameData?[index].likesCount! += 1 gamesCV.reloadItems(at: [IndexPath(row: index, section: 0)]) vm.checkNil() } } } } default: break } } } // MARK: - Collection Flow Layout extension MyListVC : 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) } }