<commit_before>//
//  SHSFlagAccessoryView.swift
//  SHSPhoneComponentSwift
//
//  Created by <NAME> on 12/09/2017.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Foundation<commit_msg>SHSFlagAccessoryView implemented<commit_after>//
//  SHSFlagAccessoryView.swift
//  SHSPhoneComponentSwift
//
//  Created by <NAME> on 12/09/2017.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit

/**
 Accessory view that shows flag images.
 */
class SHSFlagAccessoryView: UIView {
    
    private enum Sizes {
        static let iconSize: CGFloat       = 18
        static let minShift: CGFloat       = 5
        static let fontCorrection: CGFloat = 1
    }
    private var imageView: UIImageView?
    
    // MARK: - Life cycle
    init(with textField: UITextField) {
        super.init(frame: .zero)

        let fieldRect = textField.textRect(forBounds: textField.bounds)
        self.frame = CGRect(x: 0, y: 0,
                            width: self.viewWidth(for: fieldRect),
                            height: textField.frame.size.height)
        
        imageView = UIImageView(frame:
            CGRect(x: self.leftShift(for: fieldRect),
                   y: fieldRect.origin.y + (fieldRect.size.height - Sizes.iconSize)/2,
                   width: Sizes.iconSize, height: Sizes.iconSize))
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func set(image: UIImage) {
        imageView?.image = image
    }
    
    // MARK: - Internal
    private func leftShift(for textFieldRect: CGRect) -> CGFloat {
        let minX = textFieldRect.minX
        let result = minX < Sizes.minShift ? Sizes.minShift : minX
        return result + Sizes.fontCorrection
    }
    
    private func viewWidth(for textFieldRect: CGRect) -> CGFloat {
        let minX = textFieldRect.minX
        if minX < Sizes.minShift {
            return Sizes.minShift + Sizes.iconSize + Sizes.minShift - minX
        } else {
            return minX + Sizes.iconSize
        }
    }
}
{'fraction_non_alphanumeric': 0.10014513788098693, 'fraction_numerical': 0.014997581035316884, 'mean_word_length': 2.0456553755522826, 'pattern_counts': {'":': 0, '<': 9, '<?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': '6535628', 'n_tokens_mistral': 672, 'n_tokens_neox': 631, 'n_words': 155}