<commit_before>//
//  PlayerTableViewController.swift
//  IceFishing
//
//  Created by <NAME> on 11/1/15.
//  Copyright © 2015 CUAppDev. All rights reserved.
//

import UIKit
import MediaPlayer

class PlayerTableViewController: UITableViewController, UISearchResultsUpdating, UISearchControllerDelegate, UISearchBarDelegate {
	// For pinning
	let topPinViewContainer = UIView()
	let bottomPinViewContainer = UIView()
	let pinView = NSBundle.mainBundle().loadNibNamed("FeedTableViewCell", owner: nil, options: nil)[0] as! FeedTableViewCell
	var pinViewGestureRecognizer: UITapGestureRecognizer!
	
	var searchController: UISearchController!
	var posts: [Post] = []
	var filteredPosts: [Post] = []
    var currentlyPlayingPost: Post?
	
    var currentlyPlayingIndexPath: NSIndexPath? {
        didSet {
			var array = posts
			if searchController.active {
				array = filteredPosts
			}
            if let row = currentlyPlayingIndexPath?.row where currentlyPlayingPost?.isEqual(array[row]) ?? false {
                currentlyPlayingPost?.player.togglePlaying()
            } else {
                currentlyPlayingPost?.player.pause(true)
                currentlyPlayingPost?.player.progress = 1.0 // Fill cell as played
                
                if let currentlyPlayingIndexPath = currentlyPlayingIndexPath {
                    currentlyPlayingPost = array[currentlyPlayingIndexPath.row]
                    currentlyPlayingPost!.player.play(true)
                } else {
                    currentlyPlayingPost = nil
                }
            }
            tableView.selectRowAtIndexPath(currentlyPlayingIndexPath, animated: false, scrollPosition: .None)
        }
    }

	var savedSongAlertView: SavedSongView!
	
    override func viewDidLoad() {
        super.viewDidLoad()
		
		//Search Bar
		searchController = UISearchController(searchResultsController: nil)
		searchController.dimsBackgroundDuringPresentation = false
		searchController.delegate = self
		searchController.searchResultsUpdater = self
		searchController.searchBar.sizeToFit()
		searchController.searchBar.delegate = self
		let textFieldInsideSearchBar = searchController.searchBar.valueForKey("searchField") as? UITextField
		textFieldInsideSearchBar?.textColor = UIColor.whiteColor()
		
		extendedLayoutIncludesOpaqueBars = true
		definesPresentationContext = true
		
		tableView.tableHeaderView = searchController.searchBar
		tableView.backgroundView = UIView() // Fix color above search bar
		
		setupPinViews()
    }
	
	override func viewDidAppear(animated: Bool) {
		super.viewDidAppear(animated)
		
		positionPinViews()
		addRevealGesture()
	}
	
	override func viewDidDisappear(animated: Bool) {
		super.viewDidDisappear(animated)
		
		removeRevealGesture()
	}
	
