Skip to content

Commit

Permalink
⚓️ :: [#18] 타이머 로직 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
Eunho0922 committed Dec 25, 2023
1 parent 791f7e4 commit 0d71a2f
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions Sources/MindGymKit /MindGymKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,89 @@ public class MindGymStopWatchKit {
}
}

public class MindGaGymKitTimer: NSObject {
private var initCounter: Double = 0.0
private var counter: Double = 0.0
private var timer: Timer?
private let timerSubject = PublishSubject<String>()

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

func setting(count: Double) {
initCounter = count
self.counter = count
let timeString = self.timeString(from: self.counter)
self.timerSubject.onNext(timeString)
}

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.timerSubject.onNext(timeString)
}
}

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

}

func restart() {
counter = initCounter
let timeString = self.timeString(from: self.counter)
self.timerSubject.onNext(timeString)
}

func reset() {
counter = 0.0
initCounter = 0.0
let timeString = self.timeString(from: self.counter)
self.timerSubject.onNext(timeString)
}

private func timeString(from counter: Double) -> String {

let hours: String = String(format: "%02d", Int(counter / 3600))
let minutes: String = String(format: "%02d", Int(counter / 60))
let seconds: String = String(format: "%02d", Int(counter.truncatingRemainder(dividingBy: 60)))
if counter / 3600 >= 1 {
return "\(hours) : \(minutes) : \(seconds)"
} else if counter / 60 > 1 {
return "\(minutes) : \(seconds)"
} else {
return "\(minutes) : \(seconds)"
}
}
}

public class MindGymTimerKit {

public let mainTimer: MindGaGymKitTimer = MindGaGymKitTimer()

public func setting(count: Double) {
mainTimer.setting(count: count)
}

public func startTimer() {
mainTimer.start()
}

public func stopTimer() {
mainTimer.stop()
}

public func resetTimer() {
mainTimer.reset()
}

public func restartTimer() {
mainTimer.restart()
}

}

0 comments on commit 0d71a2f

Please sign in to comment.