<commit_before>//
//  SearchViewController.swift
//  PhotoViewer
//
//  Created by <NAME> on 2/27/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit

class SearchViewController: UIViewController
{
    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var tableViewBottomConstraint: NSLayoutConstraint!
    
    private var keyboardHeight:CGFloat = 0.0
    
    private var searchHistory = [CD_SearchHistoryItem]()
    
    deinit
    {
        print("deinit SearchViewController")
    }
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        navigationController?.navigationBarHidden = true
        automaticallyAdjustsScrollViewInsets = false
        searchBar.delegate = self
        tableView.dataSource = self
        tableView.delegate = self
        tableView.tableFooterView = UIView(frame: CGRectZero)
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(SearchViewController.keyboardWillShow), name: UIKeyboardWillShowNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(SearchViewController.keyboardWillHide), name: UIKeyboardWillHideNotification, object: nil)
        
        if let history = CoreDataController.sharedInstance.fetchSearchHistory()
        {
            searchHistory.appendContentsOf(history)
            tableView.reloadData()
        }
    }
    
    override func viewWillAppear(animated: Bool)
    {
        super.viewWillAppear(animated)
        tableView.reloadData()
    }
    
    @objc private func keyboardWillShow(notification: NSNotification)
    {
        if let userInfo = notification.userInfo
        {
            if let value = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue
            {
                let keyboardRec = value.CGRectValue()
                keyboardHeight = keyboardRec.height
                tableViewBottomConstraint.constant += keyboardHeight
            }
        }
    }
    
    @objc private func keyboardWillHide(notification: NSNotification)
    {
        if keyboardHeight != 0.0
        {
            tableViewBottomConstraint.constant -= keyboardHeight
            keyboardHeight = 0.0
        }
    }
    
    private func formatSearchTerm(searchTerm: String) -> String
    {
        let words = searchTerm.componentsSeparatedByString(" ")
        if let encoded = words.first!.stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
        {
            return encoded.lowercaseString
        }
        else
        {
            return words.first!.lowercaseString
        }
    }
    
    // MARK: - Navigation
    
    private func showSearchResultsForSearchTerm(searchTerm: String)
    {
        let searchHistoryItem = CoreDataController.sharedInstance.saveSearchHistoryItem(searchTerm, timeStamp: NSDate().timeIntervalSince1970)
        searchBar.text = ""
        searchBar.endEditing(true)
        searchHistory.insert(searchHistoryItem, atIndex: 0)
        performSegueWithIdentifier("showSearchResults", sender: searchHistoryItem.searchTerm)
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
    {
        if segue.identifier == "showSearchResults"
        {
            let searchResultsVC = segue.destinationViewController as! SearchResultsViewController
            searchResultsVC.searchTerm = sender as! String
        }
    }
}

extension SearchViewController: UITableViewDataSource, UITableViewDelegate
{
    // MARK: - UITableViewDataSource
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return searchHistory.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCellWithIdentifier("historyCell", forIndexPath: indexPath)
        cell.textLabel?.text = searchHistory[indexPath.row].searchTerm
        return cell
    }
    
    // MARK: - UITableViewDelegate
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        let searchHistoryItem = searchHistory[indexPath.row]
        let searchTerm = searchHistoryItem.searchTerm!
        
        CoreDataController.sharedInstance.deleteSearchHistoryItem(searchHistoryItem)
        searchHistory.removeAtIndex(indexPath.row)
        
        showSearchResultsForSearchTerm(searchTerm)
    }
}

extension SearchViewController: UISearchBarDelegate
{
    func searchBarTextDidBeginEditing(searchBar: UISearchBar)
    {
        searchBar.showsCancelButton = true
    }
    
    func searchBarTextDidEndEditing(searchBar: UISearchBar)
    {
        searchBar.showsCancelButton = false
    }
    
    func searchBarCancelButtonClicked(searchBar: UISearchBar)
    {
        searchBar.endEditing(true)
    }
    
    func searchBarSearchButtonClicked(searchBar: UISearchBar)
    {
        if let text = searchBar.text where text != ""
        {
            showSearchResultsForSearchTerm(formatSearchTerm(text))
        }
    }
}
<commit_msg>adding feature to delete history<commit_after>//
//  SearchViewController.swift
//  PhotoViewer
//
//  Created by <NAME> on 2/27/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit

class SearchViewController: UIViewController
{
    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var tableViewBottomConstraint: NSLayoutConstraint!
    
    private var keyboardHeight:CGFloat = 0.0
    