    // MARK: - Table view data source
	
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
		if posts.count > 0 && !searchController.active {
			self.tableView.backgroundView = nil
			return 1
		} else {
			let emptyView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height))
			
			let label = UILabel(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height))
			label.text = "No posts in the last 24 hours"
			label.textColor = UIColor.whiteColor()
			label.textAlignment = .Center
			label.numberOfLines = 2
			label.font = UIFont(name: "AvenirNext-Regular", size: 16)
			emptyView.addSubview(label)
			
			let button = UIButton(frame: CGRect(x: 0, y: 0, width: 190, height: 35))
			button.center = self.view.center
			button.center.y += 35
			button.backgroundColor = UIColor.iceDarkRed
			button.setTitle("Follow more friends", forState: .Normal)
			button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
			button.titleLabel?.font = UIFont(name: "AvenirNext-Regular", size: 16)
			button.layer.cornerRadius = 5.0
			button.addTarget(self, action: "navigateToSuggestions", forControlEvents: .TouchUpInside)
			
			emptyView.addSubview(button)
			
			self.tableView.backgroundView = emptyView
		}
		
        return 0
    }
    
	override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
		if searchController.active {
			return filteredPosts.count
		} else {
			return posts.count
		}
	}
	
	func navigateToSuggestions() {
		print("here!!!!")
		let suggestionsVC = (UIApplication.sharedApplication().delegate as! AppDelegate).suggestionsVC
		navigationController?.setViewControllers([suggestionsVC], animated: false)
	}
	
    private func updateNowPlayingInfo() {
        let session = AVAudioSession.sharedInstance()
        
        if let post = currentlyPlayingPost {
            // state change, update play information
            let center = MPNowPlayingInfoCenter.defaultCenter()
            if post.player.progress != 1.0 {
                do {
                    try session.setCategory(AVAudioSessionCategoryPlayback)
                } catch _ {
                }
                do {
                    try session.setActive(true)
                } catch _ {
                }
                UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
                
                let artwork = post.song.fetchArtwork() ?? UIImage(named: "Sexy")!
				var count = posts.count
				if searchController.active {
					count = filteredPosts.count
				}
				
                center.nowPlayingInfo = [
                    MPMediaItemPropertyTitle: post.song.title,
                    MPMediaItemPropertyArtist: post.song.artist,
                    MPMediaItemPropertyAlbumTitle: post.song.album,
                    MPMediaItemPropertyArtwork: MPMediaItemArtwork(image: artwork),
                    MPMediaItemPropertyPlaybackDuration: post.player.duration,
                    MPNowPlayingInfoPropertyElapsedPlaybackTime: post.player.currentTime,
                    MPNowPlayingInfoPropertyPlaybackRate: post.player.isPlaying ? post.player.rate : 0.0,
                    MPNowPlayingInfoPropertyPlaybackQueueIndex: currentlyPlayingIndexPath!.row,
                    MPNowPlayingInfoPropertyPlaybackQueueCount: count ]
            } else {
                UIApplication.sharedApplication().endReceivingRemoteControlEvents()
                do {
                    try session.setActive(false)
                } catch {
                }
                center.nowPlayingInfo = nil
            }
        }
    }
    
    func notifCenterSetup() {
        NSNotificationCenter.defaultCenter().addObserverForName(PlayerDidChangeStateNotification, object: nil, queue: nil) { [weak self] note in
            if note.object as? Player == self?.currentlyPlayingPost?.player {
                self?.updateNowPlayingInfo()
            }
        }
        
        NSNotificationCenter.defaultCenter().addObserverForName(PlayerDidSeekNotification, object: nil, queue: nil) { [weak self] note in
            if note.object as? Player == self?.currentlyPlayingPost?.player {
                self?.updateNowPlayingInfo()
            }
        }
        
        NSNotificationCenter.defaultCenter().addObserverForName(SongDidDownloadArtworkNotification, object: nil, queue: nil) { [weak self] note in
            if note.object as? Song == self?.currentlyPlayingPost?.song {
                self?.updateNowPlayingInfo()
            }
        }
        
        NSNotificationCenter.defaultCenter().addObserverForName(PlayerDidFinishPlayingNotification, object: nil, queue: nil) { [weak self] note in
            if let current = self?.currentlyPlayingPost {
                if current.player == note.object as? Player {
                    let path = self!.currentlyPlayingIndexPath
                    if let path = path {
                        var row = path.row + 1
						var count = self!.posts.count
						if self!.searchController.active {
							count = self!.filteredPosts.count
						}
                        if row >= count {
                            row = 0
                        }
                        
                        self?.currentlyPlayingIndexPath = NSIndexPath(forRow: row, inSection: path.section)
                    }
                }
            }
        }
    }
    
    func commandCenterHandler() {
        // TODO: fetch the largest artwork image for lockscreen in Post
        let center = MPRemoteCommandCenter.sharedCommandCenter()
        center.playCommand.addTargetWithHandler { [weak self] _ in
            if let player = self?.currentlyPlayingPost?.player {
                player.play(true)
                return .Success
            }
            return .NoSuchContent
        }
        
        center.pauseCommand.addTargetWithHandler { [weak self] _ in
            if let player = self?.currentlyPlayingPost?.player {
                player.pause(true)
                return .Success
            }
            return .NoSuchContent
        }
        
        center.nextTrackCommand.addTargetWithHandler { [weak self] _ in
			var count = self!.posts.count
			if self!.searchController.active {
				count = self!.filteredPosts.count
			}
            if let path = self?.currentlyPlayingIndexPath {
                if path.row < count - 1 {
                    self?.currentlyPlayingIndexPath = NSIndexPath(forRow: path.row + 1, inSection: path.section)
                    return .Success
                }
            }
            return .NoSuchContent
        }
        
        center.previousTrackCommand.addTargetWithHandler { [weak self] _ in
            if let path = self?.currentlyPlayingIndexPath {
                if path.row > 0 {
                    self?.currentlyPlayingIndexPath = NSIndexPath(forRow: path.row - 1, inSection: path.section)
                }
                return .Success
            }
            return .NoSuchContent
        }
        
        center.seekForwardCommand.addTargetWithHandler { _ in .Success }
        center.seekBackwardCommand.addTargetWithHandler { _ in .Success }
    }
	
	// MARK: - Search Stuff
	
	private func filterContentForSearchText(searchText: String, scope: String = "All") {
		if searchText == "" {
			filteredPosts = posts
		} else {
			let pred = NSPredicate(format: "song.title contains[cd] %@ OR song.artist contains[cd] %@", searchText, searchText)
			filteredPosts = (posts as NSArray).filteredArrayUsingPredicate(pred) as! [Post]
		}
		tableView.reloadData()
	}
	
	func updateSearchResultsForSearchController(searchController: UISearchController) {
		filterContentForSearchText(searchController.searchBar.text!)
	}
	
	func searchBarSearchButtonClicked(searchBar: UISearchBar) {
		searchController.searchBar.endEditing(true)
	}
	
	//This allows for the text not to be viewed behind the search bar at the top of the screen
	private let statusBarView: UIView = {
		let view = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: 20))
		view.backgroundColor = UIColor.iceDarkRed
		return view
	}()
	
	func willPresentSearchController(searchController: UISearchController) {
		navigationController?.view.addSubview(statusBarView)
	}
	
	func didDismissSearchController(searchController: UISearchController) {
		statusBarView.removeFromSuperview()
	}
	
	override func preferredStatusBarStyle() -> UIStatusBarStyle {
		return .LightContent
	}
	
	// MARK: - Save song button clicked
	
	func didTapAddButtonForPostView(postView: PostView) {
		savedSongAlertView = SavedSongView.instanceFromNib()
		savedSongAlertView.showSongStatusPopup(postView.songStatus, playlist: "")
	}
	
	func didLongPressOnCell(postView: PostView) {
		SpotifyController.sharedController.spotifyIsAvailable { success in
			if success {
				let topVC = getTopViewController()
				let playlistVC = PlaylistTableViewController()
				let tableViewNavigationController = UINavigationController(rootViewController: playlistVC)
				
				playlistVC.song = postView.post
				topVC.presentViewController(tableViewNavigationController, animated: true, completion: nil)
			}
		}
	}
}<commit_msg>Delete extraneous print statements<commit_after>//
//  PlayerTableViewController.swift
//  IceFishing
//
//  Created by <NAME> on 11/1/15.
//  Copyright © 2015 CUAppDev. All rights reserved.
//

