<commit_before>//
//  PasswordTableViewController.swift
//  pass
//
//  Created by <NAME> on 18/1/2017.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit
import Result
import SVProgressHUD


class PasswordsTableViewController: UITableViewController {
    private var passwordEntities: [PasswordEntity]?
    var filteredPasswordEntities = [PasswordEntity]()
    let searchController = UISearchController(searchResultsController: nil)
    
    @IBAction func refreshPasswords(_ sender: UIBarButtonItem) {
        SVProgressHUD.setDefaultMaskType(.black)
        SVProgressHUD.show(withStatus: "Pull Remote Repository")
        DispatchQueue.global(qos: .userInitiated).async {
            if PasswordStore.shared.pullRepository(transferProgressBlock: {(git_transfer_progress, stop) in
                DispatchQueue.main.async {
                SVProgressHUD.showProgress(Float(git_transfer_progress.pointee.received_objects)/Float(git_transfer_progress.pointee.total_objects), status: "Pull Remote Repository")
                }
            }) {
                DispatchQueue.main.async {
                    SVProgressHUD.showSuccess(withStatus: "Done")
                    SVProgressHUD.dismiss(withDelay: 1)
                }
                print("pull success")
                self.passwordEntities = PasswordStore.shared.fetchPasswordEntityCoreData()
                self.tableView.reloadData()
            }
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        passwordEntities = PasswordStore.shared.fetchPasswordEntityCoreData()
        NotificationCenter.default.addObserver(self, selector: #selector(PasswordsTableViewController.actOnPasswordUpdatedNotification), name: NSNotification.Name(rawValue: "passwordUpdated"), object: nil)
        searchController.searchResultsUpdater = self
        searchController.dimsBackgroundDuringPresentation = false
        definesPresentationContext = true
        tableView.tableHeaderView = searchController.searchBar
        tableView.setContentOffset(CGPoint(x: 0, y: 44), animated: false)

    }
    
    func filterContentForSearchText(searchText: String, scope: String = "All") {
        filteredPasswordEntities = passwordEntities!.filter { password in
            return password.name!.lowercased().contains(searchText.lowercased())
        }
        
        tableView.reloadData()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    func actOnPasswordUpdatedNotification() {
        passwordEntities = PasswordStore.shared.fetchPasswordEntityCoreData()
        self.tableView.reloadData()
        print("actOnPasswordUpdatedNotification")
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if searchController.isActive && searchController.searchBar.text != "" {
            return filteredPasswordEntities.count
        }
        return passwordEntities!.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {<commit_msg>don't hide search bar by default<commit_after>//
//  PasswordTableViewController.swift
//  pass
//
//  Created by <NAME> on 18/1/2017.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit
import Result
import SVProgressHUD


class PasswordsTableViewController: UITableViewController {
    private var passwordEntities: [PasswordEntity]?
    var filteredPasswordEntities = [PasswordEntity]()
    let searchController = UISearchController(searchResultsController: nil)
    
    @IBAction func refreshPasswords(_ sender: UIBarButtonItem) {
        SVProgressHUD.setDefaultMaskType(.black)
        SVProgressHUD.show(withStatus: "Pull Remote Repository")
        DispatchQueue.global(qos: .userInitiated).async {
            if PasswordStore.shared.pullRepository(transferProgressBlock: {(git_transfer_progress, stop) in
                DispatchQueue.main.async {
                SVProgressHUD.showProgress(Float(git_transfer_progress.pointee.received_objects)/Float(git_transfer_progress.pointee.total_objects), status: "Pull Remote Repository")
                }
            }) {
                DispatchQueue.main.async {
                    SVProgressHUD.showSuccess(withStatus: "Done")
                    SVProgressHUD.dismiss(withDelay: 1)
                }
                print("pull success")
                self.passwordEntities = PasswordStore.shared.fetchPasswordEntityCoreData()
                self.tableView.reloadData()
            }
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        passwordEntities = PasswordStore.shared.fetchPasswordEntityCoreData()
        NotificationCenter.default.addObserver(self, selector: #selector(PasswordsTableViewController.actOnPasswordUpdatedNotification), name: NSNotification.Name(rawValue: "passwordUpdated"), object: nil)
        searchController.searchResultsUpdater = self
        searchController.dimsBackgroundDuringPresentation = false
        definesPresentationContext = true
        tableView.tableHeaderView = searchController.searchBar
        // tableView.setContentOffset(CGPoint(x: 0, y: 44), animated: false)
    }
    
    func filterContentForSearchText(searchText: String, scope: String = "All") {
        filteredPasswordEntities = passwordEntities!.filter { password in
            return password.name!.lowercased().contains(searchText.lowercased())
        }
        
        tableView.reloadData()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    func actOnPasswordUpdatedNotification() {
        passwordEntities = PasswordStore.shared.fetchPasswordEntityCoreData()
        self.tableView.reloadData()
        print("actOnPasswordUpdatedNotification")
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if searchController.isActive && searchController.searchBar.text != "" {
            return filteredPasswordEntities.count
        }
        return passwordEntities!.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
{'fraction_non_alphanumeric': 0.08366407366248611, 'fraction_numerical': 0.004762660739799968, 'mean_word_length': 2.9722572509457756, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '4905877', 'n_tokens_mistral': 1664, 'n_tokens_neox': 1575, 'n_words': 297}