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

import Foundation
import UIKit

@objc public class MFYViewController: UIViewController {
    @IBOutlet weak var mainView: UIView!
    @IBOutlet weak var tableView: UITableView!
    
    public var campaigns:Array<swiftApi.CampaignItemModel> = []
    
    
    public override func viewDidLoad() {
        super.viewDidLoad()
        
        getCampaignsRequest()

        setBackButton()
        setNavigationTitle("MORE for YOU")
        
        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
        
        // Add Top left corner radius
        mainView.clipsToBounds = true
        mainView.layer.cornerRadius = 30
        mainView.layer.maskedCorners = [ .layerMinXMinYCorner] // Top left corner radius
        mainView.backgroundColor = UIColor(red: 0.22, green: 0.32, blue: 0.40, alpha: 1.00)
        
    }
    
    
    func getCampaignsRequest() {
        swiftApi().getCampaignsAsync(getCampaignsCallback)
    }
    
    func getCampaignsCallback (_ campaignsData: Array<swiftApi.CampaignItemModel>?) -> Void {
        if (campaignsData != nil) {
            
            self.campaigns = campaignsData?.filter { $0.offer_category == "more_for_you" } ?? []
            
            DispatchQueue.main.async {
                self.tableView.reloadData()
                swiftApi().setUniqueCampaignList(campaignsData ?? [])
            }
        } else {
            self.campaigns = []
        }
    }
}

// MARK: - TableView
extension MFYViewController: UITableViewDelegate, UITableViewDataSource{
    
    public func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.campaigns.count
    }
    
    public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 220.0 + 10.0
//        return UITableViewAutomaticDimension
    }
    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MFYInboxTableViewCellId", for: indexPath) as! MFYInboxTableViewCell
        
        cell.configureCell(campaign: campaigns[indexPath.row])
        
        return cell
    }
    
    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
       let storyboard = UIStoryboard(name: "Main", bundle: Bundle(for: MyEmptyClass.self))
       let vc = storyboard.instantiateViewController(withIdentifier: "CampaignViewController") as! SwiftWarplyFramework.CampaignViewController
       let url = swiftApi().constructCampaignUrl(campaigns[indexPath.row])
       vc.campaignUrl = url
       self.navigationController?.pushViewController(vc, animated: true)
    }
    
}