import UIKit
import MediaPlayer

class PlayerTableViewController: UITableViewController, UISearchResultsUpdating, UISearchControllerDelegate, UISearchBarDelegate {
	// For pinning
	let topPinViewContainer = UIView()
	let bottomPinViewContainer = UIView()
	let pinView = NSBundle.mainBundle().loadNibNamed("FeedTableViewCell", owner: nil, options: nil)[0] as! FeedTableViewCell
	var pinViewGestureRecognizer: UITapGestureRecognizer!
	
	var searchController: UISearchController!
	var posts: [Post] = []
	var filteredPosts: [Post] = []
    var currentlyPlayingPost: Post?
	
    var currentlyPlayingIndexPath: NSIndexPath? {
        didSet {
			var array = posts
			if searchController.active {
				array = filteredPosts
			}
            if let row = currentlyPlayingIndexPath?.row where currentlyPlayingPost?.isEqual(array[row]) ?? false {
                currentlyPlayingPost?.player.togglePlaying()
            } else {
                currentlyPlayingPost?.player.pause(true)
                currentlyPlayingPost?.player.progress = 1.0 // Fill cell as played
                
                if let currentlyPlayingIndexPath = currentlyPlayingIndexPath {
                    currentlyPlayingPost = array[currentlyPlayingIndexPath.row]
                    currentlyPlayingPost!.player.play(true)
                } else {
                    currentlyPlayingPost = nil
                }
            }
            tableView.selectRowAtIndexPath(currentlyPlayingIndexPath, animated: false, scrollPosition: .None)
        }
    }

	var savedSongAlertView: SavedSongView!
	
    override func viewDidLoad() {
        super.viewDidLoad()
		
		//Search Bar
		searchController = UISearchController(searchResultsController: nil)
		searchController.dimsBackgroundDuringPresentation = false
		searchController.delegate = self
		searchController.searchResultsUpdater = self
		searchController.searchBar.sizeToFit()
		searchController.searchBar.delegate = self
		let textFieldInsideSearchBar = searchController.searchBar.valueForKey("searchField") as? UITextField
		textFieldInsideSearchBar?.textColor = UIColor.whiteColor()
		
		extendedLayoutIncludesOpaqueBars = true
		definesPresentationContext = true
		
		tableView.tableHeaderView = searchController.searchBar
		tableView.backgroundView = UIView() // Fix color above search bar
		
		setupPinViews()
    }
	
