<commit_before>//
//  ViewController.swift
//  audible
//
//  Created by <NAME> on 11/23/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import UIKit

protocol OnboardingDelegate: class {
    func handleFinishedOnboarding()
}

class OnboardingController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    
    weak var delegate: OnboardingDelegate?
    
    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal //makes cells swipe horizontally
        layout.minimumLineSpacing = 0 //decreases gap between cells
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.showsHorizontalScrollIndicator = false
        cv.backgroundColor = .white
        cv.dataSource = self
        cv.delegate = self
        cv.isPagingEnabled = true //makes the cells snap (paging behavior)
        return cv
    }()
        
    let cellId = "cellId"
    let selectionCellId = "selectionCell"
    
    let pages: [Page] = {
        var firstPage: Page = Page(title: "Laundry is back!", message: "And it's better than ever. Now you can see what machines are open. Anytime, anywhere.", imageName: "Onboard 1", showConfetti: true, isFullScreen: true)
        
        let secondPage = Page(title: "Take the pain out of doing laundry", message: "Check real time usage data without having to leave your room. Swipe to see all machines.", imageName: "Onboard 2", showConfetti: false, isFullScreen: false)
        
        let thirdPage = Page(title: "Choose up to three rooms", message: "We'll remember them! You can always edit your selection later.", imageName: "Onboard 3", showConfetti: false, isFullScreen: true)
        
       return [firstPage, secondPage, thirdPage]
    }() //creates empty array (don't have to use optionals)
    
    lazy var pageControl: UIPageControl = {
        let pc = UIPageControl()
        pc.pageIndicatorTintColor = UIColor.lightGray
        pc.currentPageIndicatorTintColor = UIColor.navRed
        pc.numberOfPages = self.pages.count + 1
        return pc
    }()
    
    lazy var skipButton: UIButton = {
        let button = UIButton(type: .system)
        button.setTitle("Skip", for: .normal)
        button.setTitleColor(UIColor.navRed, for: .normal)
        button.addTarget(self, action: #selector(handleSkip), for: .touchUpInside)
        return button
    }()
    
    lazy var nextButton: UIButton = {
        let button = UIButton(type: .system)
        button.setTitle("Next", for: .normal)
        button.setTitleColor(UIColor.navRed, for: .normal)
        button.addTarget(self, action: #selector(handleNextPage), for: .touchUpInside)
        return button
    }()
    
    func handleNextPage() {
        if pageControl.currentPage == pages.count {
            return
        }
        
        let indexPath = IndexPath(item: pageControl.currentPage + 1, section: 0)
        collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
        pageControl.currentPage += 1
        
        moveOffscreen(pageNumber: pageControl.currentPage)
    }
    
    func handleSkip() {
        terminateOnboarding()
        
        let category = GoogleAnalyticsManager.events.category.onboarding
        let action = GoogleAnalyticsManager.events.action.savedSelection
        GoogleAnalyticsManager.shared.trackEvent(category: category, action: action, label: "No", value: -1)
    }
    
    func terminateOnboarding() {
        delegate?.handleFinishedOnboarding()
        dismiss(animated: true, completion: nil)
    }
    
    var pageControlBottomAnchor: NSLayoutConstraint?
    var skipButtonTopAnchor: NSLayoutConstraint?
    var nextButtonTopAnchor: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationController?.isNavigationBarHidden = true
            
        view.addSubview(collectionView)
        view.addSubview(pageControl)
        view.addSubview(skipButton)
        view.addSubview(nextButton)
        
        collectionView.anchorToTop(view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor)
        
        pageControlBottomAnchor = pageControl.anchor(nil, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 40)[1] //full width, dots center to middle //[1] -> accesses second anchor (bottom anchor)
        
        skipButtonTopAnchor = skipButton.anchor(view.topAnchor, left: view.leftAnchor, bottom: nil, right: nil, topConstant: 16, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 60, heightConstant: 50).first
        
        nextButtonTopAnchor = nextButton.anchor(view.topAnchor, left: nil, bottom: nil, right: view.rightAnchor, topConstant: 16, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 60, heightConstant: 50).first
        
        registerCells()
    }
    
    fileprivate func registerCells() {
        collectionView.register(PageCell.self, forCellWithReuseIdentifier: cellId)
        collectionView.register(SelectionCell.self, forCellWithReuseIdentifier: selectionCellId)
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        view.endEditing(true) //whenever you scroll collection view, keyboard goes away
    }
    
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        
        let pageNumber = Int(targetContentOffset.pointee.x / view.frame.width) //tells you x-value of collection view
        pageControl.currentPage = pageNumber //change the dots
        
        //print(pageNumber) //use x-value to determine which page you are on
        moveOffscreen(pageNumber: pageNumber)
    }
    
    fileprivate func moveOffscreen(pageNumber: Int) {<commit_msg>added screen tracking to onboarding<commit_after>//
//  ViewController.swift
//  audible
//
//  Created by <NAME> on 11/23/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import UIKit

protocol OnboardingDelegate: class {
    func handleFinishedOnboarding()
}

class OnboardingController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, Trackable {
    
    weak var delegate: OnboardingDelegate?
    
    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal //makes cells swipe horizontally
        layout.minimumLineSpacing = 0 //decreases gap between cells
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.showsHorizontalScrollIndicator = false
        cv.backgroundColor = .white
        cv.dataSource = self
        cv.delegate = self
        cv.isPagingEnabled = true //makes the cells snap (paging behavior)
        return cv
    }()
        
    let cellId = "cellId"
    let selectionCellId = "selectionCell"
    
    let pages: [Page] = {
        var firstPage: Page = Page(title: "Laundry is back!", message: "And it's better than ever. Now you can see what machines are open. Anytime, anywhere.", imageName: "Onboard 1", showConfetti: true, isFullScreen: true)
        
        let secondPage = Page(title: "Take the pain out of doing laundry", message: "Check real time usage data without having to leave your room. Swipe to see all machines.", imageName: "Onboard 2", showConfetti: false, isFullScreen: false)
        
        let thirdPage = Page(title: "Choose up to three rooms", message: "We'll remember them! You can always edit your selection later.", imageName: "Onboard 3", showConfetti: false, isFullScreen: true)
        
       return [firstPage, secondPage, thirdPage]
    }() //creates empty array (don't have to use optionals)
    
    lazy var pageControl: UIPageControl = {
        let pc = UIPageControl()
        pc.pageIndicatorTintColor = UIColor.lightGray
        pc.currentPageIndicatorTintColor = UIColor.navRed
        pc.numberOfPages = self.pages.count + 1
        return pc
    }()
    
    lazy var skipButton: UIButton = {
        let button = UIButton(type: .system)
        button.setTitle("Skip", for: .normal)
        button.setTitleColor(UIColor.navRed, for: .normal)
        button.addTarget(self, action: #selector(handleSkip), for: .touchUpInside)
        return button
    }()
    
    lazy var nextButton: UIButton = {
        let button = UIButton(type: .system)
        button.setTitle("Next", for: .normal)
        button.setTitleColor(UIColor.navRed, for: .normal)
        button.addTarget(self, action: #selector(handleNextPage), for: .touchUpInside)
        return button
    }()
    
    func handleNextPage() {
        if pageControl.currentPage == pages.count {
            return
        }
        
        let indexPath = IndexPath(item: pageControl.currentPage + 1, section: 0)
        collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
        pageControl.currentPage += 1
        
        moveOffscreen(pageNumber: pageControl.currentPage)
    }
    
    func handleSkip() {
        terminateOnboarding()
        
        let category = GoogleAnalyticsManager.events.category.onboarding
        let action = GoogleAnalyticsManager.events.action.savedSelection
        GoogleAnalyticsManager.shared.trackEvent(category: category, action: action, label: "No", value: -1)
    }
    
    func terminateOnboarding() {
        delegate?.handleFinishedOnboarding()
        dismiss(animated: true, completion: nil)
    }
    
    var pageControlBottomAnchor: NSLayoutConstraint?
    var skipButtonTopAnchor: NSLayoutConstraint?
    var nextButtonTopAnchor: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationController?.isNavigationBarHidden = true
            
        view.addSubview(collectionView)
        view.addSubview(pageControl)
        view.addSubview(skipButton)
        view.addSubview(nextButton)
        
        collectionView.anchorToTop(view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor)
        
        pageControlBottomAnchor = pageControl.anchor(nil, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 40)[1] //full width, dots center to middle //[1] -> accesses second anchor (bottom anchor)
        
        skipButtonTopAnchor = skipButton.anchor(view.topAnchor, left: view.leftAnchor, bottom: nil, right: nil, topConstant: 16, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 60, heightConstant: 50).first
        
        nextButtonTopAnchor = nextButton.anchor(view.topAnchor, left: nil, bottom: nil, right: view.rightAnchor, topConstant: 16, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 60, heightConstant: 50).first
        
        registerCells()
        
        trackScreen("Onboarding")
    }
    
    fileprivate func registerCells() {
        collectionView.register(PageCell.self, forCellWithReuseIdentifier: cellId)
        collectionView.register(SelectionCell.self, forCellWithReuseIdentifier: selectionCellId)
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        view.endEditing(true) //whenever you scroll collection view, keyboard goes away
    }
    
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        
        let pageNumber = Int(targetContentOffset.pointee.x / view.frame.width) //tells you x-value of collection view
        pageControl.currentPage = pageNumber //change the dots
        
        //print(pageNumber) //use x-value to determine which page you are on
        moveOffscreen(pageNumber: pageNumber)
    }
    
    fileprivate func moveOffscreen(pageNumber: Int) {
{'fraction_non_alphanumeric': 0.08862738643844635, 'fraction_numerical': 0.007570770243581304, 'mean_word_length': 3.0348605577689245, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '127324', 'n_tokens_mistral': 3419, 'n_tokens_neox': 3310, 'n_words': 910}