diff --git a/README.md b/README.md index db43f49..176718b 100644 --- a/README.md +++ b/README.md @@ -75,11 +75,22 @@ https://github.com/MaeumGajim/MindGymKit.git ``` - **Using a stopwatch**: ```swift - let stopWatchObject = MindGymStopWatchKit() - - // functions - stopWatchObject.startTimer(label: stopWatchLabel) - stopWatchObject.stopTimer() - stopWatchObject.recordTime() - stopWatchObject.resetTimer(label: stopWatchLabel) + + let stopwatch = MindGymStopWatchKit() + + stopwatch.mainStopwatch.timeUpdate + .observe(on: MainScheduler.instance) + .subscribe(onNext: { [weak self] timeString in + self?.stopWatchLabel.text = timeString + }) + .disposed(by: disposeBag) + + stopwatch.mainStopwatch.recordUpdate + .observe(on: MainScheduler.instance) + .subscribe(onNext: { [weak self] lapTimes in + self?.lapRecords = lapTimes + self?.lapRecords.append(lapTimes.last ?? "") + print("Lap record : \(String(describing: self?.lapRecords ?? nil))") + }) + .disposed(by: disposeBag) ``` diff --git a/Sources/MindGymKit /MindGymKit.swift b/Sources/MindGymKit /MindGymKit.swift index f32da16..e64c080 100644 --- a/Sources/MindGymKit /MindGymKit.swift +++ b/Sources/MindGymKit /MindGymKit.swift @@ -43,32 +43,29 @@ public class MindGymKit { } public class Stopwatch: NSObject { - var counter: Double - var timer: Timer? - var lapTimes: [Double] = [] - + private var counter: Double = 0.0 + private var timer: Timer? + private var lapTimes: [Double] = [] + private let timeSubject = PublishSubject() + private let recordSubject = PublishSubject<[String]>() + + public var timeUpdate: Observable { + return timeSubject.asObservable() + } - override init() { - counter = 0.0 - timer = nil + public var recordUpdate: Observable<[String]> { + return recordSubject.asObservable() } - func start(label: UILabel) { + func start() { timer = Timer.scheduledTimer(withTimeInterval: 0.035, repeats: true) { [weak self] _ in guard let self = self else { return } self.counter += 0.035 - let minutes: String = String(format: "%02d", Int(self.counter / 60)) - let seconds: String = String(format: "%02d", Int(self.counter.truncatingRemainder(dividingBy: 60))) - let milliseconds: String = String(format: "%02d", Int((self.counter * 100).truncatingRemainder(dividingBy: 100))) - let timeString = "\(minutes):\(seconds).\(milliseconds)" - - DispatchQueue.main.async { - label.text = timeString - } + let timeString = self.timeString(from: self.counter) + self.timeSubject.onNext(timeString) } } - func stop() { timer?.invalidate() timer = nil @@ -76,59 +73,41 @@ public class Stopwatch: NSObject { func reset() { counter = 0 + lapTimes.removeAll() + } + + func record() { + lapTimes.append(counter) + let lapTimesString = lapTimes.map { timeString(from: $0) } + recordSubject.onNext(lapTimesString) } - @objc private func updateCounter() { - counter += 0.035 + 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)" } } public class MindGymStopWatchKit { - public init () {} - public let mainStopwatch: Stopwatch = Stopwatch() - public func startTimer(label: UILabel) { - mainStopwatch.start(label: label) + public func startTimer() { + mainStopwatch.start() } public func stopTimer() { mainStopwatch.stop() } - public func resetTimer(label: UILabel) { + public func resetTimer() { mainStopwatch.reset() - let minutes: String = String(format: "%02d", Int(mainStopwatch.counter / 60)) - let seconds: String = String(format: "%02d", Int(mainStopwatch.counter.truncatingRemainder(dividingBy: 60))) - let milliseconds: String = String(format: "%02d", Int((mainStopwatch.counter * 100).truncatingRemainder(dividingBy: 100))) - let timeString = "\(minutes):\(seconds).\(milliseconds)" - - DispatchQueue.main.async { [weak label] in - label?.text = timeString - } - resetRecord() - } - - public func resetRecord() { - mainStopwatch.lapTimes.removeAll() } public func recordTime() { - mainStopwatch.lapTimes.append(mainStopwatch.counter) - printLapTimes() - } - - public func printLapTimes() { - print("Lap Times:") - for (index, lapTime) in mainStopwatch.lapTimes.enumerated() { - let minutes: String = String(format: "%02d", Int(lapTime / 60)) - let seconds: String = String(format: "%02d", Int(lapTime.truncatingRemainder(dividingBy: 60))) - let milliseconds: String = String(format: "%02d", Int((lapTime * 100).truncatingRemainder(dividingBy: 100))) - let lapTimeString = "\(minutes):\(seconds).\(milliseconds)" - print("\(index + 1). \(lapTimeString)") - } + mainStopwatch.record() } } -