SharingHistoryViewController.swift 8.55 KB
//
//  SharingHistoryViewController.swift
//  SwiftWarplyFramework
//
//  Created by Manos Chorianopoulos on 18/7/22.
//

import UIKit

class SharingHistoryViewController: AnalysisChildViewController {

    var loading: Bool = false
    var items: Array<swiftApi.SharingCouponModel> = Array()
    var allItems: Array<swiftApi.SharingCouponModel> = Array()
    var showMore = false
    
    // TODO: remove this when configuring model
    let hasMessage = false
    
    // lifecycle
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        self.tableView.delegate = self
        self.tableView.dataSource = self

        // Add shadow
        self.tableView.layer.shadowColor = UIColor(red: 0.00, green: 0.00, blue: 0.00, alpha: 0.2).cgColor
        self.tableView.layer.shadowOffset = CGSize(width: 0.0, height: 1.0)
        self.tableView.layer.shadowOpacity = 1.0
        self.tableView.layer.shadowRadius = 1.0
        
        self.refreshControl = UIRefreshControl()
        self.refreshControl?.addTarget(self, action: #selector(handleRefresh(_:)), for: .valueChanged)
        
        handleRefresh(self.refreshControl!)
    }
    
    // mvp
    @objc func load() {
    
        if (loading) {
            return;
        }
        
        showLoading()
        
        swiftApi().getSharingHistoryAsync(isMarketHistory: false, getSharingHistoryCallback: responseCallback, failureCallback: {errorCode in })
    }
    
    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.SharingCouponModel>?) -> Void {
        
        self.items = data!
        self.allItems = data!
        self.showMore = (self.items.count > 3)
        
        if (self.showMore == true) {
            self.items = Array(self.items[0...2])
        }
        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;
        }
        
        if (self.showMore == true) {
            return 4
        }
        return items.count
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//        if (indexPath.section == 0) {
//            return hasMessage ? 380.0 : (items.count > 0 ? 280 : 380)
//        }
//
//        return 140.0
        return UITableView.automaticDimension
    }

    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
            }
            
            let cell = tableView.dequeueReusableCell(withIdentifier: "AnalysisHeaderViewCell", for: indexPath) as! SwiftWarplyFramework.AnalysisHeaderViewCell
            cell.configureCell(isEmpty: items.count == 0, isMarket: false)
            return cell
        }
        
        if ((self.showMore == true) && (indexPath.row == 3)) {
            let cell = tableView.dequeueReusableCell(withIdentifier: "AnalysisMoreViewCell", for: indexPath) as! SwiftWarplyFramework.AnalysisMoreViewCell
            return cell
        }
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "AnalysisItemViewCell", for: indexPath) as! SwiftWarplyFramework.AnalysisItemViewCell
        cell.configureCell(item: items[indexPath.row])
        
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if (indexPath.section == 0) {
            return
        }
        
        if ((self.showMore == true) && (indexPath.row == 3)) {
            self.items = self.allItems
            self.showMore = false
            self.tableView.reloadData()
        }
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if (section == 0){
            return nil
            
        } else if (section == 1) {
            if (self.items.count > 0) {
                let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 50))
                
                let viewBorder = UIView(frame: CGRect(x: 10, y: 0, width: view.frame.width-20, height: view.frame.height))
                viewBorder.backgroundColor =  .white
                // viewBorder.backgroundColor =  UIColor(red: 0.90, green: 0.90, blue: 0.90, alpha: 1.00)
                
                viewBorder.layer.cornerRadius = 16.5
                viewBorder.layer.maskedCorners = [ .layerMinXMinYCorner, .layerMaxXMinYCorner] // Top left, bottom right corner radius
                
                let viewInner = UIView(frame: CGRect(x: 1, y: 1, width: viewBorder.frame.width-2, height: viewBorder.frame.height-1))
                viewInner.backgroundColor =  .white
                
                viewInner.layer.cornerRadius = 16.5
                viewInner.layer.maskedCorners = [ .layerMinXMinYCorner, .layerMaxXMinYCorner] // Top left, bottom right corner radius

                let titleLabel = UILabel(frame: CGRect(x: 20, y: 20, width: viewInner.frame.width - 40, height: 20))
                titleLabel.font = UIFont(name: "BTCosmo-Bold", size: 20)
                titleLabel.textColor = UIColor(red: 0.13, green: 0.13, blue: 0.13, alpha: 1.00)
                titleLabel.text = "Αναλυτικά"
                
                view.addSubview(viewBorder)
                viewBorder.addSubview(viewInner)
                viewInner.addSubview(titleLabel)
                return view
            } else {
                return nil
            }
            
            
        } else {
            return nil
        }
    }

    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if (section == 0) {
            return 0.0
            
        } else if (section == 1) {
            if (self.items.count > 0) {
                return 50.0
            } else {
                return 0.0
            }
            
        } else {
            return 0.0
        }
    }

    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
//        return CGFloat.leastNormalMagnitude
        if (section == 1 && self.items.count > 0) {
            return 20.0
        } else {
            return 0.0
        }
    }

    override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        if (section == 1) {
            if (self.items.count > 0) {
                let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 30))
                
                let viewBorder = UIView(frame: CGRect(x: 10, y: 0, width: view.frame.width-20, height: view.frame.height))
                viewBorder.backgroundColor =  .white
                // viewBorder.backgroundColor =  UIColor(red: 0.90, green: 0.90, blue: 0.90, alpha: 1.00)
                
                viewBorder.layer.cornerRadius = 16.5
                viewBorder.layer.maskedCorners = [ .layerMinXMaxYCorner, .layerMaxXMaxYCorner]  // bottom left, bottom right corner radius
                
                let viewInner = UIView(frame: CGRect(x: 1, y: 0, width: viewBorder.frame.width-2, height: viewBorder.frame.height-1))
                viewInner.backgroundColor =  .white
                
                viewInner.layer.cornerRadius = 16.5
                viewInner.layer.maskedCorners = [ .layerMinXMaxYCorner, .layerMaxXMaxYCorner] // bottom left, bottom right corner radius

                view.addSubview(viewBorder)
                viewBorder.addSubview(viewInner)
                return view
            } else {
                return nil
            }
            
        } else {
            return nil
        }
    }
}