<commit_before>//
//  ArtistsController.swift
//  nmf
//
//  Created by <NAME> on 4/19/16.
//  Copyright © 2016 Nelsonville Music Festival. All rights reserved.
//

import UIKit
import SafariServices


class ArtistViewController: UIViewController {
    var artist: Artists?
    var scheduledTimes: [Schedule]?
    
    @IBOutlet weak var artistName: UILabel!
    @IBOutlet weak var artistBio: UILabel!
    
    @IBOutlet weak var artistImageView: UIImageView!
    
    @IBOutlet weak var websiteButton: UIButton!
    @IBOutlet weak var youTubeButton: UIButton!
    
    @IBOutlet weak var scheduleStackView: UIStackView!
    @IBOutlet weak var scheduleNIBView: ScheduleView!
    
//    override func viewDidLoad() {
//        super.viewDidLoad()
//    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        if let foundArtist = artist {
            self.artistName.text = foundArtist.artistName
            self.artistBio.text = foundArtist.bio
            
            if let pictureURLString = foundArtist.picture,
                let imageURL = NSURL(string: pictureURLString) {
                
                self.artistImageView.downloadFromURL(imageURL: imageURL, contentMode: .ScaleAspectFit)
            }

            if let urlString = foundArtist.URL,
                let _ = NSURL(string: urlString) {
                websiteButton.enabled = true
            } else {
                websiteButton.enabled = false
            }
            
            if let urlString = foundArtist.YouTube,
                let _ = NSURL(string: urlString) {
                youTubeButton.enabled = true
            } else {
                youTubeButton.enabled = false
            }
            
            buildScheduleStack()
        }
    }
    
    func buildScheduleStack() {
        let subviews = scheduleStackView.arrangedSubviews
        
        for view in subviews {
            scheduleStackView.removeArrangedSubview(view)
            view.removeFromSuperview()
        }
        
        guard let times = scheduledTimes,
            let filePath = NSBundle.mainBundle().pathForResource("ScheduleView", ofType: "nib"),
            let nibData = NSData(contentsOfFile: filePath) else {
            scheduleStackView.hidden = true
            
            return
        }
        
        scheduleStackView.hidden = false
        
        for item in times.sort({ $0.starttime?.compare($1.starttime ?? NSDate.distantFuture()) != .OrderedDescending }) {
            UINib(data: nibData, bundle: nil).instantiateWithOwner(self, options: nil)
            
            setupScheduleView(scheduleNIBView, scheduledTime: item)
            scheduleNIBView = nil
        }
    }
    
    func setupScheduleView(scheduleView: ScheduleView, scheduledTime: Schedule) {
        scheduleView.scheduleTime = scheduledTime
        
        scheduleView.startTime.text = scheduledTime.dateString()
        scheduleView.stage.text = scheduledTime.stage
        scheduleView.starButton.selected = scheduledTime.starred
        
        scheduleView.widthAnchor.constraintEqualToConstant(CGRectGetWidth(scheduleStackView.bounds)).active = true
        scheduleView.heightAnchor.constraintEqualToConstant(48.0).active = true
        
        scheduleStackView.addArrangedSubview(scheduleView)
        
        self.view.setNeedsLayout()
    }
    
    @IBAction func starButtonAction(sender: UIButton) {
        guard let index = scheduleStackView.subviews.indexOf({ sender.isDescendantOfView($0) }) else {
            return
        }
        
        var scheduleItem: Schedule? = nil
        
        if let scheduledTimes = scheduledTimes where scheduledTimes.count > index {
            scheduleItem = scheduledTimes[index]
        }
        
        if let foundScheduleItem = scheduleItem {
            sender.selected = !sender.selected
            foundScheduleItem.starred = sender.selected
        }
    }
    
    @IBAction func websiteButtonTapped(sender: UIButton) {
        if let urlString = artist?.URL,
            let websiteURL = NSURL(string: urlString) {
            let safariVC = SFSafariViewController(URL: websiteURL)
            safariVC.delegate = self
            presentViewController(safariVC, animated: true, completion: nil)
        }
    }

    @IBAction func youtubeButtonTapped(sender: UIButton) {
        if let urlString = artist?.YouTube,
            let youTubeURL = NSURL(string: urlString) where
            UIApplication.sharedApplication().canOpenURL(youTubeURL) {
            UIApplication.sharedApplication().openURL(youTubeURL)
        }
    }

    
