Skip to content

Commit

Permalink
⚓️:: [#22] Stopwatch 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
jjunhaa0211 committed Dec 28, 2023
1 parent 2e8caa7 commit 9a3956a
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions Sources/MindGymKit /Stopwatch.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// File.swift
//
//
// Created by 박준하 on 12/28/23.
//

import UIKit
import RxSwift

// MARK: - Stopwatch
public protocol TimerControl {
func start()
func stop()
func reset()
func record()
}

public class Stopwatch: NSObject, TimerControl {
private var counter: Double = 0.0
private var timer: Timer?
private var lapTimes: [Double] = []
private let timeSubject = PublishSubject<String>()
private let recordSubject = PublishSubject<[String]>()

public var timeUpdate: Observable<String> {
return timeSubject.asObservable()
}

public var recordUpdate: Observable<[String]> {
return recordSubject.asObservable()
}

public func start() {
timer = Timer.scheduledTimer(withTimeInterval: 0.035, repeats: true) { [weak self] _ in
guard let self = self else { return }
self.counter += 0.035
let timeString = self.timeString(from: self.counter)
self.timeSubject.onNext(timeString)
}
}

public func stop() {
timer?.invalidate()
timer = nil
}

public func reset() {
counter = 0
lapTimes.removeAll()
}

public func record() {
lapTimes.append(counter)
let lapTimesString = lapTimes.map { timeString(from: $0) }
recordSubject.onNext(lapTimesString)
}

private func timeString(from counter: Double) -> String {
let minutes: String = String(format: "%02d", Int(counter / 60))
let seconds: String = String(format: "%02d", Int(counter.truncatingRemainder(dividingBy: 60)))
let milliseconds: String = String(format: "%02d", Int((counter * 100).truncatingRemainder(dividingBy: 100)))
return "\(minutes):\(seconds).\(milliseconds)"
}
}

0 comments on commit 9a3956a

Please sign in to comment.