<commit_before>//
//  TouchView.swift
//  Pods
//
//  Created by <NAME> on 2015/01/27.
//
//

import UIKit

final public class TouchView: UIImageView {
    
    // MARK: - Properties
    
    weak var touch: UITouch?
    private var _config: TouchVisualizerConfig
    
    public var config: TouchVisualizerConfig{
        get{ return _config }
        set(value) {
            _config = value
            self.image = self.config.image
            self.image = self.image?.imageWithRenderingMode(.AlwaysTemplate)
            self.tintColor = self.config.color
            self.timerLabel.textColor = self.config.color
        }
    }
    
    private var previousRatio: CGFloat = 1.0
    private var startDate: NSDate?
    private weak var timer: NSTimer?
    private var lastTimeString: String!
    
    lazy var timerLabel: UILabel = {
        let size = CGSizeMake(200.0, 44.0)
        let bottom = 8.0 as CGFloat
        var label:UILabel = UILabel(frame: CGRect(
            x: -(size.width - CGRectGetWidth(self.frame)) / 2,
            y: -size.height - bottom,
            width: size.width,
            height: size.height
            )
        )
        label.font = UIFont(name: "Helvetica", size: 24.0)
        label.textAlignment = .Center
        self.addSubview(label)
        return label
    }()
    
    // MARK: - Life cycle
    
    convenience init() {
        self.init(frame:CGRectZero)
    }
    
    override init(frame: CGRect) {
        self._config = TouchVisualizerConfig()
        super.init(frame: frame)
        self.frame = CGRect(origin: CGPoint(x: 0.0, y: 0.0), size: self._config.defaultSize)
    }

    public required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        self.timer?.invalidate()
    }
    
    func beginTouch() {
        self.alpha = 1.0
        self.layer.transform = CATransform3DIdentity
        self.previousRatio = 1.0
        self.frame = CGRect(origin: self.frame.origin, size: self._config.defaultSize)
        self.startDate = NSDate()
        
        if self._config.showsTimer {
            self.timer = NSTimer.scheduledTimerWithTimeInterval(1.0 / 60.0, target: self, selector: "update:", userInfo: nil, repeats: true)
            NSRunLoop.mainRunLoop().addTimer(self.timer!, forMode: NSRunLoopCommonModes)
        }
        
        if self._config.showsTouchRadius {
            self.updateSize()
        }
    }
    
    func endTouch() {
        self.timer?.invalidate()
    }
    
    internal func update(timer: NSTimer) {
        if let startDate = self.startDate {
            let interval = NSDate().timeIntervalSinceDate(startDate)
            var timeString = "\(interval)"
            let range = "\(interval)".rangeOfString(".")
            if let range = range {
                let r = advance(range.startIndex, 3)
                timeString = timeString.substringToIndex(advance(range.startIndex, 3))
                
                self.timerLabel.text = timeString<commit_msg>Fix bug that timer doesn't disappear and radius doesn't update without timer<commit_after>//
//  TouchView.swift
//  Pods
//
//  Created by <NAME> on 2015/01/27.
//
//

import UIKit

final public class TouchView: UIImageView {
    
    // MARK: - Properties
    
    weak var touch: UITouch?
    private var _config: TouchVisualizerConfig
    
    public var config: TouchVisualizerConfig{
        get{ return _config }
        set(value) {
            _config = value
            self.image = self.config.image
            self.image = self.image?.imageWithRenderingMode(.AlwaysTemplate)
            self.tintColor = self.config.color
            self.timerLabel.textColor = self.config.color
        }
    }
    
    private var previousRatio: CGFloat = 1.0
    private var startDate: NSDate?
    private weak var timer: NSTimer?
    private var lastTimeString: String!
    
    lazy var timerLabel: UILabel = {
        let size = CGSizeMake(200.0, 44.0)
        let bottom = 8.0 as CGFloat
        var label:UILabel = UILabel(frame: CGRect(
            x: -(size.width - CGRectGetWidth(self.frame)) / 2,
            y: -size.height - bottom,
            width: size.width,
            height: size.height
            )
        )
        label.font = UIFont(name: "Helvetica", size: 24.0)
        label.textAlignment = .Center
        self.addSubview(label)
        return label
    }()
    
    // MARK: - Life cycle
    
    convenience init() {
        self.init(frame:CGRectZero)
    }
    
    override init(frame: CGRect) {
        self._config = TouchVisualizerConfig()
        super.init(frame: frame)
        self.frame = CGRect(origin: CGPoint(x: 0.0, y: 0.0), size: self._config.defaultSize)
    }

    public required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        self.timer?.invalidate()
    }
    
    func beginTouch() {
        self.alpha = 1.0
        self.timerLabel.alpha = 0.0
        self.layer.transform = CATransform3DIdentity
        self.previousRatio = 1.0
        self.frame = CGRect(origin: self.frame.origin, size: self._config.defaultSize)
        self.startDate = NSDate()
        
        self.timer = NSTimer.scheduledTimerWithTimeInterval(1.0 / 60.0, target: self, selector: "update:", userInfo: nil, repeats: true)
        NSRunLoop.mainRunLoop().addTimer(self.timer!, forMode: NSRunLoopCommonModes)
        
        if self._config.showsTimer {
            self.timerLabel.alpha = 1.0
        }
        
        if self._config.showsTouchRadius {
            self.updateSize()
        }
    }
    
    func endTouch() {
        self.timer?.invalidate()
    }
    
    internal func update(timer: NSTimer) {
        if let startDate = self.startDate {
            let interval = NSDate().timeIntervalSinceDate(startDate)
            var timeString = "\(interval)"
            let range = "\(interval)".rangeOfString(".")
            if let range = range {
                let r = advance(range.startIndex, 3)
                timeString = timeString.substringToIndex(advance(range.startIndex, 3))
                
                self.timerLabel.text = timeString
{'fraction_non_alphanumeric': 0.10331509494689411, 'fraction_numerical': 0.013196009011908594, 'mean_word_length': 1.8974358974358974, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '744707', 'n_tokens_mistral': 1922, 'n_tokens_neox': 1839, 'n_words': 412}