From b9164e05478c81f09d5cbf43f3cbbaef7c09bd4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=80=ED=98=B8?= <127753071+Eunho0922@users.noreply.github.com> Date: Tue, 5 Dec 2023 19:22:02 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=8D=97=20::=20[#16]=20=EC=8A=A4?= =?UTF-8?q?=ED=86=B1=EC=9B=8C=EC=B9=98=20RxSwift=EB=A1=9C=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/MindGymKit /MindGymKit.swift | 81 +++++++++++----------------- 1 file changed, 30 insertions(+), 51 deletions(-) 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() } } - From 8844c81c77918aaf75b2aa235bd4f283217aa6e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=80=ED=98=B8?= <127753071+Eunho0922@users.noreply.github.com> Date: Tue, 5 Dec 2023 19:24:32 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=E2=9A=93=EF=B8=8F=20::=20[#16]=20README=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) 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) ```