<commit_before>//
//  SBTimeLabel.swift
//  SBTimeLabel
//
//  Created by <NAME> on 10/18/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import UIKit

enum TimerType {
    case stopwatch
    case countdown
    case clock
}
enum TimerAccurracy {
    case milliseconds
    case seconds
}

class SBTimeLabel: UILabel {
    
    var type:TimerType = .stopwatch
    var accurracy:TimerAccurracy = .seconds
    static var dateFormat = "HH:mm:ss"
    
    private var timer:Timer?
    private var startDate:Date?
    private var endDate:Date?
    private var elapsedTime:TimeInterval = 0
    
    private func timeInterval() -> TimeInterval {
        switch accurracy {
            case .seconds:
                return 1.0
            case .milliseconds:
                return 0.01
        }
    }
    
    private lazy var dateFormatter:DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = dateFormat
        formatter.timeZone = TimeZone(secondsFromGMT: 0)
        return formatter
    }()
    
    private func updateText() {
        var string:String!
        switch type {
            case .stopwatch:
                string = dateFormatter.string(from: self.elapsedTimeAsDate())
            case .countdown:
                string = dateFormatter.string(from: self.elapsedTimeAsDate())
            case .clock:
                string = dateFormatter.string(from: Date())
        }
        text = string
    }
    
    private func elapsedTimeAsDate() -> Date {
        return Date(timeIntervalSince1970: elapsedTime)
    }
    
    private func incrementTime() {
        self.elapsedTime += self.timeInterval()
        self.updateText()
    }
    
    //MARK: - States
    var isRunning: Bool {
        get {
            if let timer = self.timer {
                return timer.isValid
            } else {
                return false
            }
        }
    }
    
    //MARK: - Controls
    func start() {
        timer?.invalidate()
        
        timer = Timer.scheduledTimer(withTimeInterval: self.timeInterval(), repeats: true, block: { (timer) in
            self.incrementTime()
        })
        // Set Tolerance as 10% of timer, as suggested by Apple Docs
        timer?.tolerance = self.timeInterval() / 10.0
        
        startDate = Date()
    }
    
    func pause() {
        timer?.invalidate()
    }
}<commit_msg>Code cleanup for Stopwatch<commit_after>//
//  SBTimeLabel.swift
//  SBTimeLabel
//
//  Created by <NAME> on 10/18/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import UIKit

enum TimerType {
    case stopwatch
    case countdown
    case clock
}
enum TimerAccurracy {
    case milliseconds
    case seconds
}

class SBTimeLabel: UILabel {
    
    var type:TimerType = .stopwatch
    var accurracy:TimerAccurracy = .seconds
    static var dateFormat = "HH:mm:ss"
    
    private var timer:Timer?
    private var startDate:Date?
    private var endDate:Date?
    private var elapsedTime:TimeInterval = 0
    
    private lazy var dateFormatter:DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = dateFormat
        formatter.timeZone = TimeZone(secondsFromGMT: 0)
        return formatter
    }()
    
    private func updateText() {
        var string:String!
        switch type {
            case .stopwatch:
                string = dateFormatter.string(from: self.elapsedTimeAsDate)
            case .countdown:
                string = dateFormatter.string(from: self.elapsedTimeAsDate)
            case .clock:
                string = dateFormatter.string(from: Date())
        }
        text = string
    }
    
    private func incrementTime() {
        self.elapsedTime += self.timeInterval
        self.updateText()
    }
    
    //MARK: - Dynamic Properties
    var isRunning: Bool {
        get {
            if let timer = self.timer {
                return timer.isValid
            } else {
                return false
            }
        }
    }
    
    var elapsedTimeAsDate: Date {
        get {
            return Date(timeIntervalSince1970: elapsedTime)
        }
    }
    
    private var timeInterval: TimeInterval {
        switch accurracy {
            case .seconds:
                return 1.0
            case .milliseconds:
                return 0.01
        }
    }
    
    //MARK: - Controls
    func start() {
        timer?.invalidate()
        
        timer = Timer.scheduledTimer(withTimeInterval: self.timeInterval, repeats: true, block: { (timer) in
            self.incrementTime()
        })
        // Set Tolerance to 10% of timer, as suggested by Apple Docs
        timer?.tolerance = self.timeInterval / 10.0
        
        startDate = Date()
    }
    
    func pause() {
        timer?.invalidate()
    }
}
{'fraction_non_alphanumeric': 0.08619612742036227, 'fraction_numerical': 0.010826566729127629, 'mean_word_length': 1.6852990497484628, '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': '3735349', 'n_tokens_mistral': 1401, 'n_tokens_neox': 1316, 'n_words': 344}