Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(UNT-T28572): seek position layout issue #76

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions src/components/Waveform/Waveform.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
PanResponder,
ScrollView,
View,
type LayoutChangeEvent,
type LayoutRectangle,
type NativeTouchEvent,
} from 'react-native';
Expand Down Expand Up @@ -63,6 +62,7 @@ export const Waveform = forwardRef<IWaveformRef, IWaveform>((props, ref) => {
} = props as StaticWaveform & LiveWaveform;
const viewRef = useRef<View>(null);
const scrollRef = useRef<ScrollView>(null);
const isLayoutCalculated = useRef<boolean>(false);
const [waveform, setWaveform] = useState<number[]>([]);
const [viewLayout, setViewLayout] = useState<LayoutRectangle | null>(null);
const [seekPosition, setSeekPosition] = useState<NativeTouchEvent | null>(
Expand Down Expand Up @@ -403,13 +403,19 @@ export const Waveform = forwardRef<IWaveformRef, IWaveform>((props, ref) => {
const getNumberOfSamples = floor(
(viewLayout?.width ?? 0) / (candleWidth + candleSpace)
);

// when orientation changes, the layout needs to be recalculated
if (viewLayout?.x === 0 && viewLayout?.y === 0) {
isLayoutCalculated.current = false;
}

setNoOfSamples(getNumberOfSamples);
if (mode === 'static') {
getAudioWaveFormForPath(getNumberOfSamples);
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [viewLayout, mode, candleWidth, candleSpace]);
}, [viewLayout?.width, mode, candleWidth, candleSpace]);

useEffect(() => {
if (!isNil(seekPosition)) {
Expand Down Expand Up @@ -516,8 +522,25 @@ export const Waveform = forwardRef<IWaveformRef, IWaveform>((props, ref) => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [panMoving]);

const calculateLayout = (): void => {
viewRef.current?.measureInWindow((x, y, width, height) => {
setViewLayout({ x, y, width, height });
if (x !== 0 || y !== 0) {
// found the position of view in window
isLayoutCalculated.current = true;
}
});
};

const panResponder = useRef(
PanResponder.create({
onStartShouldSetPanResponder: () => {
if (!isLayoutCalculated.current) {
calculateLayout();
}

return true;
},
onMoveShouldSetPanResponder: () => true,
onPanResponderGrant: () => {
setPanMoving(true);
Expand Down Expand Up @@ -556,10 +579,7 @@ export const Waveform = forwardRef<IWaveformRef, IWaveform>((props, ref) => {
<View
ref={viewRef}
style={styles.waveformInnerContainer}
onLayout={(event: LayoutChangeEvent) => {
const { height, width, x, y } = event.nativeEvent.layout;
setViewLayout({ height, width, x, y });
}}
onLayout={calculateLayout}
{...(mode === 'static' ? panResponder.panHandlers : {})}>
<ScrollView
horizontal
Expand Down