@@ -11,19 +11,6 @@ protocol StandardConstraintableView {
11
11
func initialSubView( )
12
12
}
13
13
14
- /// The container that contains counter and executive.
15
- class MGTimerContainer : MGLogable {
16
-
17
- var counter : MGCounterBehavior
18
- var executive : MGObservableTimerBehavior
19
-
20
- init ( counter: MGCounterBehavior , executive: MGObservableTimerBehavior ) {
21
- self . counter = counter
22
- self . executive = executive
23
- log ( message: " initialized " )
24
- }
25
- }
26
-
27
14
/// The timer counting mode.
28
15
public enum MGCountMode {
29
16
case stopWatch
@@ -37,11 +24,13 @@ public enum MGCountMode {
37
24
38
25
public class MagicTimer : MGLogable {
39
26
40
- private var container : MGTimerContainer
27
+ private var counter : MGCounterBehavior
28
+ private var executive : MGObservableTimerBehavior
41
29
private var backgroundCalculator : MGBackgroundCalculableBehavior
30
+
42
31
private var state : MagicTimerState = . none {
43
32
willSet {
44
- container . executive. state = newValue
33
+ executive. state = newValue
45
34
backgroundCalculator. state = newValue
46
35
47
36
}
@@ -57,21 +46,21 @@ public class MagicTimer: MGLogable {
57
46
public var defultValue : TimeInterval = 0 {
58
47
willSet {
59
48
let positiveValue = max ( 0 , newValue)
60
- container . counter. setDefaultValue ( positiveValue)
49
+ counter. setDefaultValue ( positiveValue)
61
50
}
62
51
}
63
52
/// Set value to counter effectiveValue.
64
53
public var effectiveValue : TimeInterval = 1 {
65
54
willSet {
66
55
let positiveValue = max ( 0 , newValue)
67
- container . counter. setEffectiveValue ( positiveValue)
56
+ counter. setEffectiveValue ( positiveValue)
68
57
}
69
58
}
70
59
/// Set time interval to executive timeInerval.
71
60
public var timeInterval : TimeInterval = 1 {
72
61
willSet {
73
62
let positiveValue = max ( 0 , newValue)
74
- container . executive. setTimeInterval ( positiveValue)
63
+ executive. setTimeInterval ( positiveValue)
75
64
}
76
65
}
77
66
/// Set value to backgroundCalculator isActiveBackgroundMode property.
@@ -89,8 +78,9 @@ public class MagicTimer: MGLogable {
89
78
}
90
79
91
80
public init ( ) {
92
- self . container = MGTimerContainer ( counter: MGCounter ( ) , executive: MGTimerExecutive ( ) )
93
- self . backgroundCalculator = MGBackgroundCalculator ( )
81
+ counter = MGCounter ( )
82
+ executive = MGTimerExecutive ( )
83
+ backgroundCalculator = MGBackgroundCalculator ( )
94
84
95
85
backgroundCalculator. observe = { elapsedTime in
96
86
@@ -103,25 +93,25 @@ public class MagicTimer: MGLogable {
103
93
switch countMode {
104
94
case . stopWatch:
105
95
// Set totalCountedValue to all elpased time plus time in background.
106
- container . counter. setTotalCountedValue ( elapsedTime)
96
+ counter. setTotalCountedValue ( elapsedTime)
107
97
case let . countDown( fromSeconds: countDownSeconds) :
108
98
109
99
let subtraction = countDownSeconds - elapsedTime
110
100
// Checking elapsed time in background wasn't negeative.
111
101
if subtraction. isPositive {
112
102
// Set totalCountedValue to total time minus elapsed time in background.
113
- container . counter. setTotalCountedValue ( subtraction)
103
+ counter. setTotalCountedValue ( subtraction)
114
104
} else {
115
- container . counter. setTotalCountedValue ( 1 )
105
+ counter. setTotalCountedValue ( 1 )
116
106
}
117
107
}
118
108
}
119
109
120
110
private func countUp( ) {
121
111
122
- container . executive. observeValue = {
123
- self . container . counter. add ( )
124
- self . observeElapsedTime ? ( self . container . counter. totalCountedValue)
112
+ executive. observeValue = {
113
+ self . counter. add ( )
114
+ self . observeElapsedTime ? ( self . counter. totalCountedValue)
125
115
}
126
116
}
127
117
@@ -131,28 +121,28 @@ public class MagicTimer: MGLogable {
131
121
fatalError ( " The time does not leading to valid format. Use valid effetiveValue " )
132
122
}
133
123
134
- container . counter. setTotalCountedValue ( fromSeconds)
124
+ counter. setTotalCountedValue ( fromSeconds)
135
125
136
126
// Every timeInterval observe value is called.
137
- container . executive. observeValue = {
127
+ executive. observeValue = {
138
128
// Check if totalCountedValue is valid or not.
139
- guard self . container . counter. totalCountedValue > 0 else {
140
- self . container . executive. suspand ( )
129
+ guard self . counter. totalCountedValue > 0 else {
130
+ self . executive. suspand ( )
141
131
self . didStateChange ? ( . stopped)
142
132
143
133
return
144
134
}
145
135
// Subtract effectiveValue from totalCountedValue.
146
- self . container . counter. subtract ( )
136
+ self . counter. subtract ( )
147
137
// Tell the delegate totalCountedValue(elapsed time).
148
- self . observeElapsedTime ? ( self . container . counter. totalCountedValue)
138
+ self . observeElapsedTime ? ( self . counter. totalCountedValue)
149
139
}
150
140
}
151
141
152
142
/// Observe counting mode and start counting.
153
143
public func start( ) {
154
144
155
- container . executive. start {
145
+ executive. start {
156
146
// Set current date to timer firing date(for calculate background elapsed time). When set the time is not fired.
157
147
self . backgroundCalculator. setTimeFiredDate ( Date ( ) )
158
148
}
@@ -170,16 +160,16 @@ public class MagicTimer: MGLogable {
170
160
}
171
161
/// Stop timer counting.
172
162
public func stop( ) {
173
- container . executive. suspand ( )
163
+ executive. suspand ( )
174
164
state = . stopped
175
165
didStateChange ? ( . stopped)
176
166
177
167
log ( message: " timer stopped " )
178
168
}
179
169
/// Reset timer to zero
180
170
public func reset( ) {
181
- container . executive. suspand ( )
182
- container . counter. resetTotalCounted ( )
171
+ executive. suspand ( )
172
+ counter. resetTotalCounted ( )
183
173
state = . restarted
184
174
didStateChange ? ( . restarted)
185
175
@@ -188,8 +178,8 @@ public class MagicTimer: MGLogable {
188
178
}
189
179
/// Reset timer to default value
190
180
public func resetToDefault( ) {
191
- container . executive. suspand ( )
192
- container . counter. resetToDefaultValue ( )
181
+ executive. suspand ( )
182
+ counter. resetToDefaultValue ( )
193
183
state = . restarted
194
184
didStateChange ? ( . restarted)
195
185
log ( message: " timer restarted to default " )
0 commit comments