<commit_before>//
//  RCSTextField.swift
//  shopper
//
//  Created by <NAME> on 3/31/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit

public protocol RCSUnitTextFieldDelegate: RCSTextFieldDelegate {
    func measUnitForTextField(field: RCSUnitTextField) -> Unit
}

public extension RCSUnitTextFieldDelegate {
    func measUnitForTextField() -> Unit {
        return UnitMass.shortTons
    }
}

public class RCSUnitTextField: RCSTextField {

    private var unitString: String = ""
    private var suffixString: String = " "

    public var measUnit: Unit = UnitMass.kilograms {
        didSet {
            zeroMeasurement()
        }
    }

    override public var valueDelegate: AnyObject? {
        get {
            return self.shadowUnitDelegate
        }
        set {
            if let newValueL = newValue as? RCSUnitTextFieldDelegate {
                shadowUnitDelegate = newValueL
            } else {
                shadowUnitDelegate = nil
            }
        }
    }
    private weak var shadowUnitDelegate: RCSUnitTextFieldDelegate?

    public required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }

    public override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

    // MARK: TextField Delegate
    override public func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        if let shadowUnitDelegateL = shadowUnitDelegate {
            measUnit = shadowUnitDelegateL.measUnitForTextField(field: self)
        }

        return super.textFieldShouldBeginEditing(textField)
    }

    override public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange,
                                   replacementString string: String) -> Bool {
        guard let textL = textField.text else {
            return true
        }

        if textL.characters.count == 0 && string.characters.count > 0 {
            textField.text = suffixString
        }

        if range.location + range.length > textL.characters.count - suffixString.characters.count {
            textField.selectedTextRange = pointRangeFromEnd(offset: -suffixString.characters.count)
            return false
        }

        let newString = (textL as NSString).replacingCharacters(in: range, with: string)

        setMeasurement(string:newString)
        return true
    }

    func textFieldDidBeginEditing(_ textField: UITextField) {

        if let textL = text, textL.isEmpty {
            text = suffixString
        }

        textField.selectedTextRange = pointRangeFromEnd(offset: -suffixString.characters.count)
    }<commit_msg>Fixed bug in unit TF<commit_after>//
//  RCSTextField.swift
//  shopper
//
//  Created by <NAME> on 3/31/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit

public protocol RCSUnitTextFieldDelegate: RCSTextFieldDelegate {
    func measUnitForTextField(field: RCSUnitTextField) -> Unit
}

public extension RCSUnitTextFieldDelegate {
    func measUnitForTextField() -> Unit {
        return UnitMass.shortTons
    }
}

public class RCSUnitTextField: RCSTextField {

    private var unitString: String = ""
    private var suffixString: String = " "

    public var measUnit: Unit = UnitMass.kilograms {
        didSet {
            zeroMeasurement()
        }
    }

    override public var valueDelegate: AnyObject? {
        get {
            return self.shadowUnitDelegate
        }
        set {
            if let newValueL = newValue as? RCSUnitTextFieldDelegate {
                shadowUnitDelegate = newValueL
            } else {
                shadowUnitDelegate = nil
            }
        }
    }
    private weak var shadowUnitDelegate: RCSUnitTextFieldDelegate?

    public required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }

    public override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

    // MARK: TextField Delegate
    override public func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        if let shadowUnitDelegateL = shadowUnitDelegate {
            measUnit = shadowUnitDelegateL.measUnitForTextField(field: self)
        }

        return super.textFieldShouldBeginEditing(textField)
    }

    override public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange,
                                   replacementString string: String) -> Bool {
        guard let textL = textField.text else {
            return true
        }

        if textL.characters.count == 0 && string.characters.count > 0 {
            textField.text = string + suffixString
            setMeasurement(string: string + suffixString)
        }

        if range.location + range.length > textL.characters.count - suffixString.characters.count {
            textField.selectedTextRange = pointRangeFromEnd(offset: -suffixString.characters.count)
            return false
        }

        let newString = (textL as NSString).replacingCharacters(in: range, with: string)

        setMeasurement(string:newString)
        return true
    }


    func textFieldDidBeginEditing(_ textField: UITextField) {

        if let textL = text, textL.isEmpty {
            text = suffixString
        }

        textField.selectedTextRange = pointRangeFromEnd(offset: -suffixString.characters.count)
    }
{'fraction_non_alphanumeric': 0.07669562003326558, 'fraction_numerical': 0.004065791905377934, 'mean_word_length': 2.2602409638554217, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7477719', 'n_tokens_mistral': 1507, 'n_tokens_neox': 1426, 'n_words': 354}