LoyaltyAnalysisViewController.swift 7.34 KB
//
//  LoyaltyAnalysisViewController.swift
//  SwiftWarplyFramework
//
//  Created by Manos Chorianopoulos on 18/7/22.
//

import UIKit

@objc public class LoyaltyAnalysisViewController: UIViewController {
    
    @IBOutlet weak var leftButton: UIButton!
    @IBOutlet weak var rightButton: UIButton!
    @IBOutlet weak var leftLineView: UIView!
    @IBOutlet weak var rightLineView: UIView!
    @IBOutlet weak var contentView: UIView!
    var pageController: UIPageViewController!
    var tabSelected = 0
    
    public override func viewDidLoad() {
        super.viewDidLoad()
        
        self.hidesBottomBarWhenPushed = true
        
        // setup view
        setBackButton()
        setNavigationTitle("Ανάλυση")
        
        // tab
        leftButton.titleLabel?.font = UIFont(name: "PFSquareSansPro-Bold", size: 14)
        leftButton.setTitle("Εξαργυρωμένα", for:.normal)
        leftButton.backgroundColor = . clear
        leftButton.setTitleColor(UIColor(rgb: 0x2EAFB9), for:.normal)
        
        rightButton.titleLabel?.font = UIFont(name: "PFSquareSansPro-Regular", size: 14)
        rightButton.setTitle("Μοιρασμένα δώρα", for:.normal)
        rightButton.backgroundColor = . clear
        rightButton.setTitleColor(UIColor(rgb: 0x394A5B), for:.normal)
        
        leftLineView.applyGradient(colours: [UIColor(rgb: 0x1DA6B9), UIColor(rgb: 0xB2CE69)], gradient: GradientOrientation.horizontal, cornerRadius: 0.0)
        rightLineView.applyGradient(colours: [UIColor(rgb: 0x1DA6B9), UIColor(rgb: 0xB2CE69)], gradient: GradientOrientation.horizontal, cornerRadius: 0.0)
        
        leftLineView.isHidden = false
        rightLineView.isHidden = true
        
        // pages
        pageController = UIPageViewController(transitionStyle:.scroll, navigationOrientation:.horizontal)
        pageController.dataSource = self;
        pageController.delegate = self;
        
        pageController.view.frame = contentView.bounds;
        addChild(pageController)
        contentView.addSubview(pageController.view)
        pageController .didMove(toParent: self)
        
        let analysisVC = self.viewControllerAt(0)
        pageController.setViewControllers([analysisVC!], direction:.forward, animated:false)
    }
    
    ////////////////////////////////////////////////////////////////////////////////
    private func viewControllerAt(_ index:Int) -> AnalysisChildViewController?
    {
        let storyboard = UIStoryboard(name: "Main", bundle: Bundle(for: MyEmptyClass.self))
        
        if (index == 1) {
            let analysisVC = storyboard.instantiateViewController(withIdentifier:"SharingHistoryViewController") as! SwiftWarplyFramework.SharingHistoryViewController
            analysisVC.index = index;
            
            return analysisVC;
        }

        let analysisVC = storyboard.instantiateViewController(withIdentifier:"HistoryViewController") as! SwiftWarplyFramework.HistoryViewController
        analysisVC.index = index;

        return analysisVC;
    }
    
    // MARK: - Handlers
    @IBAction func handleLeft() {
        if (tabSelected != 0) {
            self.tabSelected = 0
            
            UIView.animate(withDuration: 2.0, delay: 0.0) {
               
                self.leftLineView.isHidden = false
                self.rightLineView.isHidden = true
                
                self.leftButton.titleLabel?.font = UIFont(name: "PFSquareSansPro-Bold", size: 14)
                self.rightButton.titleLabel?.font = UIFont(name: "PFSquareSansPro-Regular", size: 14)
                
                self.leftButton.setTitleColor(UIColor(rgb: 0x2EAFB9), for:.normal)
                self.rightButton.setTitleColor(UIColor(rgb: 0x394A5B), for:.normal)
            }
            let analysisVC = self.viewControllerAt(0)
            pageController.setViewControllers([analysisVC!], direction:.reverse, animated:true)
        }
    }
    
    @IBAction func handleRight() {
        if (tabSelected != 1) {
            self.tabSelected = 1
            
            UIView.animate(withDuration: 2.0, delay: 0.0) {
                
                self.leftLineView.isHidden = true
                self.rightLineView.isHidden = false
                
                self.leftButton.titleLabel?.font = UIFont(name: "PFSquareSansPro-Regular", size: 14)
                self.rightButton.titleLabel?.font = UIFont(name: "PFSquareSansPro-Bold", size: 14)
                
                self.rightButton.setTitleColor(UIColor(rgb: 0x2EAFB9), for:.normal)
                self.leftButton.setTitleColor(UIColor(rgb: 0x394A5B), for:.normal)
            }
            let analysisVC = self.viewControllerAt(1)
            pageController.setViewControllers([analysisVC!], direction:.forward, animated:true)
        }
    }
}

// MARK: - PageViewController
extension LoyaltyAnalysisViewController: UIPageViewControllerDataSource, UIPageViewControllerDelegate {
    
    public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        
        if let analysisVC = viewController as? AnalysisChildViewController {
            var index = analysisVC.index
            if (index == 0) {
                return nil;
            }
            
            index -= 1;
            return viewControllerAt(index);
        }
        
        return nil;
    }
    
    public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        
        if let analysisVC = viewController as? AnalysisChildViewController {
            var index = analysisVC.index
            if (index == 1) {
                return nil;
            }
            
            index += 1;
            return viewControllerAt(index);
        }
        
        return nil;
    }
    
    
    public func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        
        if (!completed) {
            return;
        }
        
        
        if let childVCs = pageViewController.viewControllers as? [AnalysisChildViewController] {
            let currentIndex = childVCs[0].index
            if (currentIndex == 0) {
                self.leftButton.titleLabel?.font = UIFont(name: "PFSquareSansPro-Bold", size: 14)
                self.rightButton.titleLabel?.font = UIFont(name: "PFSquareSansPro-Regular", size: 14)
                self.leftButton.setTitleColor(UIColor(rgb: 0x2EAFB9), for:.normal)
                self.rightButton.setTitleColor(UIColor(rgb: 0x394A5B), for:.normal)
                self.tabSelected = 0
                self.leftLineView.isHidden = false
                self.rightLineView.isHidden = true
                
            } else {
                self.leftButton.titleLabel?.font = UIFont(name: "PFSquareSansPro-Regular", size: 14)
                self.rightButton.titleLabel?.font = UIFont(name: "PFSquareSansPro-Bold", size: 14)
                self.rightButton.setTitleColor(UIColor(rgb: 0x2EAFB9), for:.normal)
                self.leftButton.setTitleColor(UIColor(rgb: 0x394A5B), for:.normal)
                self.tabSelected = 1
                self.leftLineView.isHidden = true
                self.rightLineView.isHidden = false
            }
        }
    }
}