//    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
//        switch segue.identifier! {
//        case "presentMapViewController":
//            guard let navigationController = segue.destinationViewController as? UINavigationController,
//                let mapViewController = navigationController.topViewController as? MapViewController else {
//                    fatalError("Unexpected view hierarchy")
//            }
//            mapViewController.locationToShow = vacationSpot.coordinate
//            mapViewController.title = vacationSpot.name
//        case "presentRatingViewController":
//            guard let navigationController = segue.destinationViewController as? UINavigationController,
//                let ratingViewController = navigationController.topViewController as? RatingViewController else {
//                    fatalError("Unexpected view hierarchy")
//            }
//            ratingViewController.vacationSpot = vacationSpot
//        default:
//            fatalError("Unhandled Segue: \(segue.identifier!)")
//        }
//    }
}

// MARK: - SFSafariViewControllerDelegate

extension ArtistViewController: SFSafariViewControllerDelegate {
    func safariViewControllerDidFinish(controller: SFSafariViewController) {
        controller.dismissViewControllerAnimated(true, completion: nil)
    }
}

extension UIImageView {
    func downloadFromURL(imageURL url: NSURL, contentMode mode: UIViewContentMode) {
        contentMode = mode

        NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: { (data, response, error) -> Void in
            guard let httpURLResponse = response as? NSHTTPURLResponse where httpURLResponse.statusCode == 200,
                let mimeType = response?.MIMEType where mimeType.hasPrefix("image"),
                let data = data where error == nil,
                let image = UIImage(data: data) else {
                    return
            }
            
            dispatch_async(dispatch_get_main_queue()) { () -> Void in
                self.image = image
            }
            
        }).resume()
    }
}
<commit_msg>Adding kingfisher for async image caching<commit_after>//
//  ArtistsController.swift
//  nmf
//
//  Created by <NAME> on 4/19/16.
//  Copyright © 2016 Nelsonville Music Festival. All rights reserved.
//

import UIKit
import SafariServices
import Kingfisher


class ArtistViewController: UIViewController {
    var artist: Artists?
    var scheduledTimes: [Schedule]?
    
    @IBOutlet weak var artistName: UILabel!
    @IBOutlet weak var artistBio: UILabel!
    
    @IBOutlet weak var artistImageView: UIImageView!
    
    @IBOutlet weak var websiteButton: UIButton!
    @IBOutlet weak var youTubeButton: UIButton!
    
