MyRewardsBannerOffersScrollTableViewCell.swift 4.89 KB
//
//  MyRewardsBannerOffersScrollTableViewCell.swift
//  SwiftWarplyFramework
//
//  Created by Manos Chorianopoulos on 23/5/25.
//

import UIKit

@objc public class MyRewardsBannerOffersScrollTableViewCell: UITableViewCell {
    @IBOutlet weak var tagView1: UIView!
    @IBOutlet weak var tagLabel1: UILabel!
    @IBOutlet weak var tagView2: UIView!
    @IBOutlet weak var tagLabel2: UILabel!
    @IBOutlet weak var profileImage: UIImageView!
    @IBOutlet weak var collectionView: UICollectionView!
    
    var data: SectionModel?

    public override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
        profileImage.image = UIImage(named: "profile_pic_default", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
        
        tagView1.backgroundColor = UIColor(rgb: 0x09914E)
        tagView1.layer.cornerRadius = 4.0
        tagLabel1.font = UIFont(name: "PingLCG-Regular", size: 17)
        tagLabel1.textColor = UIColor(rgb: 0xFFFFFF)
        
        tagView2.backgroundColor = UIColor(rgb: 0xFC9F25)
        tagView2.layer.cornerRadius = 4.0
        tagLabel2.font = UIFont(name: "PingLCG-Regular", size: 17)
        tagLabel2.textColor = UIColor(rgb: 0xFFFFFF)
        
        
        // Register XIBs for collection view cells
        collectionView.register(UINib(nibName: "MyRewardsBannerOfferCollectionViewCell", bundle: Bundle(for: MyEmptyClass.self)), forCellWithReuseIdentifier: "MyRewardsBannerOfferCollectionViewCell")

        // Fix background colors
        collectionView.backgroundColor = UIColor.clear
        self.backgroundColor = UIColor.clear
        self.contentView.backgroundColor = UIColor.clear
        
        // Remove content insets and gaps
        collectionView.contentInset = UIEdgeInsets.zero
        collectionView.scrollIndicatorInsets = UIEdgeInsets.zero

        if #available(iOS 11.0, *) {
            collectionView.contentInsetAdjustmentBehavior = .never
        }

        // Configure collection view layout
        if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
            layout.scrollDirection = .horizontal
            layout.minimumLineSpacing = 0
            layout.minimumInteritemSpacing = 0
            layout.sectionInset = UIEdgeInsets.zero
        }
        
        // Enable paging for smooth banner scrolling
        collectionView.isPagingEnabled = true
        collectionView.showsHorizontalScrollIndicator = false
        
        // Set delegates
        collectionView.delegate = self
        collectionView.dataSource = self
    }

    public override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    func configureCell(data: SectionModel?) {
        self.data = data
        self.collectionView.reloadData();
    }
}

extension MyRewardsBannerOffersScrollTableViewCell: UICollectionViewDataSource,  UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

    
    public func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.data?.offers.count ?? 0
    }

    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyRewardsBannerOfferCollectionViewCell", for: indexPath) as! MyRewardsBannerOfferCollectionViewCell
//        cell.configureCell(offer: self.data?.offers[indexPath.row])
        if let offer = self.data?.offers[indexPath.row] {
            cell.configureCell(data: offer)
        }
        return cell;
    }
    
    public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//    TODO: open offer
    }

    // MARK: - UICollectionViewDelegateFlowLayout
    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let screenWidth = UIScreen.main.bounds.width
        return CGSize(width: screenWidth, height: 348)
    }

    // Distance Between Item Cells
    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

    // Cell Margin
    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets.zero
    }
}