about summary refs log tree commit diff
path: root/src/screens/VideoFeed/components/Scrubber.tsx
blob: 29cc4b278ae673e72a8da25c3bbb2a3cd4ad8c49 (plain) (blame)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import {useCallback, useMemo, useState} from 'react'
import {View} from 'react-native'
import {
  Gesture,
  GestureDetector,
  NativeGesture,
} from 'react-native-gesture-handler'
import Animated, {
  interpolate,
  runOnJS,
  runOnUI,
  SharedValue,
  useAnimatedReaction,
  useAnimatedStyle,
  useSharedValue,
  withTiming,
} from 'react-native-reanimated'
import {
  useSafeAreaFrame,
  useSafeAreaInsets,
} from 'react-native-safe-area-context'
import {useEventListener} from 'expo'
import {VideoPlayer} from 'expo-video'

import {tokens} from '#/alf'
import {atoms as a} from '#/alf'
import {formatTime} from '#/components/Post/Embed/VideoEmbed/VideoEmbedInner/web-controls/utils'
import {Text} from '#/components/Typography'

// magic number that is roughly the min height of the write reply button
// we inset the video by this amount
export const VIDEO_PLAYER_BOTTOM_INSET = 57

export function Scrubber({
  active,
  player,
  seekingAnimationSV,
  scrollGesture,
  children,
}: {
  active: boolean
  player?: VideoPlayer
  seekingAnimationSV: SharedValue<number>
  scrollGesture: NativeGesture
  children?: React.ReactNode
}) {
  const {width: screenWidth} = useSafeAreaFrame()
  const insets = useSafeAreaInsets()
  const currentTimeSV = useSharedValue(0)
  const durationSV = useSharedValue(0)
  const [currentSeekTime, setCurrentSeekTime] = useState(0)
  const [duration, setDuration] = useState(0)

  const updateTime = (currentTime: number, duration: number) => {
    'worklet'
    currentTimeSV.set(currentTime)
    if (duration !== 0) {
      durationSV.set(duration)
    }
  }

  const isSeekingSV = useSharedValue(false)
  const seekProgressSV = useSharedValue(0)

  useAnimatedReaction(
    () => Math.round(seekProgressSV.get()),
    (progress, prevProgress) => {
      if (progress !== prevProgress) {
        runOnJS(setCurrentSeekTime)(progress)
      }
    },
  )

  const seekBy = useCallback(
    (time: number) => {
      player?.seekBy(time)

      setTimeout(() => {
        runOnUI(() => {
          'worklet'
          isSeekingSV.set(false)
          seekingAnimationSV.set(withTiming(0, {duration: 500}))
        })()
      }, 50)
    },
    [player, isSeekingSV, seekingAnimationSV],
  )

  const scrubPanGesture = useMemo(() => {
    return Gesture.Pan()
      .blocksExternalGesture(scrollGesture)
      .activeOffsetX([-10, 10])
      .failOffsetY([-10, 10])
      .onStart(() => {
        'worklet'
        seekProgressSV.set(currentTimeSV.get())
        isSeekingSV.set(true)
        seekingAnimationSV.set(withTiming(1, {duration: 500}))
      })
      .onUpdate(evt => {
        'worklet'
        const progress = evt.x / screenWidth
        seekProgressSV.set(
          clamp(progress * durationSV.get(), 0, durationSV.get()),
        )
      })
      .onEnd(evt => {
        'worklet'
        isSeekingSV.get()

        const progress = evt.x / screenWidth
        const newTime = clamp(progress * durationSV.get(), 0, durationSV.get())

        // optimisically set the progress bar
        seekProgressSV.set(newTime)

        // it's seek by, so offset by the current time
        // seekBy sets isSeekingSV back to false, so no need to do that here
        runOnJS(seekBy)(newTime - currentTimeSV.get())
      })
  }, [
    scrollGesture,
    seekingAnimationSV,
    seekBy,
    screenWidth,
    currentTimeSV,
    durationSV,
    isSeekingSV,
    seekProgressSV,
  ])

  const timeStyle = useAnimatedStyle(() => {
    return {
      display: seekingAnimationSV.get() === 0 ? 'none' : 'flex',
      opacity: seekingAnimationSV.get(),
    }
  })

  const barStyle = useAnimatedStyle(() => {
    const currentTime = isSeekingSV.get()
      ? seekProgressSV.get()
      : currentTimeSV.get()
    const progress = currentTime === 0 ? 0 : currentTime / durationSV.get()
    const isSeeking = seekingAnimationSV.get()
    return {
      height: isSeeking * 3 + 1,
      opacity: interpolate(isSeeking, [0, 1], [0.4, 0.6]),
      width: `${progress * 100}%`,
    }
  })
  const trackStyle = useAnimatedStyle(() => {
    return {
      height: seekingAnimationSV.get() * 3 + 1,
    }
  })
  const childrenStyle = useAnimatedStyle(() => {
    return {
      opacity: 1 - seekingAnimationSV.get(),
    }
  })

  return (
    <>
      {player && active && (
        <PlayerListener
          player={player}
          setDuration={setDuration}
          updateTime={updateTime}
        />
      )}
      <Animated.View
        style={[
          a.absolute,
          {
            left: 0,
            right: 0,
            bottom: insets.bottom + 80,
          },
          timeStyle,
        ]}
        pointerEvents="none">
        <Text style={[a.text_center, a.font_bold]}>
          <Text style={[a.text_5xl, {fontVariant: ['tabular-nums']}]}>
            {formatTime(currentSeekTime)}
          </Text>
          <Text style={[a.text_2xl, {opacity: 0.8}]}>{'  /  '}</Text>
          <Text
            style={[
              a.text_5xl,
              {opacity: 0.8},
              {fontVariant: ['tabular-nums']},
            ]}>
            {formatTime(duration)}
          </Text>
        </Text>
      </Animated.View>

      <GestureDetector gesture={scrubPanGesture}>
        <View
          style={[
            a.relative,
            a.w_full,
            a.justify_end,
            {
              paddingBottom: insets.bottom,
              minHeight:
                // bottom padding
                insets.bottom +
                // scrubber height
                tokens.space.lg +
                // write reply height
                VIDEO_PLAYER_BOTTOM_INSET,
            },
            a.z_10,
          ]}>
          <View style={[a.w_full, a.relative]}>
            <Animated.View
              style={[
                a.w_full,
                {backgroundColor: 'white', opacity: 0.2},
                trackStyle,
              ]}
            />
            <Animated.View
              style={[
                a.absolute,
                {top: 0, left: 0, backgroundColor: 'white'},
                barStyle,
              ]}
            />
          </View>
          <Animated.View
            style={[{minHeight: VIDEO_PLAYER_BOTTOM_INSET}, childrenStyle]}>
            {children}
          </Animated.View>
        </View>
      </GestureDetector>
    </>
  )
}

function PlayerListener({
  player,
  setDuration,
  updateTime,
}: {
  player: VideoPlayer
  setDuration: (duration: number) => void
  updateTime: (currentTime: number, duration: number) => void
}) {
  useEventListener(player, 'timeUpdate', evt => {
    const duration = player.duration
    if (duration !== 0) {
      setDuration(Math.round(duration))
    }
    runOnUI(updateTime)(evt.currentTime, duration)
  })

  return null
}

function clamp(num: number, min: number, max: number) {
  'worklet'
  return Math.min(Math.max(num, min), max)
}