LoyaltyViewController.swift 4.37 KB
//
//  LoyaltyViewController.swift
//  SwiftWarplyFramework
//
//  Created by Manos Chorianopoulos on 7/2/24.
//

import UIKit

@objc public class LoyaltyViewController: UIViewController {
    @IBOutlet weak var headerView: UIView!
    @IBOutlet weak var closeImage: UIImageView!
    @IBOutlet weak var logoImage: UIImageView!
    @IBOutlet weak var filtersView: UIView!
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var containerView: UIView!
    
    var selectedScreen: String = "Offers"
    var filtersList: Array<String> = ["Screen", "Όλα", "Σπίτι", "Έξοδος", "Υγεία"]
    var selectedFilter: String = "Όλα"
    
    public override func viewDidLoad() {
        super.viewDidLoad()

        
        headerView.applyGradient(colours: [UIColor(rgb: 0x081748), UIColor(rgb: 0x204EAC)], gradient: GradientOrientation.horizontal, cornerRadius: 0.0)
        
        closeImage.image = UIImage(named: "close_btn_white", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
        logoImage.image = UIImage(named: "epistrofi_logo", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
        
        closeImage.isHidden = true
        
        // Add shadow
        filtersView.layer.shadowColor = UIColor(rgb: 0x000000).cgColor
        filtersView.layer.shadowOffset = CGSize(width: 0.0, height: 10.0)
        filtersView.layer.shadowOpacity = 0.05
        filtersView.layer.shadowRadius = 6.0
        
        collectionView.contentInset.left = 20
        collectionView.contentInset.right = 20
        
        loadVC(vcName: "OffersViewController");
    }
    
    func loadVC(vcName: String) {
        let storyboard = UIStoryboard(name: "Main", bundle: Bundle(for: MyEmptyClass.self))
        
        var vc: UIViewController;
        
        if (vcName == "OffersViewController") {
            vc = storyboard.instantiateViewController(withIdentifier: "OffersViewController") as! SwiftWarplyFramework.OffersViewController
        } else {
            vc = storyboard.instantiateViewController(withIdentifier: "MapViewController") as! SwiftWarplyFramework.MapViewController
        }
        
        addChild(vc)
        vc.view.translatesAutoresizingMaskIntoConstraints = false
        containerView.addSubview(vc.view)

        NSLayoutConstraint.activate([
            vc.view.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 0),
            vc.view.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: 0),
            vc.view.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 0),
            vc.view.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: 0)
        ])

        vc.didMove(toParent: self)
    }
    
}

extension LoyaltyViewController: UICollectionViewDataSource,UICollectionViewDelegate {
    public func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return filtersList.count
    }

    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if (indexPath.row == 0) {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ScreenCollectionViewCellId", for: indexPath) as! ScreenCollectionViewCell
            cell.configureCell(screen: selectedScreen)
            return cell;
            
        } else {
            let currentFilter = filtersList[indexPath.row];
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FiltersCollectionViewCellId", for: indexPath) as! FiltersCollectionViewCell
            cell.configureCell(filterName: currentFilter, isSelected: currentFilter == selectedFilter)
            return cell;
        }
    }
    
    public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if (indexPath.row == 0) {
            let offersSelected = selectedScreen == "Offers";
            selectedScreen = offersSelected ? "Map" : "Offers"
            loadVC(vcName: selectedScreen == "Offers" ? "OffersViewController" : "MapViewController");
            
        } else {
            let currentFilter = filtersList[indexPath.row];
            selectedFilter = currentFilter;
        }
        
        collectionView.reloadData();
    }
}