<commit_before>//
//  SubscriptionsViewController.swift
//  Rocket.Chat
//
//  Created by <NAME> on 7/21/16.
//  Copyright © 2016 Rocket.Chat. All rights reserved.
//

import RealmSwift
import SideMenu

class SubscriptionsViewController: BaseViewController {
    
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var activityViewSearching: UIActivityIndicatorView!
    @IBOutlet weak var textFieldSearch: UITextField! {
        didSet {
            textFieldSearch.placeholder = localizedString("subscriptions.search")
            
            if let placeholder = textFieldSearch.placeholder {
                let color = UIColor(rgb: 0x9AB1BF, alphaVal: 1)
                textFieldSearch.attributedPlaceholder = NSAttributedString(string:placeholder, attributes: [NSForegroundColorAttributeName: color])
            }
        }
    }

    @IBOutlet weak var viewTextField: UIView! {
        didSet {
            viewTextField.layer.cornerRadius = 4
            viewTextField.layer.masksToBounds = true
        }
    }
    
    var assigned = false
    var isSearchingLocally = false
    var isSearchingRemotely = false
    var searchResult: [Subscription]?
    var subscriptions: Results<Subscription>?
    var subscriptionsToken: NotificationToken?
    var usersToken: NotificationToken?
    
    var groupInfomation: [[String: String]]?
    var groupSubscriptions: [[Subscription]]?

    override func awakeFromNib() {
        super.awakeFromNib()
        subscribeModelChanges()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        textFieldSearch.resignFirstResponder()
        unregisterKeyboardNotifications()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        tableView.reloadData()
        registerKeyboardHandlers(tableView)
    }
}

extension SubscriptionsViewController {
    
    func searchBy(_ text: String = "") {
        guard let auth = AuthManager.isAuthenticated() else { return }
        subscriptions = auth.subscriptions.filter("name CONTAINS %@", text)
        
        if text.characters.count == 0 {
            isSearchingLocally = false
            isSearchingRemotely = false
            searchResult = []
            
            groupSubscription()
            tableView.reloadData()
            tableView.tableFooterView = nil
            
            activityViewSearching.stopAnimating()
            
            return
        }
        
        if subscriptions?.count == 0 {
            searchOnSpotlight(text)
            return
        }
        
        isSearchingLocally = true
        isSearchingRemotely = false
        
        groupSubscription()
        tableView.reloadData()
        
        let footerView = SubscriptionSearchMoreView.instanceFromNib() as! SubscriptionSearchMoreView
        footerView.delegate = self
        tableView.tableFooterView = footerView
    }
    
    func searchOnSpotlight(_ text: String = "") {
        let historyText = text
        tableView.tableFooterView = nil
        activityViewSearching.startAnimating()
        
        SubscriptionManager.spotlight(text) { [unowned self] (result) in
            let currentText = self.textFieldSearch.text ?? ""
            
            if currentText.characters.count == 0 {
                return
            }
            
            self.activityViewSearching.stopAnimating()
            self.isSearchingRemotely = true
            self.searchResult = result
            self.groupSubscription()
            self.tableView.reloadData()<commit_msg>Remove unused code<commit_after>//
//  SubscriptionsViewController.swift
//  Rocket.Chat
//
//  Created by <NAME> on 7/21/16.
//  Copyright © 2016 Rocket.Chat. All rights reserved.
//

import RealmSwift
import SideMenu

class SubscriptionsViewController: BaseViewController {
    
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var activityViewSearching: UIActivityIndicatorView!
    @IBOutlet weak var textFieldSearch: UITextField! {
        didSet {
            textFieldSearch.placeholder = localizedString("subscriptions.search")
            
            if let placeholder = textFieldSearch.placeholder {
                let color = UIColor(rgb: 0x9AB1BF, alphaVal: 1)
                textFieldSearch.attributedPlaceholder = NSAttributedString(string:placeholder, attributes: [NSForegroundColorAttributeName: color])
            }
        }
    }

    @IBOutlet weak var viewTextField: UIView! {
        didSet {
            viewTextField.layer.cornerRadius = 4
            viewTextField.layer.masksToBounds = true
        }
    }
    
    var assigned = false
    var isSearchingLocally = false
    var isSearchingRemotely = false
    var searchResult: [Subscription]?
    var subscriptions: Results<Subscription>?
    var subscriptionsToken: NotificationToken?
    var usersToken: NotificationToken?
    
    var groupInfomation: [[String: String]]?
    var groupSubscriptions: [[Subscription]]?

    override func awakeFromNib() {
        super.awakeFromNib()
        subscribeModelChanges()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        textFieldSearch.resignFirstResponder()
        unregisterKeyboardNotifications()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        tableView.reloadData()
        registerKeyboardHandlers(tableView)
    }
}

extension SubscriptionsViewController {
    
    func searchBy(_ text: String = "") {
        guard let auth = AuthManager.isAuthenticated() else { return }
        subscriptions = auth.subscriptions.filter("name CONTAINS %@", text)
        
        if text.characters.count == 0 {
            isSearchingLocally = false
            isSearchingRemotely = false
            searchResult = []
            
            groupSubscription()
            tableView.reloadData()
            tableView.tableFooterView = nil
            
            activityViewSearching.stopAnimating()
            
            return
        }
        
        if subscriptions?.count == 0 {
            searchOnSpotlight(text)
            return
        }
        
        isSearchingLocally = true
        isSearchingRemotely = false
        
        groupSubscription()
        tableView.reloadData()
        
        let footerView = SubscriptionSearchMoreView.instanceFromNib() as! SubscriptionSearchMoreView
        footerView.delegate = self
        tableView.tableFooterView = footerView
    }
    
    func searchOnSpotlight(_ text: String = "") {
        tableView.tableFooterView = nil
        activityViewSearching.startAnimating()
        
        SubscriptionManager.spotlight(text) { [unowned self] (result) in
            let currentText = self.textFieldSearch.text ?? ""
            
            if currentText.characters.count == 0 {
                return
            }
            
            self.activityViewSearching.stopAnimating()
            self.isSearchingRemotely = true
            self.searchResult = result
            self.groupSubscription()
            self.tableView.reloadData()
{'fraction_non_alphanumeric': 0.07360687551982256, 'fraction_numerical': 0.0047130579428888274, 'mean_word_length': 2.135593220338983, 'pattern_counts': {'":': 0, '<': 7, '<?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': '3110896', 'n_tokens_mistral': 1918, 'n_tokens_neox': 1859, 'n_words': 384}