HistoryViewController.swift 3.03 KB
//
//  HistoryViewController.swift
//  SwiftWarplyFramework
//
//  Created by Manos Chorianopoulos on 18/7/22.
//

import UIKit

class HistoryViewController: AnalysisChildViewController {

    var loading: Bool = false
    var items: Array<swiftApi.CouponItemModel> = swiftApi().getOldCouponList()
    
    // TODO: remove this when configuring model
    let hasMessage = true
    
    // lifecycle
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        self.refreshControl = UIRefreshControl()
        self.refreshControl?.addTarget(self, action: #selector(handleRefresh(_:)), for: .valueChanged)
        
        handleRefresh(self.refreshControl!)
    }
    
    // mvp
    @objc func load() {
    
        if (loading) {
            return;
        }

        showLoading()
        
        items = swiftApi().getOldCouponList()
        showContent()
        self.tableView.reloadData()
    }
    
    private func showLoading() {
        
        loading = true
        if (self.refreshControl!.isRefreshing) {
            return;
        }
        
        self.refreshControl!.beginRefreshing()
    }
    
    private func showError() {
    }
    
    private func showContent() {
        
        loading = false
        self.refreshControl!.endRefreshing()
    }
    
    // private
    func responseCallback (_ data: Array<swiftApi.CouponItemModel>?) -> Void {
        
        self.items = data!
        showContent()
        DispatchQueue.main.async {
           self.tableView.reloadData()
       }
    }
    
    @objc func handleRefresh(_ refreshControl: UIRefreshControl) {
        
        self.perform(_: #selector(load), with: nil, afterDelay: 0.5)
    }

    // MARK: - Table view data source
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if (section == 0) {
            return 1;
        }
        
        return items.count
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if (indexPath.section == 0) {
            return hasMessage ? 380.0 : 280
        }
        
        return 140.0
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        // header
        if (indexPath.section == 0) {
            if (hasMessage) {
                return tableView.dequeueReusableCell(withIdentifier: "AnalysisHeaderMessageViewCell", for: indexPath) as! SwiftWarplyFramework.AnalysisHeaderMessageViewCell
            }
            
            return tableView.dequeueReusableCell(withIdentifier: "AnalysisHeaderViewCell", for: indexPath) as! SwiftWarplyFramework.AnalysisHeaderViewCell
        }
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "AnalysisItemViewCell", for: indexPath) as! SwiftWarplyFramework.AnalysisItemViewCell
        cell.configureCell(item: items[indexPath.row])
        
        return cell
    }
}