Skip to content

Commit

Permalink
fix(mobile): return elapsedTime in seconds fixes #119
Browse files Browse the repository at this point in the history
  • Loading branch information
vydimitrov committed Apr 28, 2021
1 parent ec5296f commit 0a2ac54
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
14 changes: 11 additions & 3 deletions packages/mobile/__tests__/TimeWrapper.test.js
Expand Up @@ -8,7 +8,7 @@ import { TimeWrapper } from '../src/components'
Math.random = () => 0.124578

const fixture = {
durationMilliseconds: 10000,
duration: 10,
animatedElapsedTime: { addListener: jest.fn(), removeListener: jest.fn() },
renderAriaTime: ({ remainingTime }) => remainingTime,
animatedColor: {},
Expand Down Expand Up @@ -50,13 +50,21 @@ describe('behaviour tests', () => {
{...fixture}
animatedElapsedTime={animatedElapsedTime}
renderAriaTime={undefined}
/>
>
{({ remainingTime, elapsedTime }) => (
<>
<Text>Remaining time - {remainingTime}</Text>
<Text>Elapsed time - {elapsedTime}</Text>
</>
)}
</TimeWrapper>
)

act(() => {
listener({ value: 4823 })
})

expect(getByText('6')).toBeTruthy()
expect(getByText('Remaining time - 6')).toBeTruthy()
expect(getByText('Elapsed time - 4.823')).toBeTruthy()
})
})
2 changes: 1 addition & 1 deletion packages/mobile/src/components/CountdownCircleTimer.jsx
Expand Up @@ -93,7 +93,7 @@ const CountdownCircleTimer = (props) => {
<TimeWrapper
animatedColor={animatedStroke}
animatedElapsedTime={animatedElapsedTime}
durationMilliseconds={durationMilliseconds}
duration={durationMilliseconds / 1000} // durationMilliseconds is locked version of the duration
renderAriaTime={renderAriaTime}
>
{children}
Expand Down
15 changes: 8 additions & 7 deletions packages/mobile/src/components/TimeWrapper.jsx
Expand Up @@ -15,30 +15,31 @@ const TimeWrapper = (props) => {
const {
children,
animatedElapsedTime,
durationMilliseconds,
duration,
renderAriaTime,
animatedColor,
} = props

const [timeProps, setTimeProps] = useState({
animatedElapsedTime: 0,
remainingTime: durationMilliseconds / 1000,
remainingTime: duration,
elapsedTime: 0,
animatedColor,
})

useEffect(() => {
const animatedListenerId = animatedElapsedTime.addListener(({ value }) => {
const elapsedTime = value / 1000
setTimeProps({
remainingTime: Math.ceil((durationMilliseconds - value) / 1000),
elapsedTime: value,
remainingTime: Math.ceil(duration - elapsedTime),
elapsedTime,
animatedColor,
})
})

return () => {
animatedElapsedTime.removeListener(animatedListenerId)
}
}, [animatedElapsedTime, animatedColor, durationMilliseconds])
}, [animatedElapsedTime, animatedColor, duration])

return (
<>
Expand Down Expand Up @@ -68,7 +69,7 @@ const TimeWrapper = (props) => {
}

TimeWrapper.propTypes = {
durationMilliseconds: PropTypes.number.isRequired,
duration: PropTypes.number.isRequired,
animatedElapsedTime: PropTypes.object.isRequired,
// when there is a single color we just past the color string
// when there are more colors it is an interpolate object
Expand Down
5 changes: 3 additions & 2 deletions packages/mobile/types/CountdownCircleTimer.d.ts
@@ -1,8 +1,9 @@
import * as React from 'react'

export interface TimeProps {
remainingTime?: number
elapsedTime?: number
remainingTime: number
elapsedTime: number
animatedColor: Animated.AnimatedInterpolation
}

type ChildAsFunc = {
Expand Down

0 comments on commit 0a2ac54

Please sign in to comment.