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

swap button states #5785

Merged
merged 38 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
46162ee
wip
greg-schrammel May 29, 2024
3a3c341
fix gesture button states
walmat May 29, 2024
b8635be
Safemath pt 2 (#5778)
brunobar79 May 29, 2024
251f247
fix dynamic island overlap on recieve modal (#5672)
BrodyHughes May 29, 2024
ecb04e1
Gas optimizations (#5779)
greg-schrammel May 29, 2024
7e367e1
fix other networks section (#5784)
walmat May 29, 2024
f3307cb
Swaps: fix favorite button press (#5782)
benisgold May 29, 2024
009f887
todo
greg-schrammel May 29, 2024
6fd56f7
Merge remote-tracking branch 'origin/develop' into review-button-states
greg-schrammel May 29, 2024
2ce9e5f
remove console logs
greg-schrammel May 29, 2024
3c86b3b
Merge branch 'develop' into review-button-states
greg-schrammel May 30, 2024
a5799fb
Insufficient Funds
greg-schrammel May 30, 2024
9d816a8
remove todo
greg-schrammel May 30, 2024
a17464e
move cache getter closer to fetcher implentation
greg-schrammel Jun 2, 2024
c60dc66
Merge remote-tracking branch 'origin/develop' into review-button-states
greg-schrammel Jun 3, 2024
93cf94c
Merge branch 'develop' into review-button-states
greg-schrammel Jun 4, 2024
0af8ff4
fix
greg-schrammel Jun 5, 2024
0187af5
Merge remote-tracking branch 'origin/develop' into review-button-states
greg-schrammel Jun 5, 2024
40c2055
:)
greg-schrammel Jun 5, 2024
adf3b51
Merge branch 'develop' into review-button-states
walmat Jun 7, 2024
c7c0102
🍕
greg-schrammel Jun 12, 2024
cba4aed
or equal 🤌
greg-schrammel Jun 12, 2024
2ea366e
remove unused isSameAddress util
greg-schrammel Jun 12, 2024
e6d4382
just reordering declarations
greg-schrammel Jun 12, 2024
31d36b6
error i18n
greg-schrammel Jun 12, 2024
508a13b
fix merge conflicts with import sorting
walmat Jun 13, 2024
76df24c
useGasSharedValues
greg-schrammel Jun 13, 2024
f93b7ad
remove estimating
greg-schrammel Jun 13, 2024
c9bd12f
Merge branch 'develop' into review-button-states
greg-schrammel Jun 13, 2024
23c29c8
Merge branch 'develop' into review-button-states
greg-schrammel Jun 13, 2024
606866f
Merge branch 'develop' into review-button-states
walmat Jun 13, 2024
7697803
fix label flickering
greg-schrammel Jun 14, 2024
da8d964
fix review panel not prompting
walmat Jun 14, 2024
2819c72
Merge branch 'develop' into review-button-states
walmat Jun 14, 2024
fea8f65
Revert "Lint on pre-commit (#5836)"
walmat Jun 14, 2024
955e223
fix a bunch of shit
walmat Jun 14, 2024
8cfad78
Merge branch 'develop' of https://github.com/rainbow-me/rainbow into …
walmat Jun 14, 2024
988541d
Merge branch 'develop' into review-button-states
walmat Jun 14, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,14 @@ export const GestureHandlerV1Button = React.forwardRef(function GestureHandlerV1
if (onPressStartWorklet) onPressStartWorklet();
},
onActive: () => {
console.log('here');
greg-schrammel marked this conversation as resolved.
Show resolved Hide resolved
if (onPressWorklet) onPressWorklet();
if (onPressJS) runOnJS(onPressJS)();
},
});

console.log({ disabled });
greg-schrammel marked this conversation as resolved.
Show resolved Hide resolved

return (
<ConditionalWrap
condition={IS_IOS && !disableButtonPressWrapper}
Expand Down
38 changes: 22 additions & 16 deletions src/__swaps__/screens/Swap/components/SwapActionButton.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/* eslint-disable no-nested-ternary */
import React from 'react';
import { StyleProp, StyleSheet, TextStyle, ViewStyle } from 'react-native';
import Animated, { DerivedValue, useAnimatedStyle, useDerivedValue } from 'react-native-reanimated';
import Animated, { DerivedValue, useAnimatedProps, useAnimatedStyle, useDerivedValue } from 'react-native-reanimated';

import { ButtonPressAnimation } from '@/components/animations';
import { AnimatedText, Box, Column, Columns, globalColors, useColorMode, useForegroundColor } from '@/design-system';
import { ExtendedAnimatedAssetWithColors } from '@/__swaps__/types/assets';
import { getColorValueForThemeWorklet } from '@/__swaps__/utils/swaps';
import { AnimatedText, Box, Column, Columns, globalColors, useColorMode, useForegroundColor } from '@/design-system';
import { GestureHandlerV1Button } from './GestureHandlerV1Button';

const AnimatedGestureHandlerV1Button = Animated.createAnimatedComponent(GestureHandlerV1Button);

export const SwapActionButton = ({
asset,
Expand All @@ -16,13 +18,13 @@ export const SwapActionButton = ({
icon,
iconStyle,
label,
onLongPress,
onPress,
onPressWorklet,
outline,
rightIcon,
scaleTo,
small,
style,
disabled,
}: {
asset: DerivedValue<ExtendedAnimatedAssetWithColors | null>;
borderRadius?: number;
Expand All @@ -31,13 +33,13 @@ export const SwapActionButton = ({
icon?: string | DerivedValue<string | undefined>;
iconStyle?: StyleProp<TextStyle>;
label: string | DerivedValue<string | undefined>;
onLongPress?: () => void;
onPress?: () => void;
onPressWorklet?: () => void;
outline?: boolean;
rightIcon?: string;
scaleTo?: number;
small?: boolean;
style?: ViewStyle;
disabled?: DerivedValue<boolean | undefined>;
}) => {
const { isDarkMode } = useColorMode();
const fallbackColor = useForegroundColor('label');
Expand Down Expand Up @@ -80,6 +82,7 @@ export const SwapActionButton = ({
},
shadowOpacity: isDarkMode ? 0.2 : small ? 0.2 : 0.36,
shadowRadius: isDarkMode ? 26 : small ? 9 : 15,
opacity: disabled?.value ? 0.6 : 1,
};
});

Expand All @@ -97,15 +100,18 @@ export const SwapActionButton = ({
return rightIcon;
});

const buttonAnimatedProps = useAnimatedProps(() => {
return {
disabled: disabled?.value,
scaleTo: disabled?.value ? 1 : scaleTo || (hugContent ? undefined : 0.925),
};
});

return (
<ButtonPressAnimation
onLongPress={onLongPress}
onPress={onPress}
scaleTo={scaleTo || (hugContent ? undefined : 0.925)}
style={{
...(hugContent && feedActionButtonStyles.buttonWrapper),
...(style || {}),
}}
<AnimatedGestureHandlerV1Button
animatedProps={buttonAnimatedProps}
onPressStartWorklet={onPressWorklet}
style={[hugContent && feedActionButtonStyles.buttonWrapper, style]}
>
<Box
as={Animated.View}
Expand Down Expand Up @@ -145,7 +151,7 @@ export const SwapActionButton = ({
)}
</Columns>
</Box>
</ButtonPressAnimation>
</AnimatedGestureHandlerV1Button>
);
};

Expand Down
26 changes: 12 additions & 14 deletions src/__swaps__/screens/Swap/components/SwapBottomPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import React from 'react';
import Animated, { runOnUI, useAnimatedStyle, withSpring } from 'react-native-reanimated';
import { StyleSheet } from 'react-native';
import { getSoftMenuBarHeight } from 'react-native-extra-dimensions-android';
import { PanGestureHandler } from 'react-native-gesture-handler';
import Animated, { useAnimatedStyle, useDerivedValue, withSpring } from 'react-native-reanimated';

import { Box, Column, Columns, Separator, globalColors, useColorMode } from '@/design-system';
import { safeAreaInsetValues } from '@/utils';

import { LIGHT_SEPARATOR_COLOR, SEPARATOR_COLOR, THICK_BORDER_WIDTH, springConfig } from '@/__swaps__/screens/Swap/constants';
import { NavigationSteps, useSwapContext } from '@/__swaps__/screens/Swap/providers/swap-provider';
import { IS_ANDROID } from '@/env';
import { useSwapContext, NavigationSteps } from '@/__swaps__/screens/Swap/providers/swap-provider';

import { opacity } from '@/__swaps__/utils/swaps';
import { useBottomPanelGestureHandler } from '../hooks/useBottomPanelGestureHandler';
Expand All @@ -20,15 +20,8 @@ import { SwapActionButton } from './SwapActionButton';

export function SwapBottomPanel() {
const { isDarkMode } = useColorMode();
const {
confirmButtonIcon,
confirmButtonIconStyle,
confirmButtonLabel,
internalSelectedOutputAsset,
AnimatedSwapStyles,
SwapNavigation,
configProgress,
} = useSwapContext();
const { confirmButtonIconStyle, confirmButtonProps, internalSelectedOutputAsset, AnimatedSwapStyles, SwapNavigation, configProgress } =
useSwapContext();

const { swipeToDismissGestureHandler, gestureY } = useBottomPanelGestureHandler();

Expand All @@ -44,6 +37,10 @@ export function SwapBottomPanel() {
};
});

const icon = useDerivedValue(() => confirmButtonProps.value.icon);
const label = useDerivedValue(() => confirmButtonProps.value.label);
const disabled = useDerivedValue(() => confirmButtonProps.value.disabled);

return (
// @ts-expect-error Property 'children' does not exist on type
<PanGestureHandler maxPointers={1} onGestureEvent={swipeToDismissGestureHandler}>
Expand Down Expand Up @@ -74,11 +71,12 @@ export function SwapBottomPanel() {
</Box>
</Column>
<SwapActionButton
onPress={() => runOnUI(SwapNavigation.handleSwapAction)()}
onPressWorklet={SwapNavigation.handleSwapAction}
asset={internalSelectedOutputAsset}
icon={confirmButtonIcon}
icon={icon}
iconStyle={confirmButtonIconStyle}
label={confirmButtonLabel}
label={label}
disabled={disabled}
scaleTo={0.9}
/>
</Columns>
Expand Down
12 changes: 6 additions & 6 deletions src/__swaps__/screens/Swap/components/SwapInputAsset.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import MaskedView from '@react-native-masked-view/masked-view';
import React from 'react';
import { StyleSheet, StatusBar } from 'react-native';
import { StatusBar, StyleSheet } from 'react-native';
import Animated, { runOnUI, useDerivedValue } from 'react-native-reanimated';
import { ScreenCornerRadius } from 'react-native-screen-corner-radius';

import { AnimatedText, Box, Column, Columns, Stack, useColorMode } from '@/design-system';

import { BalanceBadge } from '@/__swaps__/screens/Swap/components/BalanceBadge';
import { FadeMask } from '@/__swaps__/screens/Swap/components/FadeMask';
import { GestureHandlerV1Button } from '@/__swaps__/screens/Swap/components/GestureHandlerV1Button';
import { SwapActionButton } from '@/__swaps__/screens/Swap/components/SwapActionButton';
import { FadeMask } from '@/__swaps__/screens/Swap/components/FadeMask';
import { SwapInput } from '@/__swaps__/screens/Swap/components/SwapInput';
import { BalanceBadge } from '@/__swaps__/screens/Swap/components/BalanceBadge';
import { TokenList } from '@/__swaps__/screens/Swap/components/TokenList/TokenList';
import { BASE_INPUT_WIDTH, INPUT_INNER_WIDTH, INPUT_PADDING, THICK_BORDER_WIDTH } from '@/__swaps__/screens/Swap/constants';
import { IS_ANDROID } from '@/env';
import { useSwapContext } from '@/__swaps__/screens/Swap/providers/swap-provider';
import { useAssetsToSell } from '@/__swaps__/screens/Swap/hooks/useAssetsToSell';
import { useSwapContext } from '@/__swaps__/screens/Swap/providers/swap-provider';
import { isSameAssetWorklet } from '@/__swaps__/utils/assets';
import { IS_ANDROID } from '@/env';
import { AmimatedSwapCoinIcon } from './AnimatedSwapCoinIcon';

function SwapInputActionButton() {
Expand All @@ -34,7 +34,7 @@ function SwapInputActionButton() {
disableShadow={isDarkMode}
hugContent
label={label}
onPress={runOnUI(SwapNavigation.handleInputPress)}
onPressWorklet={SwapNavigation.handleInputPress}
rightIcon={'􀆏'}
small
/>
Expand Down
2 changes: 1 addition & 1 deletion src/__swaps__/screens/Swap/components/SwapOutputAsset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function SwapOutputActionButton() {
disableShadow={isDarkMode}
hugContent
label={label}
onPress={runOnUI(SwapNavigation.handleOutputPress)}
onPressWorklet={SwapNavigation.handleOutputPress}
rightIcon={'􀆏'}
small
/>
Expand Down
61 changes: 23 additions & 38 deletions src/__swaps__/screens/Swap/providers/swap-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,13 @@ interface SwapContextType {
SwapNavigation: ReturnType<typeof useSwapNavigation>;
SwapWarning: ReturnType<typeof useSwapWarning>;

confirmButtonIcon: Readonly<SharedValue<string>>;
confirmButtonLabel: Readonly<SharedValue<string>>;
confirmButtonProps: Readonly<
SharedValue<{
label: string;
icon?: string;
disabled?: boolean;
}>
>;
confirmButtonIconStyle: StyleProp<TextStyle>;
}

Expand Down Expand Up @@ -244,53 +249,34 @@ export const SwapProvider = ({ children }: SwapProviderProps) => {
runOnUI(updateAssetValue)({ type, asset: parseAssetAndExtend({ asset }) });
};

const confirmButtonIcon = useDerivedValue(() => {
const confirmButtonProps = useDerivedValue(() => {
if (configProgress.value === NavigationSteps.SHOW_REVIEW) {
return '􀎽';
} else if (configProgress.value === NavigationSteps.SHOW_GAS) {
return '􀆅';
return { icon: '􀎽', label: 'Hold to Swap', disabled: false };
greg-schrammel marked this conversation as resolved.
Show resolved Hide resolved
greg-schrammel marked this conversation as resolved.
Show resolved Hide resolved
}

if (configProgress.value === NavigationSteps.SHOW_GAS) {
return { icon: '􀆅', label: 'Save', disabled: false };
greg-schrammel marked this conversation as resolved.
Show resolved Hide resolved
}

if (isFetching.value) {
return '';
return { label: 'Fetching...', disabled: true };
greg-schrammel marked this conversation as resolved.
Show resolved Hide resolved
}

const hasSelectedAssets = internalSelectedInputAsset.value && internalSelectedOutputAsset.value;
if (!hasSelectedAssets) return { label: 'Select Token', disabled: true };

const isInputZero = Number(SwapInputController.inputValues.value.inputAmount) === 0;
const isOutputZero = Number(SwapInputController.inputValues.value.outputAmount) === 0;

if (SwapInputController.inputMethod.value !== 'slider' && (isInputZero || isOutputZero) && !isFetching.value) {
return '';
} else if (SwapInputController.inputMethod.value === 'slider' && SwapInputController.percentageToSwap.value === 0) {
return '';
} else {
return '􀕹';
if (SwapInputController.percentageToSwap.value === 0 || isInputZero || isOutputZero) {
return { label: 'Enter Amount', disabled: true };
}
});

const confirmButtonLabel = useDerivedValue(() => {
if (configProgress.value === NavigationSteps.SHOW_REVIEW) {
return 'Hold to Swap';
} else if (configProgress.value === NavigationSteps.SHOW_GAS) {
return 'Save';
}
if (quote.value && 'error' in quote.value) return { label: 'Error', disabled: true };

if (isFetching.value) {
return 'Fetching prices';
}

const isInputZero = Number(SwapInputController.inputValues.value.inputAmount) === 0;
const isOutputZero = Number(SwapInputController.inputValues.value.outputAmount) === 0;
// TODO: after the tx is built, we should check if the user has enough balance to cover the tx

if (SwapInputController.inputMethod.value !== 'slider' && (isInputZero || isOutputZero) && !isFetching.value) {
return 'Enter Amount';
} else if (
SwapInputController.inputMethod.value === 'slider' &&
(SwapInputController.percentageToSwap.value === 0 || isInputZero || isOutputZero)
) {
return 'Enter Amount';
} else {
return 'Review';
}
return { icon: '􀕹', label: 'Review', disabled: false };
});

const confirmButtonIconStyle = useAnimatedStyle(() => {
Expand Down Expand Up @@ -356,8 +342,7 @@ export const SwapProvider = ({ children }: SwapProviderProps) => {
SwapNavigation,
SwapWarning,

confirmButtonIcon,
confirmButtonLabel,
confirmButtonProps,
confirmButtonIconStyle,
}}
>
Expand Down