Skip to content

Commit f6711a3

Browse files
committed
Fix issue on timer when recording/playing voice message or voice broadcast. All the seconds were not properly displayed.
1 parent 671435c commit f6711a3

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

library/core-utils/src/main/java/im/vector/lib/core/utils/timer/CountUpTimer.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ class CountUpTimer(
3333

3434
private val lastTime: AtomicLong = AtomicLong(clock.epochMillis())
3535
private val elapsedTime: AtomicLong = AtomicLong(0)
36+
// To ensure that the regular tick value is an exact multiple of `intervalInMs`
37+
private val specialRound = SpecialRound(intervalInMs)
3638

3739
private fun startCounter() {
3840
counterJob?.cancel()
3941
counterJob = coroutineScope.launch {
4042
while (true) {
4143
delay(intervalInMs - elapsedTime() % intervalInMs)
42-
tickListener?.onTick(elapsedTime())
44+
tickListener?.onTick(specialRound.round(elapsedTime()))
4345
}
4446
}
4547
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2023 New Vector Ltd
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package im.vector.lib.core.utils.timer
18+
19+
import kotlin.math.round
20+
21+
class SpecialRound(private val step: Long) {
22+
/**
23+
* Round the provided value to the nearest multiple of `step`.
24+
*/
25+
fun round(value: Long): Long {
26+
return round(value.toDouble() / step).toLong() * step
27+
}
28+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2023 New Vector Ltd
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package im.vector.lib.core.utils.timer
18+
19+
import org.amshove.kluent.shouldBeEqualTo
20+
import org.junit.Test
21+
22+
class SpecialRoundTest {
23+
@Test
24+
fun `test special round 500`() {
25+
val sut = SpecialRound(500)
26+
sut.round(1) shouldBeEqualTo 0
27+
sut.round(499) shouldBeEqualTo 500
28+
sut.round(500) shouldBeEqualTo 500
29+
sut.round(501) shouldBeEqualTo 500
30+
sut.round(999) shouldBeEqualTo 1_000
31+
sut.round(1000) shouldBeEqualTo 1_000
32+
sut.round(1001) shouldBeEqualTo 1_000
33+
sut.round(1499) shouldBeEqualTo 1_500
34+
sut.round(1501) shouldBeEqualTo 1_500
35+
}
36+
37+
@Test
38+
fun `test special round 1_000`() {
39+
val sut = SpecialRound(1_000)
40+
sut.round(1) shouldBeEqualTo 0
41+
sut.round(499) shouldBeEqualTo 0
42+
sut.round(500) shouldBeEqualTo 0
43+
sut.round(501) shouldBeEqualTo 1_000
44+
sut.round(999) shouldBeEqualTo 1_000
45+
sut.round(1000) shouldBeEqualTo 1_000
46+
sut.round(1001) shouldBeEqualTo 1_000
47+
sut.round(1499) shouldBeEqualTo 1_000
48+
sut.round(1501) shouldBeEqualTo 2_000
49+
}
50+
}

0 commit comments

Comments
 (0)