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

import UIKit
import SwiftEventBus

@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

    var timerHistory: DispatchSourceTimer?
    var seconds: Int = 0
    
    public override func viewDidLoad() {
        super.viewDidLoad()
        
        self.hidesBottomBarWhenPushed = true
        
        // setup view
        setBackButton()
        setNavigationTitle("Ανάλυση")
        
        // tab
        leftButton.titleLabel?.font = UIFont(name: "PeridotPE-Bold", size: 15)
        leftButton.setTitle("Εξαργυρωμένα", for:.normal)
        leftButton.backgroundColor = . clear
        leftButton.setTitleColor(UIColor(rgb: 0x212121), for:.normal)
        
        rightButton.titleLabel?.font = UIFont(name: "PeridotPE-SBold", size: 15)
        rightButton.setTitle("Μοιρασμένα δώρα", for:.normal)
        rightButton.backgroundColor = . clear
        rightButton.setTitleColor(UIColor(rgb: 0x848484), 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)
    }

    public override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        swiftApi().logTrackersEvent("screen", "LoyaltyHistoryScreen")
        
        self.startTimer()

        self.navigationController?.hideHairline()
    }

    public override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        self.stopTimer()
    }

    // MARK: - Functions
    func startTimer() {
        print("========= GiftsCalculator Timer Started! =========")

        let queue = DispatchQueue(label: Bundle.main.bundleIdentifier! + ".history.timer")
        timerHistory = DispatchSource.makeTimerSource(queue: queue)
        timerHistory!.schedule(deadline: .now(), repeating: .seconds(1))
        timerHistory!.setEventHandler { [weak self] in
            // do whatever stuff you want on the background queue here here
            print("========= GiftsCalculator interval! =========")

            DispatchQueue.main.async {
                // update your model objects and/or UI here
                self?.seconds = (self?.seconds ?? 0) + 1

            }
        }
        timerHistory!.resume()
    }

    func stopTimer() {
        print("========= GiftsCalculator Timer Stopped! =========")
        timerHistory?.cancel()
        timerHistory = nil

        let firebaseEvent = swiftApi.LoyaltySDKFirebaseEventModel()
        firebaseEvent._eventName = "time_spent_on_loyalty_sdk"
        firebaseEvent.setParameter = ("name", "GiftsCalculator")
        firebaseEvent.setParameter = ("seconds", String(seconds))
        SwiftEventBus.post("firebase", sender: firebaseEvent)

        seconds = 0
    }
    
    ////////////////////////////////////////////////////////////////////////////////
    private func viewControllerAt(_ index:Int) -> AnalysisChildViewController?
    {
        let storyboard = UIStoryboard(name: "Main", bundle: Bundle(for: MyEmptyClass.self))
        
        if (index == 1) {
            swiftApi().logTrackersEvent("click", ("LoyaltyHistoryScreen:" + "TabShared"))
            
            let analysisVC = storyboard.instantiateViewController(withIdentifier:"SharingHistoryViewController") as! SwiftWarplyFramework.SharingHistoryViewController
            analysisVC.index = index;
            
            return analysisVC;
        }
        
        swiftApi().logTrackersEvent("click", ("LoyaltyHistoryScreen:" + "TabExpired"))
        
        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: "PeridotPE-Bold", size: 15)
                self.rightButton.titleLabel?.font = UIFont(name: "PeridotPE-SBold", size: 15)
                
                self.leftButton.setTitleColor(UIColor(rgb: 0x212121), for:.normal)
                self.rightButton.setTitleColor(UIColor(rgb: 0x848484), 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: "PeridotPE-SBold", size: 15)
                self.rightButton.titleLabel?.font = UIFont(name: "PeridotPE-Bold", size: 15)
                
                self.rightButton.setTitleColor(UIColor(rgb: 0x212121), for:.normal)
                self.leftButton.setTitleColor(UIColor(rgb: 0x848484), 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: "PeridotPE-Bold", size: 15)
                self.rightButton.titleLabel?.font = UIFont(name: "PeridotPE-SBold", size: 15)
                self.leftButton.setTitleColor(UIColor(rgb: 0x212121), for:.normal)
                self.rightButton.setTitleColor(UIColor(rgb: 0x848484), for:.normal)
                self.tabSelected = 0
                self.leftLineView.isHidden = false
                self.rightLineView.isHidden = true
                
            } else {
                self.leftButton.titleLabel?.font = UIFont(name: "PeridotPE-SBold", size: 15)
                self.rightButton.titleLabel?.font = UIFont(name: "PeridotPE-Bold", size: 15)
                self.rightButton.setTitleColor(UIColor(rgb: 0x212121), for:.normal)
                self.leftButton.setTitleColor(UIColor(rgb: 0x848484), for:.normal)
                self.tabSelected = 1
                self.leftLineView.isHidden = true
                self.rightLineView.isHidden = false
            }
        }
    }
}