<commit_before>//
//  PasscodeLockViewController.swift
//  PasscodeLock
//
//  Created by <NAME> on 8/28/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import UIKit
import LocalAuthentication

open class PasscodeLockViewController: UIViewController {
    
    open var dismissCompletionCallback: (()->Void)?
    open var successCallback: (()->Void)?
    open var cancelCallback: (()->Void)?
    lazy var enterPasscodeAlert: UIAlertController = {
        let enterPasscodeAlert = UIAlertController(title: "Authenticate Pass", message: "Unlock with passcode for Pass", preferredStyle: .alert)

        enterPasscodeAlert.addTextField(configurationHandler: {(_ textField: UITextField) -> Void in
            textField.placeholder = "passcode"
            textField.isSecureTextEntry = true
            textField.addTarget(self, action: #selector(self.passcodeTextFieldDidChange(_:)), for: UIControlEvents.editingChanged)
            textField.clearButtonMode = UITextFieldViewMode.whileEditing
            textField.becomeFirstResponder()
        })
        
        let myContext = LAContext()
        var authError: NSError?
        if #available(iOS 8.0, macOS 10.12.1, *) {
            if myContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) {
                var biometryType = "Touch ID"
                if #available(iOS 11.0, *) {
                    if myContext.biometryType == LABiometryType.faceID {
                        biometryType = "Face ID"
                    }
                }
                let bioAction = UIAlertAction(title: "Use " + biometryType, style: .default) { (action:UIAlertAction) -> Void in
                    self.authenticate()
                }
                enterPasscodeAlert.addAction(bioAction)
            }
        }
        
        return enterPasscodeAlert
    }()
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
    }
    
    open override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        authenticate()
    }

    internal func dismissPasscodeLock(completionHandler: (() -> Void)? = nil) {
        // clean up the textfield
        enterPasscodeAlert.textFields?[0].text = ""
        if presentingViewController?.presentedViewController == self {
            // if presented as modal
            dismiss(animated: true, completion: { [weak self] in
                self?.dismissCompletionCallback?()
                completionHandler?()
            })
        // if pushed in a navigation controller
        } else {
            _ = navigationController?.popViewController(animated: true)
            dismissCompletionCallback?()
            completionHandler?()
        }
    }

    // MARK: - PasscodeLockDelegate

    open func passcodeLockDidSucceed() {
        dismissPasscodeLock(completionHandler: successCallback)
    }
    
    open func passcodeLockDidCancel() {
        dismissPasscodeLock(completionHandler: cancelCallback)
    }
    
    public func authenticate() {
        print(enterPasscodeAlert.isBeingPresented)
        
        let myContext = LAContext()
        let myLocalizedReasonString = "Authentication is needed to access Pass."
        var authError: NSError?
        
        if #available(iOS 8.0, *) {
            if myContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) {
                myContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: myLocalizedReasonString) { success, evaluateError in
                    if success {
                        DispatchQueue.main.async {
                            // user authenticated successfully, take appropriate action
                            self.passcodeLockDidSucceed()
                        }<commit_msg>Dismiss passcode alert after unlock<commit_after>//
//  PasscodeLockViewController.swift
//  PasscodeLock
//
//  Created by <NAME> on 8/28/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import UIKit
import LocalAuthentication

open class PasscodeLockViewController: UIViewController {
    
    open var dismissCompletionCallback: (()->Void)?
    open var successCallback: (()->Void)?
    open var cancelCallback: (()->Void)?
    lazy var enterPasscodeAlert: UIAlertController = {
        let enterPasscodeAlert = UIAlertController(title: "Authenticate Pass", message: "Unlock with passcode for Pass", preferredStyle: .alert)

        enterPasscodeAlert.addTextField(configurationHandler: {(_ textField: UITextField) -> Void in
            textField.placeholder = "passcode"
            textField.isSecureTextEntry = true
            textField.addTarget(self, action: #selector(self.passcodeTextFieldDidChange(_:)), for: UIControlEvents.editingChanged)
            textField.clearButtonMode = UITextFieldViewMode.whileEditing
            textField.becomeFirstResponder()
        })
        
        let myContext = LAContext()
        var authError: NSError?
        if #available(iOS 8.0, macOS 10.12.1, *) {
            if myContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) {
                var biometryType = "Touch ID"
                if #available(iOS 11.0, *) {
                    if myContext.biometryType == LABiometryType.faceID {
                        biometryType = "Face ID"
                    }
                }
                let bioAction = UIAlertAction(title: "Use " + biometryType, style: .default) { (action:UIAlertAction) -> Void in
                    self.authenticate()
                }
                enterPasscodeAlert.addAction(bioAction)
            }
        }
        
        return enterPasscodeAlert
    }()
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
    }
    
    open override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        authenticate()
    }

    internal func dismissPasscodeLock(completionHandler: (() -> Void)? = nil) {
        // clean up the textfield
        DispatchQueue.main.async {
            self.enterPasscodeAlert.textFields?[0].text = ""
            self.enterPasscodeAlert.dismiss(animated: false, completion: nil)
        }
        
        // pop
        if presentingViewController?.presentedViewController == self {
            // if presented as modal
            dismiss(animated: true, completion: { [weak self] in
                self?.dismissCompletionCallback?()
                completionHandler?()
            })
        } else {
            // if pushed in a navigation controller
            _ = navigationController?.popViewController(animated: true)
            dismissCompletionCallback?()
            completionHandler?()
        }
    }

    // MARK: - PasscodeLockDelegate

    open func passcodeLockDidSucceed() {
        dismissPasscodeLock(completionHandler: successCallback)
    }
    
    open func passcodeLockDidCancel() {
        dismissPasscodeLock(completionHandler: cancelCallback)
    }
    
    public func authenticate() {
        let myContext = LAContext()
        let myLocalizedReasonString = "Authentication is needed to access Pass."
        var authError: NSError?
        
        if #available(iOS 8.0, *) {
            if myContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError) {
                myContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: myLocalizedReasonString) { success, evaluateError in
                    if success {
                        DispatchQueue.main.async {
                            // user authenticated successfully, take appropriate action
                            self.passcodeLockDidSucceed()
                        }
{'fraction_non_alphanumeric': 0.08557985757884029, 'fraction_numerical': 0.005595116988809766, 'mean_word_length': 2.129725427775567, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, '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': '3221664', 'n_tokens_mistral': 2064, 'n_tokens_neox': 1929, 'n_words': 439}