Skip to content

Commit

Permalink
fix: refacto record component
Browse files Browse the repository at this point in the history
Signed-off-by: clegirar <clemntgirard@gmail.com>
  • Loading branch information
clegirar committed Mar 17, 2021
1 parent 8d63d44 commit 3e3d3f8
Show file tree
Hide file tree
Showing 9 changed files with 380 additions and 379 deletions.
3 changes: 3 additions & 0 deletions js/__mocks__/react-native-device-info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
jest.mock('react-native-device-info', () =>
require('react-native-device-info/jest/react-native-device-info-mock'),
)
1 change: 0 additions & 1 deletion js/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 21 additions & 11 deletions js/packages/components/chat/common.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
Keyboard,
} from 'react-native'
import { Icon, Text } from '@ui-kitten/components'
import { widthPercentageToDP as wp } from 'react-native-responsive-screen'
import DeviceInfo from 'react-native-device-info'

import { useStyles } from '@berty-tech/styles'
Expand All @@ -27,7 +26,7 @@ import moment from 'moment'
import { check, PERMISSIONS, request, RESULTS } from 'react-native-permissions'
import ImagePicker from 'react-native-image-crop-picker'
import { SecurityAccess } from './file-uploads/SecurityAccess'
import { RecordComponent } from './RecordComponent'
import { RecordComponent } from './record/RecordComponent'

const {
PlatformConstants: { interfaceIdiom: deviceType },
Expand All @@ -48,6 +47,8 @@ const useStylesChatFooter = () => {
}
}

const aDuration = 600

// create interpolations
export const createAnimationInterpolation = (
value: Animated.Value,
Expand Down Expand Up @@ -186,12 +187,11 @@ export const ChatFooter: React.FC<{
const _aPaddingLeft = useRef(new Animated.Value(0)).current
const _aOpacity = useRef(new Animated.Value(0)).current
const _aOpacitySendButton = useRef(new Animated.Value(0)).current
const aDuration = 600

const aMaxWidth = createAnimationInterpolation(_aMaxWidth, [wp('47%'), wp('69%')])
const aMaxWidth = createAnimationInterpolation(_aMaxWidth, [0, -(94 * scaleSize)])
const aFixLeft = createAnimationInterpolation(_aFixLeft, [0, 45 * scaleSize])
const aFixMicro = createAnimationInterpolation(_aFixMicro, [0 * scaleSize, -92 * scaleSize])
const aFixSend = createAnimationInterpolation(_aFixMicro, [50, -53 * scaleSize])
const aFixSend = createAnimationInterpolation(_aFixMicro, [50, -45 * scaleSize])
const aPaddingLeft = createAnimationInterpolation(_aPaddingLeft, [0, 45])
const aOpacity = createAnimationInterpolation(_aOpacity, [1, 0])
const aOpacitySendButton = createAnimationInterpolation(_aOpacity, [0, 1])
Expand Down Expand Up @@ -273,7 +273,13 @@ export const ChatFooter: React.FC<{
},
]}
>
<Icon name='mic' height={20 * scaleSize} width={20 * scaleSize} fill={color.white} />
<Icon
name='microphone-footer'
pack='custom'
height={18 * scaleSize}
width={18 * scaleSize}
fill={color.white}
/>
</Animated.View>
}
convPk={convPk}
Expand All @@ -287,6 +293,7 @@ export const ChatFooter: React.FC<{
row.fill,
{
alignItems: 'center',
flexDirection: 'row',
},
]}
>
Expand Down Expand Up @@ -335,16 +342,17 @@ export const ChatFooter: React.FC<{
<Animated.View
style={[
border.radius.medium,
padding.horizontal.small,
padding.left.small,
{
justifyContent: 'center',
backgroundColor: _isFocused ? '#E8E9FC99' : '#F7F8FF',
width: aMaxWidth,
marginRight: aMaxWidth,
height: 42 * scaleSize,
right: aFixLeft,
marginLeft: 9 * scaleSize,
zIndex: 100,
elevation: 100,
flex: 1,
},
]}
>
Expand All @@ -363,8 +371,8 @@ export const ChatFooter: React.FC<{
text.bold.small,
{
fontFamily: 'Open Sans',
flex: 1,
fontSize: 15 * scaleSize,
flex: 1,
},
]}
placeholder={placeholder}
Expand Down Expand Up @@ -404,6 +412,7 @@ export const ChatFooter: React.FC<{
opacity: aOpacity,
flexDirection: 'row',
paddingLeft: 15 * scaleSize,
right: aFixMicro,
}}
>
<TouchableOpacity
Expand Down Expand Up @@ -463,8 +472,9 @@ export const ChatFooter: React.FC<{
>
<Icon
name='camera'
height={20 * scaleSize}
width={20 * scaleSize}
pack='custom'
height={16 * scaleSize}
width={16 * scaleSize}
fill={color.white}
/>
</TouchableOpacity>
Expand Down
5 changes: 4 additions & 1 deletion js/packages/components/chat/message/AudioMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import { getSource } from '../../utils'
import moment from 'moment'
import { EndError, PlayerItemMetadata, useMusicPlayer } from '@berty-tech/music-player'
import beapi from '@berty-tech/api'
import { limitIntensities, voiceMemoFilename } from '@berty-tech/components/chat/RecordComponent'
import {
limitIntensities,
voiceMemoFilename,
} from '@berty-tech/components/chat/record/RecordComponent'
import { playSoundAsync } from '@berty-tech/store/sounds'

const volumeValueShown = 50
Expand Down
158 changes: 158 additions & 0 deletions js/packages/components/chat/record/PreviewComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import React, { useMemo, useState } from 'react'
import { useStyles } from '@berty-tech/styles'
import { useTranslation } from 'react-i18next'
import { TouchableOpacity, View } from 'react-native'
import { Icon } from '@ui-kitten/components'
import { readFile } from 'react-native-fs'
import { playSoundFile } from '@berty-tech/store/sounds'
import { WaveForm } from '@berty-tech/components/chat/message/AudioMessage'
import {
limitIntensities,
RecordingState,
volumeValuesAttached,
volumeValueLowest,
volumeValuePrecision,
} from '@berty-tech/components/chat/record/RecordComponent'

export const PreviewComponent: React.FC<{
meteredValuesRef: React.MutableRefObject<number[]>
recordDuration: number | null
recordFilePath: string
clearRecordingInterval: ReturnType<typeof setInterval> | null
setRecordingState: React.Dispatch<React.SetStateAction<RecordingState>>
setHelpMessageValue: ({ message, delay }: { message: string; delay?: number | undefined }) => void
}> = ({
meteredValuesRef,
recordDuration,
recordFilePath,
clearRecordingInterval,
setRecordingState,
setHelpMessageValue,
}) => {
const [{ border, padding, margin, color }, { scaleSize }] = useStyles()
const { t } = useTranslation()
const [player, setPlayer] = useState<any>(null)
const isPlaying = useMemo(() => player?.isPlaying === true, [player?.isPlaying])

return (
<View
style={[{ flex: 1, flexDirection: 'row', alignItems: 'center' }, margin.horizontal.medium]}
>
<TouchableOpacity
style={[
padding.horizontal.small,
margin.right.small,
{
alignItems: 'center',
justifyContent: 'center',
width: 36 * scaleSize,
height: 36 * scaleSize,
backgroundColor: color.red,
borderRadius: 18,
},
]}
onPress={() => {
clearInterval(clearRecordingInterval)
setHelpMessageValue({
message: t('audio.record.tooltip.not-sent'),
})
setRecordingState(RecordingState.PENDING_CANCEL)
}}
>
<Icon
name='trash-outline'
height={20 * scaleSize}
width={20 * scaleSize}
fill={color.white}
/>
</TouchableOpacity>
<View
style={[
border.radius.medium,
margin.right.small,
padding.left.small,
{
height: 50,
flex: 1,
backgroundColor: '#F7F8FF',
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
},
]}
>
<View
style={[
{
height: '100%',
flex: 1,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
},
]}
>
<TouchableOpacity
onPress={() => {
if (player?.isPlaying) {
player?.pause()
} else if (player?.isPaused) {
player?.playPause()
} else {
readFile(recordFilePath, 'base64')
.then((response) => {
console.log('SUCCESS')
setPlayer(playSoundFile(response))
})
.catch((err) => {
console.error(err)
})
}
}}
>
<Icon
name={isPlaying ? 'pause' : 'play'}
fill='#4F58C0'
height={18 * scaleSize}
width={18 * scaleSize}
pack='custom'
/>
</TouchableOpacity>
<WaveForm
intensities={limitIntensities(
meteredValuesRef.current.map((v) =>
Math.round((v - volumeValueLowest) * volumeValuePrecision),
),
volumeValuesAttached,
)}
currentTime={isPlaying && player?.currentTime}
duration={recordDuration}
/>
</View>
</View>
<TouchableOpacity
style={[
padding.horizontal.small,
{
alignItems: 'center',
justifyContent: 'center',
width: 36 * scaleSize,
height: 36 * scaleSize,
backgroundColor: color.blue,
borderRadius: 18,
},
]}
onPress={() => {
setRecordingState(RecordingState.COMPLETE)
}}
>
<Icon
name='paper-plane-outline'
width={20 * scaleSize}
height={20 * scaleSize}
fill={color.white}
/>
</TouchableOpacity>
</View>
)
}
Loading

0 comments on commit 3e3d3f8

Please sign in to comment.