	override func viewDidAppear(animated: Bool) {
		super.viewDidAppear(animated)
		
		positionPinViews()
		addRevealGesture()
	}
	
	override func viewDidDisappear(animated: Bool) {
		super.viewDidDisappear(animated)
		
		removeRevealGesture()
	}
	
    // MARK: - Table view data source
	
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
		if posts.count > 0 && !searchController.active {
			self.tableView.backgroundView = nil
			return 1
		} else {
			let emptyView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height))
			
			let label = UILabel(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height))
			label.text = "No posts in the last 24 hours"
			label.textColor = UIColor.whiteColor()
			label.textAlignment = .Center
			label.numberOfLines = 2
			label.font = UIFont(name: "AvenirNext-Regular", size: 16)
			emptyView.addSubview(label)
			
			let button = UIButton(frame: CGRect(x: 0, y: 0, width: 190, height: 35))
			button.center = self.view.center
			button.center.y += 35
			button.backgroundColor = UIColor.iceDarkRed
			button.setTitle("Follow more friends", forState: .Normal)
			button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
			button.titleLabel?.font = UIFont(name: "AvenirNext-Regular", size: 16)
			button.layer.cornerRadius = 5.0
			button.addTarget(self, action: "navigateToSuggestions", forControlEvents: .TouchUpInside)
			
			emptyView.addSubview(button)
			
			self.tableView.backgroundView = emptyView
		}
		
        return 0
    }
    
	override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
		if searchController.active {
			return filteredPosts.count
		} else {
			return posts.count
		}
	}
	
	func navigateToSuggestions() {
		let suggestionsVC = (UIApplication.sharedApplication().delegate as! AppDelegate).suggestionsVC
		navigationController?.setViewControllers([suggestionsVC], animated: false)
	}
	
    private func updateNowPlayingInfo() {
        let session = AVAudioSession.sharedInstance()
        
        if let post = currentlyPlayingPost {
            // state change, update play information
            let center = MPNowPlayingInfoCenter.defaultCenter()
            if post.player.progress != 1.0 {
                do {
                    try session.setCategory(AVAudioSessionCategoryPlayback)
                } catch _ {
                }
                do {
                    try session.setActive(true)
                } catch _ {
                }
                UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
                
                let artwork = post.song.fetchArtwork() ?? UIImage(named: "Sexy")!
				var count = posts.count
				if searchController.active {
					count = filteredPosts.count
				}
				
                center.nowPlayingInfo = [
                    MPMediaItemPropertyTitle: post.song.title,
                    MPMediaItemPropertyArtist: post.song.artist,
                    MPMediaItemPropertyAlbumTitle: post.song.album,
                    MPMediaItemPropertyArtwork: MPMediaItemArtwork(image: artwork),
                    MPMediaItemPropertyPlaybackDuration: post.player.duration,
                    MPNowPlayingInfoPropertyElapsedPlaybackTime: post.player.currentTime,
                    MPNowPlayingInfoPropertyPlaybackRate: post.player.isPlaying ? post.player.rate : 0.0,
                    MPNowPlayingInfoPropertyPlaybackQueueIndex: currentlyPlayingIndexPath!.row,
                    MPNowPlayingInfoPropertyPlaybackQueueCount: count ]
            } else {
                UIApplication.sharedApplication().endReceivingRemoteControlEvents()
                do {
                    try session.setActive(false)
                } catch {
                }
                center.nowPlayingInfo = nil
            }
        }
    }
    
    func notifCenterSetup() {
        NSNotificationCenter.defaultCenter().addObserverForName(PlayerDidChangeStateNotification, object: nil, queue: nil) { [weak self] note in
            if note.object as? Player == self?.currentlyPlayingPost?.player {
                self?.updateNowPlayingInfo()
            }
        }
        
        NSNotificationCenter.defaultCenter().addObserverForName(PlayerDidSeekNotification, object: nil, queue: nil) { [weak self] note in
            if note.object as? Player == self?.currentlyPlayingPost?.player {
                self?.updateNowPlayingInfo()
            }
        }
        
        NSNotificationCenter.defaultCenter().addObserverForName(SongDidDownloadArtworkNotification, object: nil, queue: nil) { [weak self] note in
            if note.object as? Song == self?.currentlyPlayingPost?.song {
                self?.updateNowPlayingInfo()
            }
        }
        
        NSNotificationCenter.defaultCenter().addObserverForName(PlayerDidFinishPlayingNotification, object: nil, queue: nil) { [weak self] note in
            if let current = self?.currentlyPlayingPost {
                if current.player == note.object as? Player {
                    let path = self!.currentlyPlayingIndexPath
                    if let path = path {
                        var row = path.row + 1
						var count = self!.posts.count
						if self!.searchController.active {
							count = self!.filteredPosts.count
						}
                        if row >= count {
                            row = 0
                        }
                        
                        self?.currentlyPlayingIndexPath = NSIndexPath(forRow: row, inSection: path.section)
                    }
                }
            }
        }
    }
    
    func commandCenterHandler() {
        // TODO: fetch the largest artwork image for lockscreen in Post
        let center = MPRemoteCommandCenter.sharedCommandCenter()
        center.playCommand.addTargetWithHandler { [weak self] _ in
            if let player = self?.currentlyPlayingPost?.player {
                player.play(true)
                return .Success
            }
            return .NoSuchContent
        }
        
        center.pauseCommand.addTargetWithHandler { [weak self] _ in
            if let player = self?.currentlyPlayingPost?.player {
                player.pause(true)
                return .Success
            }
            return .NoSuchContent
        }
        
        center.nextTrackCommand.addTargetWithHandler { [weak self] _ in
			var count = self!.posts.count
			if self!.searchController.active {
				count = self!.filteredPosts.count
			}
            if let path = self?.currentlyPlayingIndexPath {
                if path.row < count - 1 {
                    self?.currentlyPlayingIndexPath = NSIndexPath(forRow: path.row + 1, inSection: path.section)
                    return .Success
                }
            }
            return .NoSuchContent
        }
        
        center.previousTrackCommand.addTargetWithHandler { [weak self] _ in
            if let path = self?.currentlyPlayingIndexPath {
                if path.row > 0 {
                    self?.currentlyPlayingIndexPath = NSIndexPath(forRow: path.row - 1, inSection: path.section)
                }
                return .Success
            }
            return .NoSuchContent
        }
        
        center.seekForwardCommand.addTargetWithHandler { _ in .Success }
        center.seekBackwardCommand.addTargetWithHandler { _ in .Success }
    }
	
	// MARK: - Search Stuff
	
	private func filterContentForSearchText(searchText: String, scope: String = "All") {
		if searchText == "" {
			filteredPosts = posts
		} else {
			let pred = NSPredicate(format: "song.title contains[cd] %@ OR song.artist contains[cd] %@", searchText, searchText)
			filteredPosts = (posts as NSArray).filteredArrayUsingPredicate(pred) as! [Post]
		}
		tableView.reloadData()
	}
	
	func updateSearchResultsForSearchController(searchController: UISearchController) {
		filterContentForSearchText(searchController.searchBar.text!)
	}
	
	func searchBarSearchButtonClicked(searchBar: UISearchBar) {
		searchController.searchBar.endEditing(true)
	}
	
	//This allows for the text not to be viewed behind the search bar at the top of the screen
	private let statusBarView: UIView = {
		let view = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: 20))
		view.backgroundColor = UIColor.iceDarkRed
		return view
	}()
	
	func willPresentSearchController(searchController: UISearchController) {
		navigationController?.view.addSubview(statusBarView)
	}
	
	func didDismissSearchController(searchController: UISearchController) {
		statusBarView.removeFromSuperview()
	}
	
	override func preferredStatusBarStyle() -> UIStatusBarStyle {
		return .LightContent
	}
	
	// MARK: - Save song button clicked
	
	func didTapAddButtonForPostView(postView: PostView) {
		savedSongAlertView = SavedSongView.instanceFromNib()
		savedSongAlertView.showSongStatusPopup(postView.songStatus, playlist: "")
	}
	
	func didLongPressOnCell(postView: PostView) {
		SpotifyController.sharedController.spotifyIsAvailable { success in
			if success {
				let topVC = getTopViewController()
				let playlistVC = PlaylistTableViewController()
				let tableViewNavigationController = UINavigationController(rootViewController: playlistVC)
				
				playlistVC.song = postView.post
				topVC.presentViewController(tableViewNavigationController, animated: true, completion: nil)
			}
		}
	}
}
{'fraction_non_alphanumeric': 0.0830944368274544, 'fraction_numerical': 0.004237816278200175, 'mean_word_length': 2.4748087195033923, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, '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': '2009431', 'n_tokens_mistral': 7209, 'n_tokens_neox': 6215, 'n_words': 1350}