<commit_before>//
//  BaseSongTableViewController.swift
//  CGSSGuide
//
//  Created by zzk on 16/8/7.
//  Copyright © 2016年 zzk. All rights reserved.
//

import UIKit
import ZKDrawerController

protocol BaseSongTableViewControllerDelegate: class {
    func selectLive(_ live: CGSSLive, beatmap: CGSSBeatmap, diff: Int)
}
class BaseSongTableViewController: RefreshableTableViewController, ZKDrawerControllerDelegate {
    weak var delegate: BaseSongTableViewControllerDelegate?
    lazy var defualtLiveList: [CGSSLive] = {
        var result = [CGSSLive]()
        let semaphore = DispatchSemaphore.init(value: 0)
        CGSSGameResource.shared.master.getLives(callback: { (lives) in
            result = lives
            semaphore.signal()
        })
        semaphore.wait()
        return result
    }()
    var liveList: [CGSSLive] = [CGSSLive]()
    var sorter: CGSSSorter {
        get {
            return CGSSSorterFilterManager.default.liveSorter
        }
        set {
            CGSSSorterFilterManager.default.liveSorter = newValue
        }
    }
    var filter: CGSSLiveFilter {
        get {
            return CGSSSorterFilterManager.default.liveFilter
        }
        set {
            CGSSSorterFilterManager.default.liveFilter = newValue
        }
    }
    var filterVC: SongFilterSortController!
    
    // 根据设定的筛选和排序方法重新展现数据
    override func refresh() {
        filter.searchText = searchBar.text ?? ""
        self.liveList = filter.filter(defualtLiveList)
        sorter.sortList(&self.liveList)
        tableView.reloadData()
    }
    
    func cancelAction() {
        searchBar.resignFirstResponder()
        searchBar.text = ""
        refresh()
    }
    
    override func refresherValueChanged() {
        check([.beatmap, .master])
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        refresh()
    }
    
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        let drawer = CGSSClient.shared.drawerController
        drawer?.rightVC = filterVC
        drawer?.delegate = self
        drawer?.defaultRightWidth = min(Screen.width - 86, 400)
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        CGSSClient.shared.drawerController?.rightVC = nil
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 初始化导航栏的搜索条
        self.navigationItem.titleView = searchBar
        searchBar.placeholder = NSLocalizedString("歌曲名", comment: "")
        
        self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(image: #imageLiteral(resourceName: "798-filter-toolbar"), style: .plain, target: self, action: #selector(filterAction))
        self.tableView.register(SongTableViewCell.self, forCellReuseIdentifier: "SongCell")
        self.tableView.rowHeight = 86
        
        filterVC = SongFilterSortController()
        filterVC.filter = self.filter
        filterVC.sorter = self.sorter
        filterVC.delegate = self
        
        CGSSNotificationCenter.add(self, selector: #selector(updateEnd(notification:)), name: CGSSNotificationCenter.updateEnd, object: nil)
    }
    
    deinit {
        CGSSNotificationCenter.removeAll(self)
    }
    
    func reloadData() {
        CGSSGameResource.shared.master.getLives { (lives) in
            self.defualtLiveList = lives
            self.refresh()
        }
    }
    
    func updateEnd(notification: Notification) {
        let types = notification.object as! CGSSUpdateDataTypes
        if types.contains(.master) || types.contains(.beatmap) {
            self.reloadData()
        }
    }
    
    func filterAction() {
        CGSSClient.shared.drawerController?.show(animated: true)
        // navigationController?.pushViewController(filterVC, animated: true)

    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // MARK: - Table view data source
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return liveList.count<commit_msg>change live data's db query to async<commit_after>//
//  BaseSongTableViewController.swift
//  CGSSGuide
//
//  Created by zzk on 16/8/7.
//  Copyright © 2016年 zzk. All rights reserved.
//

import UIKit
import ZKDrawerController

protocol BaseSongTableViewControllerDelegate: class {
    func selectLive(_ live: CGSSLive, beatmap: CGSSBeatmap, diff: Int)
}
class BaseSongTableViewController: RefreshableTableViewController, ZKDrawerControllerDelegate {
    weak var delegate: BaseSongTableViewControllerDelegate?
    var defualtLiveList = [CGSSLive]()
    var liveList: [CGSSLive] = [CGSSLive]()
    var sorter: CGSSSorter {
        get {
            return CGSSSorterFilterManager.default.liveSorter
        }
        set {
            CGSSSorterFilterManager.default.liveSorter = newValue
        }
    }
    var filter: CGSSLiveFilter {
        get {
            return CGSSSorterFilterManager.default.liveFilter
        }
        set {
            CGSSSorterFilterManager.default.liveFilter = newValue
        }
    }
    var filterVC: SongFilterSortController!
    
    // 根据设定的筛选和排序方法重新展现数据
    override func refresh() {
        filter.searchText = searchBar.text ?? ""
        self.liveList = filter.filter(defualtLiveList)
        sorter.sortList(&self.liveList)
        tableView.reloadData()
    }
    
    func cancelAction() {
        searchBar.resignFirstResponder()
        searchBar.text = ""
        refresh()
    }
    
    override func refresherValueChanged() {
        check([.beatmap, .master])
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        let drawer = CGSSClient.shared.drawerController
        drawer?.rightVC = filterVC
        drawer?.delegate = self
        drawer?.defaultRightWidth = min(Screen.width - 86, 400)
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        CGSSClient.shared.drawerController?.rightVC = nil
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 初始化导航栏的搜索条
        self.navigationItem.titleView = searchBar
        searchBar.placeholder = NSLocalizedString("歌曲名", comment: "")
        
        self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(image: #imageLiteral(resourceName: "798-filter-toolbar"), style: .plain, target: self, action: #selector(filterAction))
        self.tableView.register(SongTableViewCell.self, forCellReuseIdentifier: "SongCell")
        self.tableView.rowHeight = 86
        
        filterVC = SongFilterSortController()
        filterVC.filter = self.filter
        filterVC.sorter = self.sorter
        filterVC.delegate = self
        
        CGSSNotificationCenter.add(self, selector: #selector(updateEnd(notification:)), name: CGSSNotificationCenter.updateEnd, object: nil)
        
        reloadData()
    }
    
    deinit {
        CGSSNotificationCenter.removeAll(self)
    }
    
    func reloadData() {
        CGSSLoadingHUDManager.default.show()
        DispatchQueue.global(qos: .userInitiated).async {
            CGSSGameResource.shared.master.getLives { (lives) in
                self.defualtLiveList = lives
                DispatchQueue.main.async {
                    CGSSLoadingHUDManager.default.hide()
                    self.refresh()
                }
            }
        }
    }
    
    func updateEnd(notification: Notification) {
        let types = notification.object as! CGSSUpdateDataTypes
        if types.contains(.master) || types.contains(.beatmap) {
            self.reloadData()
        }
    }
    
    func filterAction() {
        CGSSClient.shared.drawerController?.show(animated: true)
        // navigationController?.pushViewController(filterVC, animated: true)

    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // MARK: - Table view data source
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return liveList.count
{'fraction_non_alphanumeric': 0.08697112155809268, 'fraction_numerical': 0.004365345869711216, 'mean_word_length': 2.389605462822458, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '3832278', 'n_tokens_mistral': 2633, 'n_tokens_neox': 2520, 'n_words': 505}