Skip to content

Commit 1cd5ee2

Browse files
authored
Update MagicTimer.md
1 parent 91d23e6 commit 1cd5ee2

1 file changed

Lines changed: 52 additions & 75 deletions

File tree

docs/MagicTimer.md

Lines changed: 52 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,75 @@
1-
# MagicTimer
1+
# MagicTimer Class
22

3-
The `MagicTimer` class is a timer implementation written in Swift. It provides various functionalities to start, stop, reset, background time calculation and observe the elapsed time. The class uses a set of handler closures to notify the changes in the timer's state and elapsed time.
3+
The `MagicTimer` class is a timer implementation that provides various functionalities to start, stop, reset, and calculate elapsed time. It supports different timer modes, background time calculation, and event handlers.
44

5-
## Typealiases
5+
## Properties
66

7-
```swift
8-
public typealias StateHandler = ((MagicTimerState) -> Void)
9-
public typealias ElapsedTimeHandler = ((TimeInterval) -> Void)
10-
```
7+
### Handlers
118

12-
- `StateHandler`: A closure type that takes a `MagicTimerState` parameter, representing the timer's state, and returns `Void`. This closure is called when the state of the timer changes.
13-
- `ElapsedTimeHandler`: A closure type that takes a `TimeInterval` parameter, representing the elapsed time, and returns `Void`. This closure is called on each time interval to update the elapsed time.
9+
- `lastStateDidChangeHandler`: A handler called when the timer state changes. It provides the last state of the timer.
10+
- `elapsedTimeDidChangeHandler`: A handler called on each time interval. It provides the elapsed time.
1411

15-
## Public Properties
12+
### Get-only Properties
1613

17-
- `lastStateDidChangeHandler`: A closure that is called when the state of the timer changes.
18-
- `elapsedTimeDidChangeHandler`: A closure that is called on each time interval to update the elapsed time.
14+
- `lastState`: The last state of the timer. It can be one of the following states: `.none`, `.fired`, `.stopped`, or `.restarted`.
15+
- `elapsedTime`: The elapsed time from when the timer started.
1916

20-
```swift
21-
public var lastStateDidChangeHandler: StateHandler?
22-
public var elapsedTimeDidChangeHandler: ElapsedTimeHandler?
23-
```
17+
### Timer Configuration Properties
2418

25-
- `lastState`: A read-only property representing the last state of the timer. Checkout `MagicTimerState`.
19+
- `countMode`: The timer count mode. It can be either `.stopWatch` or `.countDown(fromSeconds: TimeInterval)`.
20+
- `defultValue`: The timer default value. This value is used when resetting the timer.
21+
- `effectiveValue`: The value added or subtracted on each time interval.
22+
- `timeInterval`: The time interval between each timer tick.
23+
- `isActiveInBackground`: Determines whether the timer calculates time in the background.
2624

27-
```swift
28-
public private(set) var lastState: MagicTimerState = .none {
29-
didSet {
30-
lastStateDidChangeHandler?(lastState)
31-
}
32-
}
33-
```
25+
## Constructors
3426

35-
- `elapsedTime`: A read-only property representing the elapsed time from when the timer started. Default is 0.
27+
- `init(counter: MagicTimerCounterInterface = MagicTimerCounter(), executive: MagicTimerExecutiveInterface = MagicTimerExecutive(), backgroundCalculator: MagicTimerBackgroundCalculatorInterface = MagicTimerBackgroundCalculator())`: Initializes a new instance of the `MagicTimer` class with the specified counter, executive, and background calculator.
3628

37-
```swift
38-
public private(set) var elapsedTime: TimeInterval = 0 {
39-
didSet {
40-
elapsedTimeDidChangeHandler?(elapsedTime)
41-
}
42-
}
43-
```
29+
## Methods
4430

45-
- `countMode`: Timer count mode. Default is `.stopWatch`. Checkout `MGTimerMode`.
31+
- `start()`: Starts the timer.
32+
- `stop()`: Stops the timer.
33+
- `reset()`: Resets the timer, setting the elapsed time to zero.
34+
- `resetToDefault()`: Resets the timer to the default value.
4635

47-
```swift
48-
public var countMode: MGTimerMode = .stopWatch
49-
```
5036

51-
- `defultValue`: Timer default value. Default is 0.
5237

53-
```swift
54-
public var defultValue: TimeInterval = 0 {
55-
willSet {
56-
guard newValue.isBiggerThanOrEqual(.zero) else {
57-
fatalError("The defultValue should be greater or equal to zero.")
58-
}
59-
counter.defultValue = newValue
60-
}
61-
}
62-
```
38+
> Note: The `MagicTimer` class uses the `MagicTimerCounterInterface`, `MagicTimerExecutiveInterface`, and `MagicTimerBackgroundCalculatorInterface` protocols for counter, executive, and background calculator implementations, respectively.
39+
40+
Checkout the documentations:
41+
[MagicTimerCounterInterface](https://github.com/MagicTimerFW/MagicTimerCore/blob/main/docs/MagicTimerCounter.md]MagicTimerCounterInterface), [MagicTimerExecutiveInterface](https://github.com/MagicTimerFW/MagicTimerCore/blob/main/docs/MagicTimerExecutive.md), [MagicTimerBackgroundCalculatorInterface](https://github.com/MagicTimerFW/MagicTimerCore/blob/main/docs/MagicTimerBackgroundCalculator.md
42+
),
6343

64-
- `effectiveValue`: A number which is added or minused on each `timeInterval`. Default is 1.
44+
## Usage
6545

6646
```swift
67-
public var effectiveValue: TimeInterval = 1 {
68-
willSet {
69-
guard newValue.isBiggerThanOrEqual(.zero) else {
70-
fatalError("The effectiveValue should be greater or equal to zero.")
71-
}
72-
counter.effectiveValue = newValue
73-
}
74-
}
75-
```
47+
// Create an instance of MagicTimer
48+
let timer = MagicTimer()
7649

77-
- `timeInterval`: Timer time interval, Default is 1.
50+
// Configure event handlers
51+
timer.lastStateDidChangeHandler = { state in
52+
// Handle timer state changes
53+
}
7854

79-
```swift
80-
public var timeInterval: TimeInterval = 1 {
81-
willSet {
82-
guard newValue.isBiggerThanOrEqual(.zero) else {
83-
fatalError("The timeInterval should be greater or equal to zero.")
84-
}
85-
executive.timeInterval = newValue
86-
}
55+
timer.elapsedTimeDidChangeHandler = { elapsedTime in
56+
// Handle elapsed time changes
8757
}
88-
```
8958

90-
- `isActiveInBackground`: By changing this type, the timer decides whether to calculate the time in the background. Default is true.
59+
// Start the timer
60+
timer.start()
9161

92-
```swift
93-
public var isActiveInBackground: Bool = true {
94-
willSet {
95-
backgroundCalculator.isActiveBackgroundMode = newValue
96-
}
97-
}
98-
```
62+
// ...
63+
64+
// Stop the timer
65+
timer.stop()
66+
67+
// ...
68+
69+
// Reset the timer
70+
timer.reset()
71+
72+
// ...
73+
74+
// Reset the timer to the default value
75+
timer.resetToDefault()

0 commit comments

Comments
 (0)