<commit_before>//
//  NGOPassCodeView.swift
//  andGo
//
//  Created by <NAME> on 29.06.15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Foundation

public protocol NGOPassCodeViewDelegate {
    func NGOPassCodeViewDidFinishEnteringPassword(password: String)
}

public class NGOPassCodeView: UIView, UITextFieldDelegate {
    
    public var delegate: NGOPassCodeViewDelegate?
    public var borderColor: UIColor = UIColor.whiteColor() {
        didSet {
            self.setupScrollView()
        }
    }
    public var borderWidth: CGFloat = 1 {
        didSet {
            self.setupScrollView()
        }
    }
    public var numberOfDigits: Int = 6 {
        didSet {
            self.setupScrollView()
        }
    }
    public var circleDiameter: CGFloat = 24 {
        didSet {
            self.setupScrollView()
        }
    }
    public var keyboardAppearance: UIKeyboardAppearance = UIKeyboardAppearance.Dark {
        didSet {
            self.setupTextField()
        }
    }
    
    private var scrollView: UIScrollView = UIScrollView()
    private var textField: UITextField = UITextField()
    
    //MARK: - View Cycle
    
    public required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    public override func awakeFromNib() {
        super.awakeFromNib()
        self.setupScrollView()
        self.setupTextField()
    }
    
    public override func layoutSubviews() {
        super.layoutSubviews()
        self.setupScrollView()
    }
    
    //MARK: - Setup UI
    
    private func setupScrollView() {
        
        self.scrollView.removeFromSuperview()
        self.scrollView = UIScrollView(frame: self.bounds)
        self.addSubview(self.scrollView)
        self.scrollView.subviews.map{ $0.removeFromSuperview() }
        
        let viewWidth       = CGRectGetWidth(self.bounds) / CGFloat(self.numberOfDigits)
        let passCharacters  = count(self.textField.text)
        
        for (index, element) in enumerate(0...self.numberOfDigits - 1) {
            
            let frame   = CGRectMake(CGFloat(index) * viewWidth, 0, viewWidth, CGRectGetHeight(self.bounds))
            var view    = UIView(frame: frame)
            view.backgroundColor = UIColor.clearColor()
            
            if index < passCharacters {
                view.layer.addSublayer(self.circleInFrame(frame, isFilled: true))
            }
            else {
                view.layer.addSublayer(self.circleInFrame(frame, isFilled: false))
            }
            self.scrollView.addSubview(view)<commit_msg>Fixed a bug when the view couldn't become first responder again.<commit_after>//
//  NGOPassCodeView.swift
//  andGo
//
//  Created by <NAME> on 29.06.15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Foundation

public protocol NGOPassCodeViewDelegate {
    func NGOPassCodeViewDidFinishEnteringPassword(password: String)
}

public class NGOPassCodeView: UIView, UITextFieldDelegate {
    
    public var delegate: NGOPassCodeViewDelegate?
    public var borderColor: UIColor = UIColor.whiteColor() {
        didSet {
            self.setupScrollView()
        }
    }
    public var borderWidth: CGFloat = 1 {
        didSet {
            self.setupScrollView()
        }
    }
    public var numberOfDigits: Int = 6 {
        didSet {
            self.setupScrollView()
        }
    }
    public var circleDiameter: CGFloat = 24 {
        didSet {
            self.setupScrollView()
        }
    }
    public var keyboardAppearance: UIKeyboardAppearance = UIKeyboardAppearance.Dark {
        didSet {
            self.setupTextField()
        }
    }
    
    private var scrollView: UIScrollView = UIScrollView()
    private var textField: UITextField = UITextField()
    
    //MARK: - View Cycle
    
    public required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    public override func awakeFromNib() {
        super.awakeFromNib()
        self.setupScrollView()
        self.setupTextField()
    }
    
    public override func layoutSubviews() {
        super.layoutSubviews()
        self.setupScrollView()
    }
    
    //MARK: - Setup UI
    
    private func setupScrollView() {
        
        self.scrollView.removeFromSuperview()
        self.scrollView = UIScrollView(frame: self.bounds)
        self.addSubview(self.scrollView)
        self.scrollView.userInteractionEnabled = false
        
        let viewWidth       = CGRectGetWidth(self.bounds) / CGFloat(self.numberOfDigits)
        let passCharacters  = count(self.textField.text)
        
        for (index, element) in enumerate(0...self.numberOfDigits - 1) {
            
            let frame   = CGRectMake(CGFloat(index) * viewWidth, 0, viewWidth, CGRectGetHeight(self.bounds))
            var view    = UIView(frame: frame)
            view.userInteractionEnabled = false
            view.backgroundColor = UIColor.clearColor()
            
            if index < passCharacters {
                view.layer.addSublayer(self.circleInFrame(frame, isFilled: true))
            }
            else {
                view.layer.addSublayer(self.circleInFrame(frame, isFilled: false))
            }
            self.scrollView.addSubview(view)
{'fraction_non_alphanumeric': 0.08895423635361147, 'fraction_numerical': 0.006432641058628928, 'mean_word_length': 2.0266963292547273, 'pattern_counts': {'":': 0, '<': 9, '<?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': '5623908', 'n_tokens_mistral': 1632, 'n_tokens_neox': 1585, 'n_words': 323}