<commit_before>//
//  FormViewTextFieldTableViewCell.swift
//  topik-ios
//
//  Created by <NAME> on 31/05/2015.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import UIKit

let kPadding: CGFloat = 10

public class FormViewTextFieldCell: FormViewCell {

    public var label = UILabel()
    public var textField = UITextField()
    
    override public func drawRect(rect: CGRect) {
        super.drawRect(rect)
        
        label.setTranslatesAutoresizingMaskIntoConstraints(false)
        contentView.addSubview(label)
        
        textField.setTranslatesAutoresizingMaskIntoConstraints(false)
        contentView.addSubview(textField)
        textField.textAlignment = NSTextAlignment.Right
        
        setupLabelConstraints()
        setupTextFieldConstraints()
    
        if configuation.formCellType == FormCellType.TextFieldCurrency {
            
            textField.keyboardType = UIKeyboardType.DecimalPad
        }
        
        textField.delegate = self
    }

    // MARK: - Constraints
    
    public func setupLabelConstraints() {
        
        label.addTopConstraint(toView: contentView, relation: .Equal, constant: 0)
        label.addLeftConstraint(toView: contentView, relation: .Equal, constant: kPadding)
        label.addBottomConstraint(toView: contentView, relation: .Equal, constant: 0)
        label.addWidthConstraint(relation: .Equal, constant: 160)
    }
    
    public func setupTextFieldConstraints() {
        
        textField.addTopConstraint(toView: contentView, relation: .Equal, constant: 0)
        textField.addLeftConstraint(toView: label, attribute: NSLayoutAttribute.Right, relation: .Equal, constant: 0)
        textField.addRightConstraint(toView: contentView, relation: .Equal, constant: -kPadding)
        textField.addBottomConstraint(toView: contentView, relation: .Equal, constant: 0)
    }

}


extension FormViewTextFieldCell: UITextFieldDelegate {
    
    public func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        
        if configuation.formCellType == FormCellType.TextFieldCurrency {
            
            // Construct the text that will be in the field if this change is accepted
            var oldText = textField.text as NSString
            var newText = oldText.stringByReplacingCharactersInRange(range, withString: string) as NSString!
            var newTextString = String(newText)
            
            let digits = NSCharacterSet.decimalDigitCharacterSet()
            var digitText = ""
            for c in newTextString.unicodeScalars {
                if digits.longCharacterIsMember(c.value) {
                    digitText.append(c)
                }
            }
            
            let formatter = NSNumberFormatter()
            formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
            formatter.locale = configuation.currencyLocale
            var numberFromField = (NSString(string: digitText).doubleValue)/100
            newText = formatter.stringFromNumber(numberFromField)
            
            textField.text = String(newText)
            delegate?.valueDidChange(configuation.identifier, value: numberFromField)
            
            return false
        }
        
        else {
            
            delegate?.valueDidChange(configuation.identifier, value: string)
        }
        
        return true
    }
}<commit_msg>skdfl<commit_after>//
//  FormViewTextFieldTableViewCell.swift
//  topik-ios
//
//  Created by <NAME> on 31/05/2015.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import UIKit

let kPadding: CGFloat = 5

public class FormViewTextFieldCell: FormViewCell {

    public var label = UILabel()
    public var textField = UITextField()
    
    override public func drawRect(rect: CGRect) {
        super.drawRect(rect)
        
        label.setTranslatesAutoresizingMaskIntoConstraints(false)
        contentView.addSubview(label)
        
        textField.setTranslatesAutoresizingMaskIntoConstraints(false)
        contentView.addSubview(textField)
        textField.textAlignment = NSTextAlignment.Right
        
        setupLabelConstraints()
        setupTextFieldConstraints()
    
        if configuation.formCellType == FormCellType.TextField {
            
            textField.addTarget(self, action: "textFieldChanged:", forControlEvents: UIControlEvents.EditingChanged)
        }
        if configuation.formCellType == FormCellType.TextFieldCurrency {
            
            textField.keyboardType = UIKeyboardType.DecimalPad
            textField.delegate = self
        }
    }
    
    public func textFieldChanged(textField: UITextField) {
        
        delegate?.valueDidChange(configuation.identifier, value: textField.text)
    }

    // MARK: - Constraints
    
    public func setupLabelConstraints() {
        
        label.addTopConstraint(toView: contentView, relation: .Equal, constant: 0)
        label.addLeftConstraint(toView: contentView, relation: .Equal, constant: kPadding)
        label.addBottomConstraint(toView: contentView, relation: .Equal, constant: 0)
        label.addWidthConstraint(relation: .Equal, constant: 160)
    }
    
    public func setupTextFieldConstraints() {
        
        textField.addTopConstraint(toView: contentView, relation: .Equal, constant: 0)
        textField.addLeftConstraint(toView: label, attribute: NSLayoutAttribute.Right, relation: .Equal, constant: 0)
        textField.addRightConstraint(toView: contentView, relation: .Equal, constant: -kPadding)
        textField.addBottomConstraint(toView: contentView, relation: .Equal, constant: 0)
    }

}


extension FormViewTextFieldCell: UITextFieldDelegate {
    
    public func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        
        if configuation.formCellType == FormCellType.TextFieldCurrency {
            
            // Construct the text that will be in the field if this change is accepted
            var oldText = textField.text as NSString
            var newText = oldText.stringByReplacingCharactersInRange(range, withString: string) as NSString!
            var newTextString = String(newText)
            
            let digits = NSCharacterSet.decimalDigitCharacterSet()
            var digitText = ""
            for c in newTextString.unicodeScalars {
                if digits.longCharacterIsMember(c.value) {
                    digitText.append(c)
                }
            }
            
            let formatter = NSNumberFormatter()
            formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
            formatter.locale = configuation.currencyLocale
            var numberFromField = (NSString(string: digitText).doubleValue)/100
            newText = formatter.stringFromNumber(numberFromField)
            
            textField.text = String(newText)
            delegate?.valueDidChange(configuation.identifier, value: numberFromField)
            
            return false
        }
        
        return true
    }
}
{'fraction_non_alphanumeric': 0.07522679692951849, 'fraction_numerical': 0.0068387997208653175, 'mean_word_length': 2.3994307400379506, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '3099926', 'n_tokens_mistral': 1900, 'n_tokens_neox': 1819, 'n_words': 399}