Skip to content

Instantly share code, notes, and snippets.

@NavdeepSinghh
Last active June 19, 2019 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NavdeepSinghh/ed908ef0b126ab25a8c10369636a133a to your computer and use it in GitHub Desktop.
Save NavdeepSinghh/ed908ef0b126ab25a8c10369636a133a to your computer and use it in GitHub Desktop.
struct Counter {
internal var countKeeper: CountKeeper
init(countKeeper: CountKeeper = RealTimeCounter()) {
self.countKeeper = countKeeper
}
private var start: Double?
private var totalCount: Double = 0
var isCounterRunning: Bool {
return start != nil
}
var incrementedCount: Double {
return totalCount + (start.map {_ in countKeeper.count } ?? 0)
}
mutating func startCounter() {
start = countKeeper.count
}
mutating func pauseCounter() {
totalCount = incrementedCount
start = nil
}
mutating func resetCounter() {
start = nil
totalCount = 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment