-
Notifications
You must be signed in to change notification settings - Fork 3
/
ThemedRecorderSheet.tsx
219 lines (191 loc) · 6.78 KB
/
ThemedRecorderSheet.tsx
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
import React, { forwardRef, useRef, type Ref, useState } from 'react'
import {
Pressable,
View,
type StyleProp,
type ViewStyle,
TouchableOpacity,
Text,
type TextStyle,
} from 'react-native'
import { Audio } from 'expo-av'
import * as Haptics from 'expo-haptics'
import Ionicons from '@expo/vector-icons/Ionicons'
import Animated, {
Extrapolation,
interpolate,
useAnimatedStyle,
useSharedValue,
withSpring,
type WithSpringConfig,
} from 'react-native-reanimated'
import { useSafeAreaInsets } from 'react-native-safe-area-context'
import { Recorder, type RecorderRef } from '@lodev09/expo-recorder'
import { type TrueSheetProps, TrueSheet } from '@lodev09/react-native-true-sheet'
import { useThemeColor } from '@/hooks/useThemeColor'
import { Box } from './Box'
import { Spacing } from '@/constants/Spacing'
import { formatTimer } from '@/utils/formatTimer'
const RECORD_BUTTON_SIZE = 60
const RECORD_BUTTON_BACKGROUND_SIZE = RECORD_BUTTON_SIZE + Spacing.md
const RECORDING_INDICATOR_COLOR = '#d72d66'
const RECORDING_INDICATOR_SCALE = 0.5
const SPRING_SHORT_CONFIG: WithSpringConfig = {
stiffness: 120,
overshootClamping: true,
}
export interface ThemedRecorderSheetProps extends TrueSheetProps {
lightColor?: string
darkColor?: string
}
export interface ThemedRecorderSheetRef extends TrueSheet {}
export const ThemedRecorderSheet = forwardRef(
(props: ThemedRecorderSheetProps, ref: Ref<TrueSheet>) => {
const { style, lightColor, darkColor, ...rest } = props
const insets = useSafeAreaInsets()
const [isRecording, setIsRecording] = useState(false)
const [isPlaying, setIsPlaying] = useState(false)
const [position, setPosition] = useState(0)
const recorderRef = useRef<RecorderRef>(null)
const backgroundColor = useThemeColor({ light: lightColor, dark: darkColor }, 'recorderSheet')
const progressBackgroundColor = useThemeColor({}, 'recorderProgress')
const iconColor = useThemeColor({}, 'recorderIcon')
const tintColor = useThemeColor({}, 'recorderTint')
const timelineColor = useThemeColor({}, 'recorderTimeline')
const positionColor = useThemeColor({}, 'text')
const recordBorderColor = useThemeColor({ light: 'rgba(0,0,0,0.3)' }, 'text')
const recorderBackgroundColor = useThemeColor({}, 'recorderBackground')
const waveformInactiveColor = useThemeColor({}, 'recorderWaveformInactive')
const scale = useSharedValue(1)
const toggleRecording = async () => {
const permissionStatus = await Audio.getPermissionsAsync()
if (!permissionStatus.granted) return
Haptics.selectionAsync()
if (isRecording) {
await recorderRef.current?.stopRecording()
} else {
await recorderRef.current?.startRecording()
}
}
const resetRecording = async () => {
if (isRecording) return
Haptics.selectionAsync()
await recorderRef.current?.resetRecording()
}
const togglePlayback = async () => {
if (isRecording) return
Haptics.selectionAsync()
if (isPlaying) {
await recorderRef.current?.stopPlayback()
} else {
await recorderRef.current?.startPlayback()
}
}
const $recordIndicatorStyles: StyleProp<ViewStyle> = [
$recordIndicator,
useAnimatedStyle(() => ({
borderRadius: interpolate(
scale.value,
[1, RECORDING_INDICATOR_SCALE],
[RECORD_BUTTON_SIZE / 2, Spacing.xs],
Extrapolation.CLAMP
),
transform: [{ scale: scale.value }],
})),
]
return (
<TrueSheet
ref={ref}
sizes={['auto']}
style={[{ backgroundColor }, style]}
contentContainerStyle={[$sheetContent, { paddingBottom: insets.bottom + Spacing.md }]}
{...rest}
>
<Recorder
ref={recorderRef}
tintColor={tintColor}
waveformInactiveColor={waveformInactiveColor}
progressInterval={50}
timelineColor={timelineColor}
backgroundColor={recorderBackgroundColor}
progressBackgroundColor={progressBackgroundColor}
onRecordReset={() => {
scale.value = 1
setIsRecording(false)
setIsPlaying(false)
}}
onRecordStart={() => {
scale.value = withSpring(RECORDING_INDICATOR_SCALE, SPRING_SHORT_CONFIG)
setIsRecording(true)
}}
onRecordStop={(uri, duration, meterings) => {
scale.value = withSpring(1, SPRING_SHORT_CONFIG)
setIsRecording(false)
recorderRef.current?.startPlayback()
// Use this uri. Yay! 🎉
console.log(uri)
console.log(duration)
console.log(meterings)
}}
onPlaybackStart={() => setIsPlaying(true)}
onPlaybackStop={() => setIsPlaying(false)}
onPositionChange={(pos: number) => setPosition(pos)}
/>
<View style={{ padding: Spacing.md, marginTop: Spacing.xxl }}>
<Text style={[$positionText, { color: positionColor ?? '#333333' }]}>
{formatTimer(Math.round(position / 100) * 100, true)}
</Text>
</View>
<Box row justify="space-between" align="center" mt={Spacing.lg}>
<Box>
<TouchableOpacity activeOpacity={0.5} style={$recordControl} onPress={resetRecording}>
<Ionicons name="refresh" size={Spacing.xl} style={{ color: iconColor }} />
</TouchableOpacity>
</Box>
<Box justify="center" align="center" mx={Spacing.xxl}>
<View style={[$recordButtonBackground, { borderColor: recordBorderColor }]} />
<Pressable style={$recordButton} onPress={toggleRecording}>
<Animated.View style={$recordIndicatorStyles} />
</Pressable>
</Box>
<Box>
<TouchableOpacity activeOpacity={0.8} style={$recordControl} onPress={togglePlayback}>
<Ionicons
name={isPlaying ? 'pause' : 'play'}
size={Spacing.xl}
style={{ color: iconColor }}
/>
</TouchableOpacity>
</Box>
</Box>
</TrueSheet>
)
}
)
const $sheetContent: ViewStyle = {
paddingTop: Spacing.xl,
}
const $recordButtonBackground: ViewStyle = {
borderRadius: RECORD_BUTTON_BACKGROUND_SIZE / 2,
height: RECORD_BUTTON_BACKGROUND_SIZE,
width: RECORD_BUTTON_BACKGROUND_SIZE,
borderWidth: 2,
borderColor: 'white',
}
const $recordButton: ViewStyle = {
position: 'absolute',
}
const $recordIndicator: ViewStyle = {
backgroundColor: RECORDING_INDICATOR_COLOR,
borderRadius: RECORD_BUTTON_SIZE / 2,
height: RECORD_BUTTON_SIZE,
width: RECORD_BUTTON_SIZE,
}
const $recordControl: ViewStyle = {
padding: Spacing.md,
}
const $positionText: TextStyle = {
fontWeight: 'medium',
fontSize: 28,
textAlign: 'center',
}