<commit_before>//
//  HomeViewController.swift
//  PicFeed
//
//  Created by <NAME> on 3/27/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit
import Social

let buttonAnimationDuration = 0.5

class HomeViewController: UIViewController {
    
    let filterNames = [FilterName.blackAndWhite, FilterName.comic, FilterName.cool, FilterName.invert, FilterName.vintage, FilterName.warm]
    let filterStrings = ["Black&White", "Comic", "Cool", "Invert", "Vintage", "Warm"]
    let imagePicker = UIImagePickerController()

    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var filterButtonBottomConstraint: NSLayoutConstraint!
    @IBOutlet weak var postButtonBottomConstraint: NSLayoutConstraint!
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var collectionViewHeightConstraint: NSLayoutConstraint!
    
    let collectionViewConstant = 150

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.collectionView.dataSource = self
        self.collectionView.delegate = self
        
        setupGalleryDelegate()
        Filters.originalImage = imageView.image
        

        }
    
    func setupGalleryDelegate() {
        if let tabBarController = self.tabBarController {
            guard let viewControllers = tabBarController.viewControllers else { return }
            
            guard let galleryController = viewControllers[1] as? GalleryViewController else { return }
            
            galleryController.delegate = self
        }
    }

   
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
            filterButtonBottomConstraint.constant = 8
            postButtonBottomConstraint.constant = 8
        
            UIView.animate(withDuration: buttonAnimationDuration) {
                self.view.layoutIfNeeded()
        }
    }


    @IBAction func imageTapped(_ sender: Any) {
        print("User Tapped Image!")
        
        self.presentActionSheet()
    }
    
    func presentActionSheet(){
        let actionSheetController = UIAlertController(title: "Source", message: "Please select source type", preferredStyle: .actionSheet)
        
        if cameraAvailable() == true {
            let cameraAction = UIAlertAction(title: "Camera", style: .default) { (action) in
                self.presentImagePickerWith(sourceType: .camera)
            }
            actionSheetController.addAction(cameraAction)
        }
        
        let photoAction = UIAlertAction(title: "Photo Library", style: .default) { (action) in
            self.presentImagePickerWith(sourceType: .photoLibrary)
        }
        
        let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: nil)
        
        actionSheetController.addAction(photoAction)
        actionSheetController.addAction(cancelAction)
        
        self.present(actionSheetController, animated: true, completion: nil)
    }
    
    @IBAction func postButtonPressed(_ sender: Any) {
        if let image = self.imageView.image {
            
            let newPost = Post(image: image)
            CloudKit.shared.save(post: newPost, completion: { (success) in
                
                if success {
                    print("Saved post sucessfully to CloudKit!")
                } else {
                    print("Failed to save post to CloudKit")
                }
                
            })
            UIImageWriteToSavedPhotosAlbum(image, self, nil, nil)
            
        }
        
    }
    
    @IBAction func filterButtonPressed(_ sender: Any) {
        
        guard let image = self.imageView.image else { return }
        
        self.collectionViewHeightConstraint.constant = CGFloat(collectionViewConstant)
        
        UIView.animate(withDuration: 0.5) {
            self.view.layoutIfNeeded()
        }
    }

    @IBAction func userLongPressed(_ sender: UILongPressGestureRecognizer) {
        
        if(SLComposeViewController.isAvailable(forServiceType: SLServiceTypeTwitter)) {
            guard let composeController = SLComposeViewController(forServiceType: SLServiceTypeTwitter) else { return }
            composeController.add(self.imageView.image)
            
            self.present(composeController, animated: true, completion: nil)
        }
    }

}

//MARK: UICollectionViewDataSource, UICollectionViewDelegate
extension HomeViewController : UICollectionViewDataSource, UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let filterCell = collectionView.dequeueReusableCell(withReuseIdentifier: FilterCell.identifier, for: indexPath) as! FilterCell
        
        guard let originalImage = Filters.originalImage else { return filterCell }
        
        
        guard let resizedImage = originalImage.resize(size: CGSize(width: 150, height: 150)) else { return filterCell}<commit_msg>added show/hide for filters<commit_after>//
//  HomeViewController.swift
//  PicFeed
//
//  Created by <NAME> on 3/27/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit
import Social

let buttonAnimationDuration = 0.5

class HomeViewController: UIViewController {
    