    private var searchHistory = [CD_SearchHistoryItem]()
    
    deinit
    {
        print("deinit SearchViewController")
    }
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        navigationController?.navigationBarHidden = true
        automaticallyAdjustsScrollViewInsets = false
        searchBar.delegate = self
        tableView.dataSource = self
        tableView.delegate = self
        tableView.tableFooterView = UIView(frame: CGRectZero)
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(SearchViewController.keyboardWillShow), name: UIKeyboardWillShowNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(SearchViewController.keyboardWillHide), name: UIKeyboardWillHideNotification, object: nil)
        
        if let history = CoreDataController.sharedInstance.fetchSearchHistory()
        {
            searchHistory.appendContentsOf(history)
            tableView.reloadData()
        }
    }
    
    override func viewWillAppear(animated: Bool)
    {
        super.viewWillAppear(animated)
        tableView.reloadData()
    }
    
    @objc private func keyboardWillShow(notification: NSNotification)
    {
        if let userInfo = notification.userInfo
        {
            if let value = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue
            {
                let keyboardRec = value.CGRectValue()
                keyboardHeight = keyboardRec.height
                tableViewBottomConstraint.constant += keyboardHeight
            }
        }
    }
    
    @objc private func keyboardWillHide(notification: NSNotification)
    {
        if keyboardHeight != 0.0
        {
            tableViewBottomConstraint.constant -= keyboardHeight
            keyboardHeight = 0.0
        }
    }
    
    private func formatSearchTerm(searchTerm: String) -> String
    {
        let words = searchTerm.componentsSeparatedByString(" ")
        if let encoded = words.first!.stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
        {
            return encoded.lowercaseString
        }
        else
        {
            return words.first!.lowercaseString
        }
    }
    
    // MARK: - Navigation
    
    private func showSearchResultsForSearchTerm(searchTerm: String)
    {
        let searchHistoryItem = CoreDataController.sharedInstance.saveSearchHistoryItem(searchTerm, timeStamp: NSDate().timeIntervalSince1970)
        searchBar.text = ""
        searchBar.endEditing(true)
        searchHistory.insert(searchHistoryItem, atIndex: 0)
        performSegueWithIdentifier("showSearchResults", sender: searchHistoryItem.searchTerm)
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
    {
        if segue.identifier == "showSearchResults"
        {
            let searchResultsVC = segue.destinationViewController as! SearchResultsViewController
            searchResultsVC.searchTerm = sender as! String
        }
    }
}

extension SearchViewController: UITableViewDataSource, UITableViewDelegate
{
    // MARK: - UITableViewDataSource
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return searchHistory.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCellWithIdentifier("historyCell", forIndexPath: indexPath)
        cell.textLabel?.text = searchHistory[indexPath.row].searchTerm
        return cell
    }
    
    func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool
    {
        return true
    }
    
    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
    {
        if (editingStyle == UITableViewCellEditingStyle.Delete)
        {
            let searchHistoryItem = searchHistory[indexPath.row]
            CoreDataController.sharedInstance.deleteSearchHistoryItem(searchHistoryItem)
            searchHistory.removeAtIndex(indexPath.row)
            tableView.reloadData()
        }
    }
    
    // MARK: - UITableViewDelegate
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        let searchHistoryItem = searchHistory[indexPath.row]
        let searchTerm = searchHistoryItem.searchTerm!
        
        CoreDataController.sharedInstance.deleteSearchHistoryItem(searchHistoryItem)
        searchHistory.removeAtIndex(indexPath.row)
        
        showSearchResultsForSearchTerm(searchTerm)
    }
}

extension SearchViewController: UISearchBarDelegate
{
    func searchBarTextDidBeginEditing(searchBar: UISearchBar)
    {
        searchBar.showsCancelButton = true
    }
    
    func searchBarTextDidEndEditing(searchBar: UISearchBar)
    {
        searchBar.showsCancelButton = false
    }
    
    func searchBarCancelButtonClicked(searchBar: UISearchBar)
    {
        searchBar.endEditing(true)
    }
    
    func searchBarSearchButtonClicked(searchBar: UISearchBar)
    {
        if let text = searchBar.text where text != ""
        {
            showSearchResultsForSearchTerm(formatSearchTerm(text))
        }
    }
}

{'fraction_non_alphanumeric': 0.07178555202180827, 'fraction_numerical': 0.003634711494775102, 'mean_word_length': 2.734645402103834, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 14, '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': '4136933', 'n_tokens_mistral': 2979, 'n_tokens_neox': 2862, 'n_words': 535}