From f6711a3d16dcd3622806806e4efba983f51f22c7 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 15 Feb 2023 12:23:12 +0100 Subject: [PATCH] Fix issue on timer when recording/playing voice message or voice broadcast. All the seconds were not properly displayed. --- .../lib/core/utils/timer/CountUpTimer.kt | 4 +- .../lib/core/utils/timer/SpecialRound.kt | 28 +++++++++++ .../lib/core/utils/timer/SpecialRoundTest.kt | 50 +++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 library/core-utils/src/main/java/im/vector/lib/core/utils/timer/SpecialRound.kt create mode 100644 library/core-utils/src/test/java/im/vector/lib/core/utils/timer/SpecialRoundTest.kt diff --git a/library/core-utils/src/main/java/im/vector/lib/core/utils/timer/CountUpTimer.kt b/library/core-utils/src/main/java/im/vector/lib/core/utils/timer/CountUpTimer.kt index ea1d3d36e2..17af1c3190 100644 --- a/library/core-utils/src/main/java/im/vector/lib/core/utils/timer/CountUpTimer.kt +++ b/library/core-utils/src/main/java/im/vector/lib/core/utils/timer/CountUpTimer.kt @@ -33,13 +33,15 @@ class CountUpTimer( private val lastTime: AtomicLong = AtomicLong(clock.epochMillis()) private val elapsedTime: AtomicLong = AtomicLong(0) + // To ensure that the regular tick value is an exact multiple of `intervalInMs` + private val specialRound = SpecialRound(intervalInMs) private fun startCounter() { counterJob?.cancel() counterJob = coroutineScope.launch { while (true) { delay(intervalInMs - elapsedTime() % intervalInMs) - tickListener?.onTick(elapsedTime()) + tickListener?.onTick(specialRound.round(elapsedTime())) } } } diff --git a/library/core-utils/src/main/java/im/vector/lib/core/utils/timer/SpecialRound.kt b/library/core-utils/src/main/java/im/vector/lib/core/utils/timer/SpecialRound.kt new file mode 100644 index 0000000000..82fead13e0 --- /dev/null +++ b/library/core-utils/src/main/java/im/vector/lib/core/utils/timer/SpecialRound.kt @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.lib.core.utils.timer + +import kotlin.math.round + +class SpecialRound(private val step: Long) { + /** + * Round the provided value to the nearest multiple of `step`. + */ + fun round(value: Long): Long { + return round(value.toDouble() / step).toLong() * step + } +} diff --git a/library/core-utils/src/test/java/im/vector/lib/core/utils/timer/SpecialRoundTest.kt b/library/core-utils/src/test/java/im/vector/lib/core/utils/timer/SpecialRoundTest.kt new file mode 100644 index 0000000000..73be5aab8b --- /dev/null +++ b/library/core-utils/src/test/java/im/vector/lib/core/utils/timer/SpecialRoundTest.kt @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.lib.core.utils.timer + +import org.amshove.kluent.shouldBeEqualTo +import org.junit.Test + +class SpecialRoundTest { + @Test + fun `test special round 500`() { + val sut = SpecialRound(500) + sut.round(1) shouldBeEqualTo 0 + sut.round(499) shouldBeEqualTo 500 + sut.round(500) shouldBeEqualTo 500 + sut.round(501) shouldBeEqualTo 500 + sut.round(999) shouldBeEqualTo 1_000 + sut.round(1000) shouldBeEqualTo 1_000 + sut.round(1001) shouldBeEqualTo 1_000 + sut.round(1499) shouldBeEqualTo 1_500 + sut.round(1501) shouldBeEqualTo 1_500 + } + + @Test + fun `test special round 1_000`() { + val sut = SpecialRound(1_000) + sut.round(1) shouldBeEqualTo 0 + sut.round(499) shouldBeEqualTo 0 + sut.round(500) shouldBeEqualTo 0 + sut.round(501) shouldBeEqualTo 1_000 + sut.round(999) shouldBeEqualTo 1_000 + sut.round(1000) shouldBeEqualTo 1_000 + sut.round(1001) shouldBeEqualTo 1_000 + sut.round(1499) shouldBeEqualTo 1_000 + sut.round(1501) shouldBeEqualTo 2_000 + } +}