Skip to content

Commit

Permalink
fix android floating point issues
Browse files Browse the repository at this point in the history
  • Loading branch information
yayvery committed Mar 18, 2024
1 parent fc1a76f commit 8093e96
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
22 changes: 15 additions & 7 deletions src/components/bottomSheet/BottomSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import {
getKeyboardAnimationConfigs,
normalizeSnapPoint,
print,
floatingPointEquals,
} from '../../utilities';
import {
DEFAULT_OVER_DRAG_RESISTANCE_FACTOR,
Expand All @@ -78,7 +79,7 @@ import {
DEFAULT_DYNAMIC_SIZING,
DEFAULT_ACCESSIBLE,
DEFAULT_ACCESSIBILITY_LABEL,
DEFAULT_ACCESSIBILITY_ROLE
DEFAULT_ACCESSIBILITY_ROLE,
} from './constants';
import type { BottomSheetMethods, Insets } from '../../types';
import type { BottomSheetProps, AnimateToPositionType } from './types';
Expand Down Expand Up @@ -328,7 +329,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
// extended position = container height - sheet height
const extendedPosition =
animatedContainerHeight.value - animatedSheetHeight.value;
if (animatedPosition.value === extendedPosition)
if (floatingPointEquals(animatedPosition.value, extendedPosition))
return SHEET_STATE.EXTENDED;

// extended position with keyboard =
Expand All @@ -344,13 +345,16 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
if (
keyboardBehavior === KEYBOARD_BEHAVIOR.interactive &&
isInTemporaryPosition.value &&
animatedPosition.value === extendedPositionWithKeyboard
floatingPointEquals(
animatedPosition.value,
extendedPositionWithKeyboard
)
) {
return SHEET_STATE.EXTENDED;
}

// fill parent = 0
if (animatedPosition.value === 0) {
if (floatingPointEquals(animatedPosition.value, 0)) {
return SHEET_STATE.FILL_PARENT;
}

Expand Down Expand Up @@ -633,7 +637,11 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
[_providedOnChange, animatedCurrentIndex]
);
const handleOnAnimate = useCallback(
function handleOnAnimate(toPoint: number, source: ANIMATION_SOURCE, snapPoints: number[]) {
function handleOnAnimate(
toPoint: number,
source: ANIMATION_SOURCE,
snapPoints: number[]
) {
const closedPosition = animatedClosedPosition.value;
const toIndex =
toPoint === closedPosition ? -1 : snapPoints.indexOf(toPoint);
Expand Down Expand Up @@ -700,10 +708,10 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
configs?: WithTimingConfig | WithSpringConfig
) {
if (
position === animatedPosition.value ||
floatingPointEquals(position, animatedPosition.value) ||
position === undefined ||
(animatedAnimationState.value === ANIMATION_STATE.RUNNING &&
position === animatedNextPosition.value)
floatingPointEquals(position, animatedNextPosition.value))
) {
return;
}
Expand Down
18 changes: 12 additions & 6 deletions src/hooks/useGestureEventsHandlersDefault.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
} from '../types';
import { clamp } from '../utilities/clamp';
import { snapPoint } from '../utilities/snapPoint';
import { floatingPointEquals } from '../utilities';

type GestureEventContextType = {
initialPosition: number;
Expand Down Expand Up @@ -136,7 +137,7 @@ export const useGestureEventsHandlersDefault: GestureEventsHandlersHookType =
if (
source === GESTURE_SOURCE.CONTENT &&
isScrollableRefreshable.value &&
animatedPosition.value === highestSnapPoint
floatingPointEquals(animatedPosition.value, highestSnapPoint)
) {
return;
}
Expand All @@ -148,7 +149,10 @@ export const useGestureEventsHandlersDefault: GestureEventsHandlersHookType =
* a negative scrollable content offset when the scrollable is not locked.
*/
const negativeScrollableContentOffset =
(context.value.initialPosition === highestSnapPoint &&
(floatingPointEquals(
context.value.initialPosition,
highestSnapPoint
) &&
source === GESTURE_SOURCE.CONTENT) ||
!context.value.isScrollablePositionLocked
? animatedScrollableContentOffsetY.value * -1
Expand Down Expand Up @@ -184,7 +188,7 @@ export const useGestureEventsHandlersDefault: GestureEventsHandlersHookType =
if (
context.value.isScrollablePositionLocked &&
source === GESTURE_SOURCE.CONTENT &&
animatedPosition.value === highestSnapPoint
floatingPointEquals(animatedPosition.value, highestSnapPoint)
) {
context.value = {
...context.value,
Expand Down Expand Up @@ -258,8 +262,10 @@ export const useGestureEventsHandlersDefault: GestureEventsHandlersHookType =
const handleOnEnd: GestureEventHandlerCallbackType = useWorkletCallback(
function handleOnEnd(source, { translationY, absoluteY, velocityY }) {
const highestSnapPoint = animatedHighestSnapPoint.value;
const isSheetAtHighestSnapPoint =
animatedPosition.value === highestSnapPoint;
const isSheetAtHighestSnapPoint = floatingPointEquals(
animatedPosition.value,
highestSnapPoint
);

/**
* if scrollable is refreshable and sheet position at the highest
Expand Down Expand Up @@ -354,7 +360,7 @@ export const useGestureEventsHandlersDefault: GestureEventsHandlersHookType =
* if destination point is the same as the current position,
* then no need to perform animation.
*/
if (destinationPoint === animatedPosition.value) {
if (floatingPointEquals(destinationPoint, animatedPosition.value)) {
return;
}

Expand Down
6 changes: 6 additions & 0 deletions src/utilities/floatingPointEquals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const EPSILON = 0.1;

export function floatingPointEquals(valueA: number, valueB: number) {
'worklet';
return Math.abs(valueB - valueA) < EPSILON;
}
1 change: 1 addition & 0 deletions src/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { animate } from './animate';
export { getKeyboardAnimationConfigs } from './getKeyboardAnimationConfigs';
export { print } from './logger';
export { noop, workletNoop } from './noop';
export { floatingPointEquals } from './floatingPointEquals';

0 comments on commit 8093e96

Please sign in to comment.