CouponsViewController.swift 3.89 KB
//
//  CouponsViewController.swift
//  WarplySDKFrameworkIOS
//
//  Created by Βασιλης Σκουρας on 4/5/22.
//

import Foundation
import UIKit
import SwiftEventBus

@objc public class CouponsViewController: UIViewController {
    @IBOutlet weak var backgroundImage: UIImageView!
    @IBOutlet weak var tableView: UITableView!
    
    public var coupons:Array<swiftApi.CouponItemModel> = []

    public override func viewDidLoad() {
        super.viewDidLoad()
        
        self.hidesBottomBarWhenPushed = true
        
        getCouponsRequest()
        
        setBackButton()
        setNavigationTitle("Ενεργά κουπόνια")
        
        backgroundImage.image = UIImage(named: "coupons_scrollview_dark", in: Bundle(for: MyEmptyClass.self), compatibleWith: nil)
        
        tableView.delegate = self
        tableView.dataSource = self
        
        tableView.clipsToBounds = true
        tableView.layer.cornerRadius = 30
        tableView.layer.maskedCorners = [ .layerMinXMinYCorner] // Top left corner radius
        tableView.contentInset.top = 50
    }
    
    public override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        swiftApi().logTrackersEvent("screen", "ActiveCouponsScreen")
        
        self.coupons = swiftApi().getCouponList()
        self.tableView.reloadData()

    }
    

    // MARK: - API Functions
    func getCouponsRequest() {
        swiftApi().getCouponsAsync(getCouponsCallback)
    }
    
    func getCouponsCallback (_ couponsData: Array<swiftApi.CouponItemModel>?) -> Void {
        if (couponsData != nil) {
            let activeCouponData = swiftApi().filterActiveCoupons(couponsData ?? [])
            
            self.coupons = activeCouponData
            
            DispatchQueue.main.async {
                SwiftEventBus.post("coupons_fetched")
                self.tableView.reloadData()
            }
        } else {
            self.coupons = []
        }
    }
    
}

// MARK: - TableView
extension CouponsViewController: UITableViewDelegate, UITableViewDataSource{
    
    public func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.coupons.count
    }
    
    public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 120.0 + 8.0
//        return UITableViewAutomaticDimension
    }
    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CouponsTableViewCellId", for: indexPath) as! CouponsTableViewCell
        
        cell.configureCell(coupon: coupons[indexPath.row])
        
        return cell
    }
    
    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        // SwiftEventBus.post("couponBarcodePressed", sender: coupons[indexPath.row])
        
        // Logs
        let couponSetData: swiftApi.CouponSetItemModel? = coupons[indexPath.row].couponset_data
        print("Coupon clicked: " + (coupons[indexPath.row].coupon ?? ""))
        print("Coupon Name clicked: " + (couponSetData?.name ?? ""))
        print("Coupon Description clicked: " + (couponSetData?.short_description ?? ""))
        print("Coupon Expiration clicked: " + (coupons[indexPath.row].expiration ?? ""))
        
        swiftApi().logTrackersEvent("click", ("Coupon:" + (couponSetData?.name ?? "")))

        let storyboard = UIStoryboard(name: "Main", bundle: Bundle(for: MyEmptyClass.self))
        let vc = storyboard.instantiateViewController(withIdentifier: "CouponBarcodeViewController") as! SwiftWarplyFramework.CouponBarcodeViewController
        vc.coupon = coupons[indexPath.row]
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
}