8
8
9
9
import Foundation
10
10
11
- enum CurrentState {
11
+ @objc
12
+ public enum CurrentState : Int {
12
13
case running
13
14
case suspended
14
15
}
15
16
16
17
@objc
17
18
public class JATimer : NSObject {
18
19
20
+ // MARK: Private Properties
21
+
19
22
private var name : String = " "
20
23
private var initialized = false
21
- private var running : CurrentState = . suspended
24
+ private var timerState : CurrentState = . suspended
22
25
private var queue : DispatchQueue ?
23
26
private var timerForQueue : Int = 0
24
27
private var block : ( ) -> Void = { }
@@ -27,6 +30,8 @@ public class JATimer: NSObject {
27
30
super. init ( )
28
31
}
29
32
33
+ // MARK: Public Properties
34
+
30
35
@objc
31
36
public convenience init ( name: String , interval: Int , completion: ( @escaping ( ) -> Void ) ) {
32
37
self . init ( )
@@ -42,104 +47,62 @@ public class JATimer: NSObject {
42
47
return queue
43
48
}
44
49
50
+ deinit {
51
+ queue = nil
52
+ }
53
+
54
+ /// This function needs to be called to initialize timer after instantiation
45
55
@objc
46
56
public func inititalize( ) {
47
57
if let _ = self . getQueue ( ) {
48
58
timedBlock ( )
49
59
}
50
60
initialized = true
61
+ timerState = . running
62
+ }
63
+
64
+ @objc
65
+ public func isInitialized( ) -> Bool {
66
+ return initialized
67
+ }
68
+
69
+ @objc
70
+ public func getCurrentState( ) -> CurrentState {
71
+ return timerState
72
+ }
73
+
74
+ @objc
75
+ public func pause( ) {
76
+ guard initialized == true else {
77
+ fatalError ( " Timer needs to be initialized before you can pause it. " )
78
+ }
79
+ guard timerState == . running else {
80
+ fatalError ( " Attempt to pause timer when it is already paused. " )
81
+ }
82
+ queue? . suspend ( )
83
+ timerState = . suspended
84
+ }
85
+
86
+ @objc
87
+ public func resume( ) {
88
+ guard initialized == true else {
89
+ fatalError ( " Timer needs to be initialized before you can reusme it. " )
90
+ }
91
+ guard timerState == . suspended else {
92
+ fatalError ( " Attempt to resume timer when it is already running. " )
93
+ }
94
+ queue? . resume ( )
95
+ timerState = . running
51
96
}
52
97
98
+ // MARK: Private Properties
99
+
53
100
private func timedBlock( ) {
54
101
queue? . asyncAfter ( deadline: . now( ) + . seconds( timerForQueue) ) {
55
102
self . block ( )
56
103
self . timedBlock ( )
57
104
}
58
- print ( " Test: seconds: \( timerForQueue ) " )
105
+ print ( " Timed Block set up " )
59
106
}
60
107
61
108
}
62
-
63
- //
64
- //import Foundation
65
- //
66
- //@objc
67
- //public class LocationManagerScheduler: NSObject {
68
- // private static var frequentCounter = 0
69
- // private static var occasionalCounter = 0
70
- // private static let frequentInterval = DispatchQueue(label: "com.vtinfo.locationScheduler.Frequent")
71
- // private static let occasionalInterval = DispatchQueue(label: "com.vtinfo.locationScheduler.Occasional")
72
- //
73
- // @objc
74
- // static var currentInterval: locationUpdateInterval = .occasional {
75
- // didSet {
76
- // guard currentInterval != oldValue else { return }
77
- //
78
- // switch currentInterval {
79
- // case .frequent:
80
- // switchQueue()
81
- // case .occasional:
82
- // switchQueue()
83
- // case .none:
84
- // suspendAllQueues()
85
- // }
86
- // }
87
- // }
88
- //
89
- // private class func switchQueue() {
90
- // if occasionalCounter == 0 {
91
- // occasionalInterval.suspend()
92
- // occasionalCounter -= 1
93
- //
94
- // frequentInterval.resume()
95
- // frequentCounter = 1
96
- // }
97
- //
98
- // if frequentCounter == 0 {
99
- // frequentInterval.suspend()
100
- // frequentCounter -= 1
101
- //
102
- // occasionalInterval.resume()
103
- // occasionalCounter = 1
104
- // }
105
- // }
106
- //
107
- // private class func suspendAllQueues() {
108
- // if occasionalCounter == 0 {
109
- // occasionalInterval.suspend()
110
- // occasionalCounter -= 1
111
- // }
112
- //
113
- // if frequentCounter == 0 {
114
- // frequentInterval.suspend()
115
- // frequentCounter -= 1
116
- // }
117
- // }
118
- //
119
- // private func frequentIntervalHandler() {
120
- // print("[LOC TEST] Frequent running")
121
- // LocationManagerScheduler.frequentInterval.asyncAfter(deadline: .now() 10) {
122
- // self.frequentIntervalHandler()
123
- // }
124
- // }
125
- //
126
- // private func occassionalIntervalHandler() {
127
- // print("[LOC TEST] Occassional running")
128
- // LocationManagerScheduler.occasionalInterval.asyncAfter(deadline: .now() 30) {
129
- // self.occassionalIntervalHandler()
130
- // }
131
- // }
132
- //
133
- // public override init() {
134
- // LocationManagerScheduler.frequentInterval.asyncAfter(deadline: .now() 10) {
135
- // self.frequentIntervalHandler()
136
- // }
137
- //
138
- // LocationManagerScheduler.occasionalInterval.asyncAfter(deadline: .now() 30) {
139
- // self.occassionalIntervalHandler()
140
- // }
141
- //
142
- //
143
- // }
144
- //}
145
-
0 commit comments