// // BlogsVC.swift // WOKA // // Created by MacBook Pro on 01/08/24. // import UIKit class BlogsVC: UIViewController { @IBOutlet weak var blogCV: UICollectionView! @IBOutlet weak var noDataStack: UIStackView! var blogData = [BlogDM.Blog]() 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() } 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) } }