NumberPopupViewController.swift 7.03 KB
//
//  NumberPopupViewController.swift
//  SwiftWarplyFramework
//
//  Created by Manos Chorianopoulos on 15/7/22.
//

import UIKit

protocol NumbersPopupDelegate {
    func numbersPopupTapped(_ sender: Any)
    func presentedPopup()
    func dismissedPopup()
    func optionSelected(_ option: String)
}


@objc public class NumberPopupViewController: UIViewController, UIPopoverPresentationControllerDelegate {
    @IBOutlet weak var popupView: UIView!
    @IBOutlet weak var headerLabel: UILabel!
    @IBOutlet weak var closeButton: UIButton!
    @IBOutlet weak var tableView: UITableView!
    
    let uiscreen: CGRect = UIScreen.main.bounds
    
    var delegateBallon: NumbersPopupDelegate?
    var numbersList: Array<String> = []
    
    var widthPopup: Int = 0
    var heightPopup: Int = 0
    
     func InitWithController(controller: UIViewController, numbersList: Array<String>, sender: Any, delegate: NumbersPopupDelegate){
        
        //print("Ok InitWithController sender ")
        
        delegateBallon = delegate
        
        // set the presentation style
        self.modalPresentationStyle = UIModalPresentationStyle.popover
        
        // set up the popover presentation controller
//        self.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.up
        self.popoverPresentationController?.delegate = self
        self.popoverPresentationController?.sourceView = (sender as! UIView ) // button
        //--- Better center of the arrow
//        let rect: CGRect =  (sender as AnyObject).bounds
//        let rectFixed: CGRect = CGRect(x: rect.origin.x, y: rect.origin.y, width: rect.size.width-10, height: rect.size.height)
//        self.popoverPresentationController?.sourceRect = rectFixed
        self.popoverPresentationController?.backgroundColor = UIColor.clear
//         self.popoverPresentationController?.backgroundColor = UIColor(red: 0.22, green: 0.32, blue: 0.40, alpha: 0.58)
        
        DispatchQueue.main.async {
            // present the popover
            controller.present(self, animated: true, completion: nil)
        }
         self.numbersList = numbersList
    }
    
    public override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    public override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
//        setupScreens()
        
        tableView.delegate = self
        tableView.dataSource = self
        
        popupView.clipsToBounds = true
        popupView.layer.cornerRadius = 12
        popupView.layer.maskedCorners = [ .layerMinXMinYCorner, .layerMaxXMinYCorner] // Top left, right corner radius
        
        self.preferredContentSize = CGSize(width: widthPopup, height: heightPopup)
        
        closeButton.setImage(UIImage(named: "ic_close.png", in: Bundle(for: MyEmptyClass.self), compatibleWith: nil), for: .normal)
        closeButton.imageView?.layer.transform = CATransform3DMakeScale(1.5, 1.5, 1.5)
        
//        self.tableView.reloadData()
        self.tableView.invalidateIntrinsicContentSize()
    }
    
    func setupScreens() {
//        if UIDevice.current.userInterfaceIdiom == .pad {
//            // Do what you want
//            widthPopup = 450
//            heightPopup = 550
//            textView.font = UIFont(name: Fonts.ProximaNova.semibold, size: 20)
//        } else if UIDevice.current.userInterfaceIdiom == .phone {
//            // Do what you want
//            widthPopup = 250
//            heightPopup = 360
//            textView.font = UIFont(name: Fonts.ProximaNova.semibold, size: 14)
//        }
    }
    
    public override func viewDidAppear(_ animated: Bool) {
        delegateBallon?.presentedPopup()
    }
    
    public override func viewDidDisappear(_ animated: Bool) {
        delegateBallon?.dismissedPopup()
    }
    
    public override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    public override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        //print("In prepare")
    }
    
    public func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
        return UIModalPresentationStyle.fullScreen
    }
    
    public func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
        return UIModalPresentationStyle.fullScreen
//        return UIModalPresentationStyle.none
    }
    
    // MARK: - UIButton Action
    @IBAction func closeButtonAction(_ sender: Any) {
        self.dismiss(animated: true, completion: {

        })
    }

}

// MARK: - TableView
extension NumberPopupViewController: UITableViewDelegate, UITableViewDataSource{
    
    public func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.numbersList.count
    }
    
    public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50 //+ 30.0
//        return UITableViewAutomaticDimension
    }
    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "NumbersTableViewCellId", for: indexPath) as! NumbersTableViewCell
        let isLast = indexPath.row == (self.numbersList.count - 1)
        
        cell.configureCell(number: numbersList[indexPath.row], isLast: isLast)
        
        return cell
    }
    
    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        delegateBallon?.optionSelected(self.numbersList[indexPath.row])
        self.dismiss(animated: true, completion: {})
        
        // 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 ?? ""))
//
//        let storyboard = UIStoryboard(name: "Main", bundle: Bundle(for: MyEmptyClass.self))
//        let vc = storyboard.instantiateViewController(withIdentifier: "CouponBarcodeViewController") as! CouponBarcodeViewController
//        vc.coupon = coupons[indexPath.row]
//        self.navigationController?.pushViewController(vc, animated: true)
        
    }
    
}

// Class for dynamic height table view
@objc public class DynamicSizeTableView: UITableView
{
    override public func layoutSubviews() {
        super.layoutSubviews()
        if bounds.size != intrinsicContentSize {
            invalidateIntrinsicContentSize()
        }
    }

    override public var intrinsicContentSize: CGSize {
        return contentSize
    }
}