From 92942bc07e8a574409044b44c131ed6524c630fa Mon Sep 17 00:00:00 2001 From: Diego Mello Date: Thu, 26 Oct 2023 13:52:09 -0300 Subject: [PATCH] clamp --- app/containers/AudioPlayer/Seek.tsx | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/app/containers/AudioPlayer/Seek.tsx b/app/containers/AudioPlayer/Seek.tsx index 9c797895c..0c6c1e464 100644 --- a/app/containers/AudioPlayer/Seek.tsx +++ b/app/containers/AudioPlayer/Seek.tsx @@ -26,6 +26,13 @@ interface ISeek { onChangeTime: (time: number) => Promise; } +// Could use from react-native-redash. We have it as dev dep already. +function clamp(value: number, min: number, max: number) { + 'worklet'; + + return Math.min(Math.max(value, min), max); +} + const Seek = ({ currentTime, duration, loaded = false, onChangeTime }: ISeek) => { const { colors } = useTheme(); @@ -54,14 +61,7 @@ const Seek = ({ currentTime, duration, loaded = false, onChangeTime }: ISeek) => ctx.startX = timePosition.value; }, onActive: (event, ctx: any) => { - const moveInX: number = ctx.startX + event.translationX; - if (moveInX < 0) { - timePosition.value = 0; - } else if (moveInX > maxWidth.value) { - timePosition.value = maxWidth.value; - } else { - timePosition.value = moveInX; - } + timePosition.value = clamp(ctx.startX + event.translationX, 0, maxWidth.value); isTimeChanged.value = true; scale.value = 1.3; currentTime.value = (timePosition.value * duration.value) / maxWidth.value || 0;