-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathTimeCounter.kt
More file actions
192 lines (160 loc) · 4.2 KB
/
TimeCounter.kt
File metadata and controls
192 lines (160 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package common.base.utils
import android.os.SystemClock
import androidx.annotation.WorkerThread
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
class TimeCounter {
companion object Builder {
fun aCounter() = TimeCounter()
}
@Volatile
private var mStartTime: Long = 0
/**
* 步进间隔时间
*/
private var mStepForwardTimeMills: Long = 1000
/**
* true: 倒计时;false:累加计时
* def = true: 倒计时
*/
private var mIsCountDownOrAddTime = true
/**
* 计时任务
*/
@Volatile
private var mJobOfTimer: Job? = null
/**
* 监听者
*/
private var mCounterListener: ITimeCounterListener? = null
private var mTag = ""
var extraData: Any? = null
/**
* 是否需要 计算时间(加或减)
* def = true
*/
@Volatile
var isNeedCalculateTime = true
/**
* 开始计时
*/
fun start() {
if (mJobOfTimer != null) {
return
}
runTask()
}
private fun runTask() {
mJobOfTimer = XCoroutineScope.ioCoroutineScope?.launch {
val gapTime = mStepForwardTimeMills
delay(gapTime)
if (mIsCountDownOrAddTime){
mStartTime -= gapTime
if (mStartTime <= 0){
mJobOfTimer = null
onComplete()
return@launch
}
} else {
if (isNeedCalculateTime){
mStartTime += gapTime
}
}
onTimeUpdated(mStartTime)
runTask()
}
}
/**
* 上次暂停时的 系统时间戳
*/
private var mLastPausedTimeMills: Long = 0
/**
* 暂停计时
*/
fun pauseCountTime() {
mLastPausedTimeMills = SystemClock.elapsedRealtime()
stop()
}
fun stop() {
mJobOfTimer?.cancel()
mJobOfTimer = null
cancelDelayTask()
}
/**
* @param startTime Long 开始的时间(戳),一般为毫秒
*/
fun withStartTime(startTime: Long): TimeCounter{
this.mStartTime = startTime
return this
}
/**
* 计时的 步进/间隔 时间(戳)
* @param stepForwardTime 一般为毫秒
*/
fun withStepForwardTime(stepForwardTime: Long): TimeCounter {
this.mStepForwardTimeMills = stepForwardTime
return this
}
/**
* @param isToCountdownOrToAdd true:去倒计时; false: 去累加计时
*/
fun withCountdownOrAdd(isToCountdownOrToAdd: Boolean):TimeCounter {
this.mIsCountDownOrAddTime = isToCountdownOrToAdd
return this
}
fun withListener(l: ITimeCounterListener?): TimeCounter{
this.mCounterListener = l
return this
}
fun withTag(tag: String):TimeCounter {
this.mTag = tag
return this
}
/**
* 恢复计时
*/
fun resumeCountTime(){
if (mLastPausedTimeMills <= 0) {//没有暂停过
return
}
val elapsedTimeMills = SystemClock.elapsedRealtime() - mLastPausedTimeMills
mLastPausedTimeMills = 0
if (mIsCountDownOrAddTime){
mStartTime -= elapsedTimeMills
} else{
mStartTime += elapsedTimeMills
}
if (mIsCountDownOrAddTime){
if (mStartTime <= 0){
onComplete()
return
}
}
onTimeUpdated(mStartTime)
start()
}
@WorkerThread
open fun onTimeUpdated(theTime: Long){
mCounterListener?.onTimeCount(this,theTime,false)
}
@WorkerThread
open fun onComplete() {
mCounterListener?.onTimeCount(this,0,true)
}
fun isRunnig() = mJobOfTimer != null
private var mJobOfDelay: Job? = null
fun delayTask(delayTs: Long) {
mJobOfDelay = XCoroutineScope.ioCoroutineScope?.launch {
delay(delayTs)
onTimeUpdated(0)
}
}
fun cancelDelayTask(){
mJobOfDelay?.cancel()
}
fun getCurTime() = mStartTime
override fun toString(): String {
return "TimeCounter(mTag='$mTag')"
}
}