code
// func collectionView(_ collectionView: UICollectionView, // swiftlint:disable:this function_body_length
// cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// let product = self.product
// switch elementsOfProductCard[indexPath.item] {
// // case .productCardCell:
// // let cell = collectionView
// // .dequeueReusableCell(withReuseIdentifier: ProductCardCell.identifire,
// // for: indexPath) as! ProductCardCell // swiftlint:disable:this force_cast
// // cell.configureCell(product: product,
// onTapHeartButton: { [weak self] isFavorite in
// guard let productId = self?.product.uuidProduct else { return }
// switch isFavorite {
// case true:
// self?.viewModel.addToFavorite(product: productId)
// self?.product.isFavourite = isFavorite
// case false:
// self?.viewModel.removeFromFavorite(product: productId)
// self?.product.isFavourite = isFavorite
// }
// })
// cell.promotionLabel.textColor = Asset.Colors.white.color
// cell.promotionLabel.layer.cornerRadius = 14
// cell.promotionLabel.clipsToBounds = true
// let tapgesture = UITapGestureRecognizer(target: self, action: #selector(imageTapped))
// tapgesture.numberOfTapsRequired = 1
// cell.imageFoodView.addGestureRecognizer(tapgesture)
// cell.imageFoodView.isUserInteractionEnabled = true
// cell.layoutIfNeeded()
// return cell
// case .priceInOtherStoresCell:
// let cell = collectionView
// .dequeueReusableCell(withReuseIdentifier: PriceInOtherStoresCollectionViewCell.identifire,
// for: indexPath) as! PriceInOtherStoresCollectionViewCell // swiftlint:disable:this force_cast
// cell.configureCell(storeAllName: product.storeAllName,
// basketCache: basketCache,
// onTappedBuyButton: { [weak self] (productId: String, count: Int) in
// self?.viewModel.onTapBuyButton.send((productId, count))
// self?.popularProducts[indexPath.item].inBasket = count
// })
//
// return cell
// case .recommendedProductCell:
// guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: RecommendedProductCollectionCell.identifier, for: indexPath) as? RecommendedProductCollectionCell else { return UICollectionViewCell() }
//
// let recommendedProduct = popularProducts[indexPath.row]
// cell.configure(recommendedProduct: recommendedProduct,
// products: popularProducts,
// onTapBuyButton: { [weak self] (productId: String, count: Int) in
// self?.viewModel.onTapBuyButton.send((productId, count))
// self?.popularProducts[indexPath.row].inBasket = count
// },
// onTapHeartButton: { [weak self] isFavorite in
// self?.viewModel.updateFavorites(productId: recommendedProduct.uuidProductInStore, isFavorite: isFavorite)
// self?.popularProducts[indexPath.row].isFavourite = isFavorite
// },
// onProductScreen: { [weak self] product in
// self?.viewModel.onProductScreen?(product)
// })
//
// cell.layoutIfNeeded()
// return cell
// }
// }
extension ProductCardViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let product = popularProducts[indexPath.row]
viewModel.onProductScreen?(product)
}
}
extension ProductCardViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
switch indexPath.row {
case 0:
let height = screenWidth - 20
return CGSize(width: screenWidth, height: height)
case 1: let height: CGFloat = CGFloat(self.storeAllName.count*50) + CGFloat((self.storeAllName.count+1)*16)
return CGSize(width: screenWidth, height: height)
case 2: return CGSize(width: screenWidth, height: 269)
default:
return CGSize(width: screenWidth, height: 420)
}
}
}
extension ProductCardViewController: CacheableViewControllerProtocol {
func fetchFavoritesCache() {
viewModel.fetchFavoritesCache()
}
func fetchBasketCache() {
viewModel.fetchBasketCache()
}
func setupCacheBindings() {
viewModel.basketCache.sink(receiveValue: { [weak self] basketCache in
self?.basketCache = basketCache
self?.setupBasketCacheToProducts()
self?.timer?.invalidate()
self?.timer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false, block: { _ in
// self?.productCardView.recommendedProductView.collectionView.reloadData()
})
}).store(in: &cancellable)
viewModel.favoritesCache.sink(receiveValue: { [weak self] favoritesCache in
self?.favoritesCache = favoritesCache
self?.setupFavoritesCacheToProducts()
// self?.productCardView.recommendedProductView.collectionView.reloadData()
}).store(in: &cancellable)
}
func setupBasketCacheToProducts() {}
func setupFavoritesCacheToProducts() {
product.isFavourite = false
_ = favoritesCache.map { uuid in
if product.uuidProduct == uuid {
product.isFavourite = true
}
}
}
}