    @IBOutlet weak var scheduleStackView: UIStackView!
    @IBOutlet weak var scheduleNIBView: ScheduleView!
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        if let foundArtist = artist {
            self.artistName.text = foundArtist.artistName
            self.artistBio.text = foundArtist.bio
            
            if let pictureURLString = foundArtist.picture,
                let imageURL = NSURL(string: pictureURLString) {
                
                self.artistImageView.kf_setImageWithURL(imageURL)
            }

            if let urlString = foundArtist.URL,
                let _ = NSURL(string: urlString) {
                websiteButton.enabled = true
            } else {
                websiteButton.enabled = false
            }
            
            if let urlString = foundArtist.YouTube,
                let _ = NSURL(string: urlString) {
                youTubeButton.enabled = true
            } else {
                youTubeButton.enabled = false
            }
            
            buildScheduleStack()
        }
    }
    
    func buildScheduleStack() {
        let subviews = scheduleStackView.arrangedSubviews
        
        for view in subviews {
            scheduleStackView.removeArrangedSubview(view)
            view.removeFromSuperview()
        }
        
        guard let times = scheduledTimes,
            let filePath = NSBundle.mainBundle().pathForResource("ScheduleView", ofType: "nib"),
            let nibData = NSData(contentsOfFile: filePath) else {
            scheduleStackView.hidden = true
            
            return
        }
        
        scheduleStackView.hidden = false
        
        for item in times.sort({ $0.starttime?.compare($1.starttime ?? NSDate.distantFuture()) != .OrderedDescending }) {
            UINib(data: nibData, bundle: nil).instantiateWithOwner(self, options: nil)
            
            setupScheduleView(scheduleNIBView, scheduledTime: item)
            scheduleNIBView = nil
        }
    }
    
    func setupScheduleView(scheduleView: ScheduleView, scheduledTime: Schedule) {
        scheduleView.scheduleTime = scheduledTime
        
        scheduleView.startTime.text = scheduledTime.dateString()
        scheduleView.stage.text = scheduledTime.stage
        scheduleView.starButton.selected = scheduledTime.starred
        
        scheduleView.widthAnchor.constraintEqualToConstant(CGRectGetWidth(scheduleStackView.bounds)).active = true
        scheduleView.heightAnchor.constraintEqualToConstant(48.0).active = true
        
        scheduleStackView.addArrangedSubview(scheduleView)
        
        self.view.setNeedsLayout()
    }
    
    @IBAction func starButtonAction(sender: UIButton) {
        guard let index = scheduleStackView.subviews.indexOf({ sender.isDescendantOfView($0) }) else {
            return
        }
        
        var scheduleItem: Schedule? = nil
        
        if let scheduledTimes = scheduledTimes where scheduledTimes.count > index {
            scheduleItem = scheduledTimes[index]
        }
        
        if let foundScheduleItem = scheduleItem {
            sender.selected = !sender.selected
            foundScheduleItem.starred = sender.selected
        }
    }
    
    @IBAction func websiteButtonTapped(sender: UIButton) {
        if let urlString = artist?.URL,
            let websiteURL = NSURL(string: urlString) {
            let safariVC = SFSafariViewController(URL: websiteURL)
            safariVC.delegate = self
            presentViewController(safariVC, animated: true, completion: nil)
        }
    }

    @IBAction func youtubeButtonTapped(sender: UIButton) {
        if let urlString = artist?.YouTube,
            let youTubeURL = NSURL(string: urlString) where
            UIApplication.sharedApplication().canOpenURL(youTubeURL) {
            UIApplication.sharedApplication().openURL(youTubeURL)
        }
    }

    
//    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
//        switch segue.identifier! {
//        case "presentMapViewController":
//            guard let navigationController = segue.destinationViewController as? UINavigationController,
//                let mapViewController = navigationController.topViewController as? MapViewController else {
//                    fatalError("Unexpected view hierarchy")
//            }
//            mapViewController.locationToShow = vacationSpot.coordinate
//            mapViewController.title = vacationSpot.name
//        case "presentRatingViewController":
//            guard let navigationController = segue.destinationViewController as? UINavigationController,
//                let ratingViewController = navigationController.topViewController as? RatingViewController else {
//                    fatalError("Unexpected view hierarchy")
//            }
//            ratingViewController.vacationSpot = vacationSpot
//        default:
//            fatalError("Unhandled Segue: \(segue.identifier!)")
//        }
//    }
}

// MARK: - SFSafariViewControllerDelegate

extension ArtistViewController: SFSafariViewControllerDelegate {
    func safariViewControllerDidFinish(controller: SFSafariViewController) {
        controller.dismissViewControllerAnimated(true, completion: nil)
    }
}

{'fraction_non_alphanumeric': 0.07820573038985439, 'fraction_numerical': 0.0025833724753405356, 'mean_word_length': 2.1411359724612735, 'pattern_counts': {'":': 4, '<': 5, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5914317', 'n_tokens_mistral': 3320, 'n_tokens_neox': 3175, 'n_words': 688}