    let filterNames = [FilterName.blackAndWhite, FilterName.comic, FilterName.cool, FilterName.invert, FilterName.vintage, FilterName.warm]
    let filterStrings = ["Black&White", "Comic", "Cool", "Invert", "Vintage", "Warm"]
    let imagePicker = UIImagePickerController()

    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var filterButtonBottomConstraint: NSLayoutConstraint!
    @IBOutlet weak var postButtonBottomConstraint: NSLayoutConstraint!
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var collectionViewHeightConstraint: NSLayoutConstraint!
    
    let collectionViewConstant : CGFloat = 150

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.collectionView.dataSource = self
        self.collectionView.delegate = self
        
        setupGalleryDelegate()
        Filters.originalImage = imageView.image
        

        }
    
    func setupGalleryDelegate() {
        if let tabBarController = self.tabBarController {
            guard let viewControllers = tabBarController.viewControllers else { return }
            
            guard let galleryController = viewControllers[1] as? GalleryViewController else { return }
            
            galleryController.delegate = self
        }
    }

   
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
            filterButtonBottomConstraint.constant = 8
            postButtonBottomConstraint.constant = 8
        
            UIView.animate(withDuration: buttonAnimationDuration) {
                self.view.layoutIfNeeded()
        }
    }


    @IBAction func imageTapped(_ sender: Any) {
        print("User Tapped Image!")
        
        self.presentActionSheet()
    }
    
    func presentActionSheet(){
        let actionSheetController = UIAlertController(title: "Source", message: "Please select source type", preferredStyle: .actionSheet)
        
        if cameraAvailable() == true {
            let cameraAction = UIAlertAction(title: "Camera", style: .default) { (action) in
                self.presentImagePickerWith(sourceType: .camera)
            }
            actionSheetController.addAction(cameraAction)
        }
        
        let photoAction = UIAlertAction(title: "Photo Library", style: .default) { (action) in
            self.presentImagePickerWith(sourceType: .photoLibrary)
        }
        
        let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: nil)
        
        actionSheetController.addAction(photoAction)
        actionSheetController.addAction(cancelAction)
        
        self.present(actionSheetController, animated: true, completion: nil)
    }
    
    @IBAction func postButtonPressed(_ sender: Any) {
        if let image = self.imageView.image {
            
            let newPost = Post(image: image)
            CloudKit.shared.save(post: newPost, completion: { (success) in
                
                if success {
                    print("Saved post sucessfully to CloudKit!")
                } else {
                    print("Failed to save post to CloudKit")
                }
                
            })
            UIImageWriteToSavedPhotosAlbum(image, self, nil, nil)
            
        }
        
    }
    
    @IBAction func filterButtonPressed(_ sender: Any) {
        
        guard let image = self.imageView.image else { return }
        
        if(self.collectionViewHeightConstraint.constant < collectionViewConstant) {
            self.collectionViewHeightConstraint.constant = collectionViewConstant } else {
            collectionViewHeightConstraint.constant = 0
        }
        
        
        UIView.animate(withDuration: 0.5) {
            self.view.layoutIfNeeded()
        }
    }

    @IBAction func userLongPressed(_ sender: UILongPressGestureRecognizer) {
        
        if(SLComposeViewController.isAvailable(forServiceType: SLServiceTypeTwitter)) {
            guard let composeController = SLComposeViewController(forServiceType: SLServiceTypeTwitter) else { return }
            composeController.add(self.imageView.image)
            
            self.present(composeController, animated: true, completion: nil)
        }
    }

}

//MARK: UICollectionViewDataSource, UICollectionViewDelegate
extension HomeViewController : UICollectionViewDataSource, UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let filterCell = collectionView.dequeueReusableCell(withReuseIdentifier: FilterCell.identifier, for: indexPath) as! FilterCell
        
        guard let originalImage = Filters.originalImage else { return filterCell }
        
        
        guard let resizedImage = originalImage.resize(size: CGSize(width: 150, height: 150)) else { return filterCell}
{'fraction_non_alphanumeric': 0.07746547840564097, 'fraction_numerical': 0.004994613651943982, 'mean_word_length': 2.4085447263017357, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 9, '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': '7544428', 'n_tokens_mistral': 2670, 'n_tokens_neox': 2585, 'n_words': 594}