<commit_before>//
//  SearchBoxTableViewController.swift
//  
//
//  Created by <NAME> on 2015-8-19.
//
//

import UIKit
import CoreData
import Haneke
import MJRefresh

class SearchBoxTableViewController: UITableViewController, UISearchResultsUpdating, UISearchControllerDelegate, UISearchBarDelegate, MGSwipeTableCellDelegate {

    var isSearching = false {
        willSet {
            if newValue == true {
                self.tableView.footer.resetNoMoreData()
            } else {
                self.tableView.footer.noticeNoMoreData()
            }
        }
    }
    
//    var fetchResultController = NSFetchedResultsController!
    var searchController: UISearchController!
    var isSearchTabelControllerFirstDisplay = true
    
//    var snapshot: UIView!
//    let blurredView = UIVisualEffectView(effect: UIBlurEffect(style: .Light))   // or .ExtraLight .Dark


    let searchModel = BangumiSearchModel.shared
    let request = BangumiRequest.shared
    
    
    @IBOutlet weak var searchButton: UIBarButtonItem!
    
    @IBAction func searchButtonClicked(button: UIBarButtonItem?) {
        
        // Set searchController
        searchController = UISearchController(searchResultsController: nil)
        searchController.delegate = self
        searchController.searchBar.delegate = self
        searchController.searchBar.placeholder = "条目搜索"
        searchController.searchResultsUpdater = self
        searchController.dimsBackgroundDuringPresentation = true
        searchController.hidesNavigationBarDuringPresentation = false
        searchController.searchBar.enablesReturnKeyAutomatically = false
        
        let title = self.navigationItem.title ?? "搜索盒子"
        searchController.searchBar.text = (title == "搜索盒子") ? "" : title
        // Present the view controller.
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            self.presentViewController(self.searchController, animated: true, completion: nil)
        })
    }
    
    // MARK: Search Controller Delegate
    func updateSearchResultsForSearchController(searchController: UISearchController) {
       if searchController.searchBar.text == "搜索盒子" {
            isSearching = false
            fetchLocalData()
        } else if searchController.searchBar.text != "" {
            isSearching = true
        }
        self.tableView.reloadData()
        self.tableView.footer.resetNoMoreData()
    }
    
    // MARK: Search Bar Delegate
    
    func searchBarSearchButtonClicked(searchBar: UISearchBar) {

        if searchBar.text == "" || searchBar.text == "搜索盒子" {
            isSearching = false
            fetchLocalData()
            self.navigationItem.title = "搜索盒子"
        } else {
            searchModel.dropModel()
            loadMoreData(searchBar)
            self.navigationItem.title = searchBar.text
        }
        
        searchController.dismissViewControllerAnimated(true, completion: nil)
        self.tableView.reloadData()
    }
    
    func searchBarCancelButtonClicked(searchBar: UISearchBar) {
        
        debugPrint("----> search text: \(searchBar.text)")<commit_msg>fix: search box data display logic<commit_after>//
//  SearchBoxTableViewController.swift
//  
//
//  Created by <NAME> on 2015-8-19.
//
//

import UIKit
import CoreData
import Haneke
import MJRefresh

class SearchBoxTableViewController: UITableViewController, UISearchResultsUpdating, UISearchControllerDelegate, UISearchBarDelegate, MGSwipeTableCellDelegate {

    var isSearching = false {
        willSet {
            if newValue == true {
                self.tableView.footer.resetNoMoreData()
            } else {
                self.tableView.footer.noticeNoMoreData()
            }
        }
    }
    
//    var fetchResultController = NSFetchedResultsController!
    var searchController: UISearchController!
    var isSearchTabelControllerFirstDisplay = true
    
//    var snapshot: UIView!
//    let blurredView = UIVisualEffectView(effect: UIBlurEffect(style: .Light))   // or .ExtraLight .Dark


    let searchModel = BangumiSearchModel.shared
    let request = BangumiRequest.shared
    
    
    @IBOutlet weak var searchButton: UIBarButtonItem!
    
    @IBAction func searchButtonClicked(button: UIBarButtonItem?) {
        
        // Set searchController
        searchController = UISearchController(searchResultsController: nil)
        searchController.delegate = self
        searchController.searchBar.delegate = self
        searchController.searchBar.placeholder = "条目搜索"
        searchController.searchResultsUpdater = self
        searchController.dimsBackgroundDuringPresentation = true
        searchController.hidesNavigationBarDuringPresentation = false
        searchController.searchBar.enablesReturnKeyAutomatically = false
        
        let title = self.navigationItem.title ?? "搜索盒子"
        searchController.searchBar.text = (title == "搜索盒子") ? "" : title
        // Present the view controller.
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            self.presentViewController(self.searchController, animated: true, completion: nil)
        })
    }
    
    // MARK: Search Controller Delegate
    func updateSearchResultsForSearchController(searchController: UISearchController) {
       if searchController.searchBar.text == "搜索盒子" {
            isSearching = false
            fetchLocalData()
        }
        
        self.tableView.reloadData()
        self.tableView.footer.resetNoMoreData()
    }
    
    // MARK: Search Bar Delegate
    
    func searchBarSearchButtonClicked(searchBar: UISearchBar) {

        if searchBar.text == "" || searchBar.text == "搜索盒子" {
            isSearching = false
            fetchLocalData()
            self.navigationItem.title = "搜索盒子"
        } else {
            searchModel.dropModel()
            loadMoreData(searchBar)
            self.navigationItem.title = searchBar.text
        }
        
        searchController.dismissViewControllerAnimated(true, completion: nil)
        self.tableView.reloadData()
    }
    
    func searchBarCancelButtonClicked(searchBar: UISearchBar) {
        
        debugPrint("----> search text: \(searchBar.text)")
{'fraction_non_alphanumeric': 0.08280973094892863, 'fraction_numerical': 0.002255517963589496, 'mean_word_length': 2.5172804532577904, 'pattern_counts': {'":': 0, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7586371', 'n_tokens_mistral': 1705, 'n_tokens_neox': 1688, 'n_words': 315}