|
| 1 | +import Foundation |
| 2 | +import MathOperators |
| 3 | + |
| 4 | +public protocol MagicTimerCounterInterface { |
| 5 | + /// The total counted value. |
| 6 | + var totalCountedValue: TimeInterval { get set } |
| 7 | + /// A number which is added or minused on each ``timeInterval``. |
| 8 | + var effectiveValue: TimeInterval { get set } |
| 9 | + /// The initial value of counter. |
| 10 | + var defultValue: TimeInterval { get set } |
| 11 | + /// Add effectiveValue to totalCountedValue. |
| 12 | + func add() |
| 13 | + /// Subtract effectiveValue from totalCountedValue. |
| 14 | + func subtract() |
| 15 | + /// Reset totalCountedValue to zero. |
| 16 | + func resetTotalCounted() |
| 17 | + /// Reset totalCountedValue to defultValue. |
| 18 | + func resetToDefaultValue() |
| 19 | +} |
| 20 | + |
| 21 | +public final class MagicTimerCounter: MagicTimerCounterInterface { |
| 22 | + |
| 23 | + // MARK: - Public properties |
| 24 | + public var totalCountedValue: TimeInterval = 0 |
| 25 | + public var effectiveValue: TimeInterval = 1.0 |
| 26 | + public var defultValue: TimeInterval = 0.0 { |
| 27 | + didSet { |
| 28 | + totalCountedValue = totalCountedValue.plus(defultValue) |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + // MARK: - Constructors |
| 33 | + public init() { } |
| 34 | + |
| 35 | + // MARK: - Public methods |
| 36 | + public func add() { |
| 37 | + totalCountedValue = totalCountedValue.plus(effectiveValue) |
| 38 | + } |
| 39 | + |
| 40 | + public func subtract() { |
| 41 | + totalCountedValue = totalCountedValue.minus(effectiveValue) |
| 42 | + } |
| 43 | + |
| 44 | + public func resetTotalCounted() { |
| 45 | + totalCountedValue = 0 |
| 46 | + } |
| 47 | + |
| 48 | + public func resetToDefaultValue() { |
| 49 | + totalCountedValue = defultValue |
| 50 | + } |
| 51 | +} |
| 52 | + |
0 commit comments