@@ -11,19 +11,6 @@ protocol StandardConstraintableView {
1111 func initialSubView( )
1212}
1313
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-
2714/// The timer counting mode.
2815public enum MGCountMode {
2916 case stopWatch
@@ -37,11 +24,13 @@ public enum MGCountMode {
3724
3825public class MagicTimer : MGLogable {
3926
40- private var container : MGTimerContainer
27+ private var counter : MGCounterBehavior
28+ private var executive : MGObservableTimerBehavior
4129 private var backgroundCalculator : MGBackgroundCalculableBehavior
30+
4231 private var state : MagicTimerState = . none {
4332 willSet {
44- container . executive. state = newValue
33+ executive. state = newValue
4534 backgroundCalculator. state = newValue
4635
4736 }
@@ -57,21 +46,21 @@ public class MagicTimer: MGLogable {
5746 public var defultValue : TimeInterval = 0 {
5847 willSet {
5948 let positiveValue = max ( 0 , newValue)
60- container . counter. setDefaultValue ( positiveValue)
49+ counter. setDefaultValue ( positiveValue)
6150 }
6251 }
6352 /// Set value to counter effectiveValue.
6453 public var effectiveValue : TimeInterval = 1 {
6554 willSet {
6655 let positiveValue = max ( 0 , newValue)
67- container . counter. setEffectiveValue ( positiveValue)
56+ counter. setEffectiveValue ( positiveValue)
6857 }
6958 }
7059 /// Set time interval to executive timeInerval.
7160 public var timeInterval : TimeInterval = 1 {
7261 willSet {
7362 let positiveValue = max ( 0 , newValue)
74- container . executive. setTimeInterval ( positiveValue)
63+ executive. setTimeInterval ( positiveValue)
7564 }
7665 }
7766 /// Set value to backgroundCalculator isActiveBackgroundMode property.
@@ -89,8 +78,9 @@ public class MagicTimer: MGLogable {
8978 }
9079
9180 public init ( ) {
92- self . container = MGTimerContainer ( counter: MGCounter ( ) , executive: MGTimerExecutive ( ) )
93- self . backgroundCalculator = MGBackgroundCalculator ( )
81+ counter = MGCounter ( )
82+ executive = MGTimerExecutive ( )
83+ backgroundCalculator = MGBackgroundCalculator ( )
9484
9585 backgroundCalculator. observe = { elapsedTime in
9686
@@ -103,25 +93,25 @@ public class MagicTimer: MGLogable {
10393 switch countMode {
10494 case . stopWatch:
10595 // Set totalCountedValue to all elpased time plus time in background.
106- container . counter. setTotalCountedValue ( elapsedTime)
96+ counter. setTotalCountedValue ( elapsedTime)
10797 case let . countDown( fromSeconds: countDownSeconds) :
10898
10999 let subtraction = countDownSeconds - elapsedTime
110100 // Checking elapsed time in background wasn't negeative.
111101 if subtraction. isPositive {
112102 // Set totalCountedValue to total time minus elapsed time in background.
113- container . counter. setTotalCountedValue ( subtraction)
103+ counter. setTotalCountedValue ( subtraction)
114104 } else {
115- container . counter. setTotalCountedValue ( 1 )
105+ counter. setTotalCountedValue ( 1 )
116106 }
117107 }
118108 }
119109
120110 private func countUp( ) {
121111
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)
125115 }
126116 }
127117
@@ -131,28 +121,28 @@ public class MagicTimer: MGLogable {
131121 fatalError ( " The time does not leading to valid format. Use valid effetiveValue " )
132122 }
133123
134- container . counter. setTotalCountedValue ( fromSeconds)
124+ counter. setTotalCountedValue ( fromSeconds)
135125
136126 // Every timeInterval observe value is called.
137- container . executive. observeValue = {
127+ executive. observeValue = {
138128 // 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 ( )
141131 self . didStateChange ? ( . stopped)
142132
143133 return
144134 }
145135 // Subtract effectiveValue from totalCountedValue.
146- self . container . counter. subtract ( )
136+ self . counter. subtract ( )
147137 // Tell the delegate totalCountedValue(elapsed time).
148- self . observeElapsedTime ? ( self . container . counter. totalCountedValue)
138+ self . observeElapsedTime ? ( self . counter. totalCountedValue)
149139 }
150140 }
151141
152142 /// Observe counting mode and start counting.
153143 public func start( ) {
154144
155- container . executive. start {
145+ executive. start {
156146 // Set current date to timer firing date(for calculate background elapsed time). When set the time is not fired.
157147 self . backgroundCalculator. setTimeFiredDate ( Date ( ) )
158148 }
@@ -170,16 +160,16 @@ public class MagicTimer: MGLogable {
170160 }
171161 /// Stop timer counting.
172162 public func stop( ) {
173- container . executive. suspand ( )
163+ executive. suspand ( )
174164 state = . stopped
175165 didStateChange ? ( . stopped)
176166
177167 log ( message: " timer stopped " )
178168 }
179169 /// Reset timer to zero
180170 public func reset( ) {
181- container . executive. suspand ( )
182- container . counter. resetTotalCounted ( )
171+ executive. suspand ( )
172+ counter. resetTotalCounted ( )
183173 state = . restarted
184174 didStateChange ? ( . restarted)
185175
@@ -188,8 +178,8 @@ public class MagicTimer: MGLogable {
188178 }
189179 /// Reset timer to default value
190180 public func resetToDefault( ) {
191- container . executive. suspand ( )
192- container . counter. resetToDefaultValue ( )
181+ executive. suspand ( )
182+ counter. resetToDefaultValue ( )
193183 state = . restarted
194184 didStateChange ? ( . restarted)
195185 log ( message: " timer restarted to default " )
0 commit comments