// // BlogsVC.swift // WOKA // // Created by MacBook Pro on 01/08/24. // import UIKit import GoogleMobileAds class BlogsVC: UIViewController { @IBOutlet weak var blogCV: UICollectionView! @IBOutlet weak var noDataStack: UIStackView! @IBOutlet weak var imageAdView: UIImageView! @IBOutlet weak var imageAdHeight: NSLayoutConstraint! var blogData = [BlogDM.Blog]() var bottomBannerView = GADBannerView() override func viewDidLoad() { super.viewDidLoad() setupCell() getBLogs() 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) self.title = "BLOGS".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue) self.view.applyGradient(colors: [color2, color1], startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint(x: 0.8, y: 0)) navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default) navigationController?.navigationBar.shadowImage = UIImage() /* First check if webSeries ad is present via slug, then check for Local Ads, if not then check google ads. */ if let adsData = AuthFunc.shareInstance.adsData, let blogAd = adsData.result?.filter({$0.slug == AdsEnum.shopListing.rawValue}).first{ // check if ads data contains LocalAD for webseries if let advertisement = blogAd.advertisement,let bannerImage = advertisement.bannerImage{ imageAdView.imageURL(bannerImage, color: .white) imageAdView.alpha = 0 let height = UIScreen.main.bounds.width * 0.192 imageAdHeight.constant = height UIView.animate(withDuration: 0.2, animations: { [weak self] in guard let self else{return} imageAdView.alpha = 1 imageAdView.isHidden = false }) imageAdView.addTapGesture { if let adID = blogAd.id{ PersistentStorage.shared.addAdsCount(adID: adID ,clicks: 1) } if let adLink = blogAd.advertisement?.adLink ,let url = URL(string: adLink), UIApplication.shared.canOpenURL(url) { UIApplication.shared.open(url) } } }else if blogAd.googleAd != nil{ /* Show google ads with dispatch queue. */ DispatchQueue.main.asyncAfter(deadline: .now() + 0.2, execute: { [weak self] in guard let self else{return} AdReusable.sharedInstance.setupBannerAd(bannerView: bottomBannerView, in: imageAdView, adUnitID: K.GoogleAdIDs.blogs, viewController: self) }) } } } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) navigationController?.setNavigationBarHidden(false, animated: animated) } override func viewDidAppear(_ animated: Bool) { 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) } func setupCell(){ blogCV.register(UINib(nibName: K.CellIdentifier.Home.blogsCell, bundle: nil), forCellWithReuseIdentifier: K.CellIdentifier.Home.blogsCell) blogCV.delegate = self blogCV.dataSource = self } @IBAction func retryBtnTapped(_ sender: UIButton) { getBLogs() } // MARK: - Get BLogs Data func getBLogs(){ Utilities.startProgressHUD() NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Home.blogs, method: .get) { [weak self](result : Result, NetworkManager.APIError>) in guard let self else{return} switch result{ case .success(let data): switch data.success{ case 0: /* Error */ Utilities.dismissProgressHUD() self.toast(msg: data.message ?? "Unrecognised error" , time: 2) self.noDataStack.isHidden = false self.blogCV.isHidden = true case 1: Utilities.dismissProgressHUD() guard let data = data.data?.blogs else{return} blogData = data blogCV.reloadData() default: break } case .failure(let error): Utilities.dismissProgressHUD() toast(msg: error.localizedDescription , time: 2) } } } } // MARK: - CollectionView Delegate extension BlogsVC : CollectionViewSRC{ func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return blogData.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: K.CellIdentifier.Home.blogsCell, for: indexPath) as! BlogsCell cell.setData(data : blogData[indexPath.row]) return cell } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { let data = blogData[indexPath.row] let sb = UIStoryboard(name: K.StoryBoard.theme, bundle: nil) let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Theme.blogDetailsVC) as! BlogDetailsVC vcPush.blogData = data vcPush.modalPresentationStyle = .overCurrentContext vcPush.modalTransitionStyle = .crossDissolve self.present(vcPush, animated: true) } } // MARK: - Collection Flow Layout extension BlogsVC : UICollectionViewDelegateFlowLayout{ func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { return 5 } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { return 5 } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { return UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5) } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let itemsPerRow: CGFloat = 2 let paddingSpace = 5 * (itemsPerRow + 1) let availableWidth = collectionView.frame.width - paddingSpace let widthPerItem = availableWidth / itemsPerRow return CGSize(width: widthPerItem, height: widthPerItem) } } // MARK: - Google Ad Banner Delegate extension BlogsVC : GADBannerViewDelegate{ func bannerViewDidReceiveAd(_ bannerView: GADBannerView) { bannerView.alpha = 0 bannerView.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1) UIView.animate(withDuration: 0.3, animations: { [weak self] in guard let self else{return} bannerView.alpha = 1 imageAdView.isHidden = false }) print("bannerViewDidReceiveAd") } func bannerView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: Error) { imageAdView.isHidden = true print("bannerView:didFailToReceiveAdWithError: \(error.localizedDescription)") } func bannerViewDidRecordImpression(_ bannerView: GADBannerView) { print("bannerViewDidRecordImpression") } func bannerViewWillPresentScreen(_ bannerView: GADBannerView) { print("bannerViewWillPresentScreen") } func bannerViewWillDismissScreen(_ bannerView: GADBannerView) { print("bannerViewWillDIsmissScreen") } func bannerViewDidDismissScreen(_ bannerView: GADBannerView) { print("bannerViewDidDismissScreen") } }