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

feat(swap): Initial Swap screen redesign #5409

Merged
merged 15 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 3 additions & 2 deletions locales/base/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -1765,10 +1765,11 @@
"insufficientFunds": "Insufficient {{token}} balance. Try a smaller amount or choose a different asset.",
"fetchSwapQuoteFailed": "Sorry for the delay! It's taking longer than usual to get the best exchange rate. Please try again in a few minutes.",
"disclaimer": "Best rate determined by decentralized sources. <0>Learn more</0>",
"swapFromTokenSelection": "SWAP FROM",
"swapToTokenSelection": "SWAP TO",
jophish marked this conversation as resolved.
Show resolved Hide resolved
"tokenUsdValueUnknown": "-",
"unsupportedSwapTokens": "The selected tokens aren't supported by our swap providers.",
"selectToken": "Select token",
jophish marked this conversation as resolved.
Show resolved Hide resolved
"selectTokenTitle": "Select Token",
jophish marked this conversation as resolved.
Show resolved Hide resolved
"onNetwork": "on {{networkName}}",
"switchedToNetworkWarning": {
"title": "You switched to the {{networkName}} network",
"body_swapFrom": "Select a token to swap from on the {{networkName}} network to continue.",
Expand Down
1 change: 1 addition & 0 deletions src/analytics/Events.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,7 @@ export enum SwapEvents {
swap_show_info = 'swap_show_info',
swap_show_fund_your_wallet = 'swap_show_fund_your_wallet',
swap_add_funds = 'swap_add_funds',
swap_switch_tokens = 'swap_switch_tokens',
}

export enum CeloNewsEvents {
Expand Down
4 changes: 4 additions & 0 deletions src/analytics/Properties.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1317,6 +1317,10 @@ interface SwapEventsProperties {
}
[SwapEvents.swap_show_fund_your_wallet]: undefined
[SwapEvents.swap_add_funds]: undefined
[SwapEvents.swap_switch_tokens]: {
fromTokenId: string | undefined
toTokenId: string | undefined
}
}

interface CeloNewsEventsProperties {
Expand Down
1 change: 1 addition & 0 deletions src/analytics/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ export const eventDocs: Record<AnalyticsEventType, string> = {
[SwapEvents.swap_show_info]: `When a user taps an info icon to show more information on the swap screen`,
[SwapEvents.swap_show_fund_your_wallet]: `When "Fund your wallet" bottom sheet is displayed`,
[SwapEvents.swap_add_funds]: `When user taps "Add funds" button is "Fund your wallet" bottom sheet`,
[SwapEvents.swap_switch_tokens]: `When a user taps the button to switch tokens`,
jophish marked this conversation as resolved.
Show resolved Hide resolved
[CeloNewsEvents.celo_news_screen_open]: `When the screen is mounted`,
[CeloNewsEvents.celo_news_article_tap]: `When a user taps on a news article`,
[CeloNewsEvents.celo_news_bottom_read_more_tap]: `When a user taps on the read more button at the bottom of the screen`,
Expand Down
5 changes: 3 additions & 2 deletions src/icons/DownIndicator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import Svg, { Path } from 'svgs'
export interface Props {
color?: string
testID?: string
size?: number
}

function DownIndicator({ color = colors.error, testID }: Props) {
function DownIndicator({ color = colors.error, testID, size = 10 }: Props) {
return (
<Svg width="10" height="10" viewBox="0 0 10 10" fill="none" testID={testID}>
<Svg width={size} height={size} viewBox="0 0 10 10" fill="none" testID={testID}>
<Path
fill={color}
d="M4.369 0H5.63v7.576l3.473-3.472L10 5l-5 5-5-5 .896-.896L4.37 7.576V0Z"
Expand Down
258 changes: 114 additions & 144 deletions src/swap/SwapAmountInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import fontStyles, { typeScale } from 'src/styles/fonts'
import { Spacing } from 'src/styles/styles'
import { TokenBalance } from 'src/tokens/slice'
import { NETWORK_NAMES } from 'src/shared/conts'

interface Props {
label: string
onInputChange(value: string): void
inputValue?: string | null
parsedInputValue?: BigNumber | null
Expand All @@ -38,7 +38,6 @@
}

const SwapAmountInput = ({
label,
onInputChange,
inputValue,
parsedInputValue,
Expand Down Expand Up @@ -68,137 +67,123 @@

return (
<View style={[styles.container, style]} testID="SwapAmountInput">
<Text style={styles.label}>{label}</Text>
<View style={styles.contentContainer}>
<View style={styles.inputContainer}>
<TextInput
forwardedRef={textInputRef}
onChangeText={(value) => {
handleSetStartPosition(undefined)
onInputChange(value)
}}
value={inputValue || undefined}
placeholder="0"
// hide input when loading so that the value is not visible under the loader
style={{ opacity: loading ? 0 : 1 }}
editable={editable && !loading}
keyboardType="decimal-pad"
// Work around for RN issue with Samsung keyboards
// https://github.com/facebook/react-native/issues/22005
autoCapitalize="words"
autoFocus={autoFocus}
// unset lineHeight to allow ellipsis on long inputs on iOS
inputStyle={[styles.inputText, inputError ? styles.inputError : {}]}
testID="SwapAmountInput/Input"
onBlur={() => {
handleSetStartPosition(0)
}}
onFocus={() => {
handleSetStartPosition(inputValue?.length ?? 0)
}}
onSelectionChange={() => {
handleSetStartPosition(undefined)
}}
selection={
Platform.OS === 'android' && typeof startPosition === 'number'
? { start: startPosition }
: undefined
}
/>
{loading && (
<View style={[styles.loaderContainer, { paddingVertical: Spacing.Small12 }]}>
<SkeletonPlaceholder
borderRadius={100} // ensure rounded corners with font scaling
backgroundColor={Colors.gray2}
highlightColor={Colors.white}
testID="SwapAmountInput/Loader"
>
<View style={styles.loader} />
</SkeletonPlaceholder>
{token ? (
<View style={styles.tokenInfo}>
<TokenIcon token={token} size={IconSize.MEDIUM} />
<View style={styles.tokenInfoText}>
<Text style={styles.tokenName}>{token.symbol}</Text>
<Text style={styles.tokenNetwork}>
{t('swapScreen.onNetwork', { networkName: NETWORK_NAMES[token.networkId] })}
</Text>
</View>
)}
</View>
{onPressMax && (
<Touchable
borderless
onPress={() => {
onPressMax()
textInputRef.current?.blur()
}}
style={styles.maxButton}
testID="SwapAmountInput/MaxButton"
>
<Text style={styles.maxText}>{t('max')}</Text>
</Touchable>
</View>
) : (
<Text style={styles.tokenNamePlaceholder}>{buttonPlaceholder}</Text>
)}
<Touchable
borderless
onPress={onSelectToken}
style={styles.tokenSelectButton}
testID="SwapAmountInput/TokenSelect"
>
{token ? (
<>
<TokenIcon token={token} viewStyle={styles.tokenImage} size={IconSize.SMALL} />
<Text style={styles.tokenName}>{token.symbol}</Text>
<DownArrowIcon color={Colors.gray5} />
</>
) : (
<>
<Text style={styles.tokenNamePlaceholder}>{buttonPlaceholder}</Text>
<DownArrowIcon color={Colors.gray5} />
</>
)}
<Touchable borderless onPress={onSelectToken} testID="SwapAmountInput/TokenSelect">
<DownArrowIcon height={24} color={Colors.gray3} />
</Touchable>
</View>
<View testID="SwapAmountInput/FiatValue">
<Text
style={[
styles.fiatValue,
{
opacity: loading || !parsedInputValue?.gt(0) || !token ? 0 : 1,
},
]}
>
<TokenDisplay
amount={parsedInputValue ?? 0}
showLocalAmount
showApprox
errorFallback={t('swapScreen.tokenUsdValueUnknown') ?? undefined}
tokenId={token?.tokenId}
/>
</Text>
{loading && (
<View style={styles.loaderContainer}>
<SkeletonPlaceholder
borderRadius={100} // ensure rounded corners with font scaling
backgroundColor={Colors.gray2}
highlightColor={Colors.white}
testID="SwapAmountInput/FiatValueLoader"
>
<View style={styles.fiatValueLoader} />
</SkeletonPlaceholder>
{token && (
<View style={[styles.contentContainer, styles.bottomContainer]}>
<View style={styles.inputContainer}>
<TextInput
forwardedRef={textInputRef}
onChangeText={(value) => {
handleSetStartPosition(undefined)
onInputChange(value)
}}
value={inputValue || undefined}
placeholder="0"
// hide input when loading so that the value is not visible under the loader
style={{ opacity: loading ? 0 : 1 }}
editable={editable && !loading}
keyboardType="decimal-pad"
// Work around for RN issue with Samsung keyboards
// https://github.com/facebook/react-native/issues/22005
autoCapitalize="words"
autoFocus={autoFocus}
// unset lineHeight to allow ellipsis on long inputs on iOS
inputStyle={[styles.inputText, inputError ? styles.inputError : {}]}
testID="SwapAmountInput/Input"
onBlur={() => {
handleSetStartPosition(0)

Check warning on line 111 in src/swap/SwapAmountInput.tsx

View check run for this annotation

Codecov / codecov/patch

src/swap/SwapAmountInput.tsx#L110-L111

Added lines #L110 - L111 were not covered by tests
}}
onFocus={() => {

Check warning on line 113 in src/swap/SwapAmountInput.tsx

View check run for this annotation

Codecov / codecov/patch

src/swap/SwapAmountInput.tsx#L113

Added line #L113 was not covered by tests
handleSetStartPosition(inputValue?.length ?? 0)
}}
onSelectionChange={() => {
handleSetStartPosition(undefined)

Check warning on line 117 in src/swap/SwapAmountInput.tsx

View check run for this annotation

Codecov / codecov/patch

src/swap/SwapAmountInput.tsx#L116-L117

Added lines #L116 - L117 were not covered by tests
}}
selection={
Platform.OS === 'android' && typeof startPosition === 'number'
? { start: startPosition }
: undefined
}
/>
{loading && (
<View style={styles.loaderContainer}>
<SkeletonPlaceholder
borderRadius={100} // ensure rounded corners with font scaling
backgroundColor={Colors.gray2}
highlightColor={Colors.white}
testID="SwapAmountInput/Loader"
>
<View style={styles.loader} />
</SkeletonPlaceholder>
</View>
)}
</View>
)}
</View>
{onPressMax && (
<Touchable
borderless
onPress={() => {
onPressMax()
textInputRef.current?.blur()
}}
style={styles.maxButton}
testID="SwapAmountInput/MaxButton"
>
<Text style={styles.maxText}>{t('max')}</Text>
</Touchable>
)}
{!loading && parsedInputValue?.gt(0) && token && (
<Text style={styles.fiatValue} testID="SwapAmountInput/FiatValue">
<TokenDisplay
amount={parsedInputValue ?? 0}
showLocalAmount
showApprox
errorFallback={t('swapScreen.tokenUsdValueUnknown') ?? undefined}
tokenId={token?.tokenId}
/>
</Text>
)}
</View>
)}
</View>
)
}

const styles = StyleSheet.create({
container: {
padding: Spacing.Regular16,
backgroundColor: Colors.gray1,
borderColor: Colors.gray2,
borderWidth: 1,
},
label: {
...fontStyles.xsmall,
fontWeight: 'bold',
tokenInfo: {
flexDirection: 'row',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think you need an alignItems center here so that the icon is vertically centered. if you increase your device font size, the icon is no longer centered currently

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bump on this! :)

},
contentContainer: {
height: 64,
paddingHorizontal: Spacing.Regular16,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
},
bottomContainer: {
borderColor: Colors.gray2,
borderTopWidth: 1,
},
inputContainer: {
flex: 1,
Expand All @@ -214,60 +199,45 @@
paddingVertical: Spacing.Smallest8,
},
loaderContainer: {
position: 'absolute',
top: 0,
left: 0,
width: '100%',
height: '100%',
paddingVertical: Spacing.Small12,
},
loader: {
height: '100%',
width: '100%',
},
fiatValueLoader: {
height: '100%',
width: '40%',
},
maxButton: {
backgroundColor: Colors.white,
borderWidth: 1,
borderColor: Colors.gray2,
borderRadius: 4,
paddingVertical: 2,
paddingHorizontal: 4,
marginRight: Spacing.Smallest8,
paddingVertical: 4,
paddingHorizontal: 6,
jophish marked this conversation as resolved.
Show resolved Hide resolved
},
maxText: {
...fontStyles.small,
...typeScale.labelXXSmall,
color: Colors.gray5,
fontSize: 10,
},
tokenSelectButton: {
flexDirection: 'row',
alignItems: 'center',
backgroundColor: Colors.white,
borderWidth: 1,
borderColor: Colors.gray2,
borderRadius: 100,
paddingHorizontal: Spacing.Smallest8,
paddingVertical: 4,
},
tokenName: {
...fontStyles.small600,
...typeScale.labelSemiBoldXSmall,
paddingHorizontal: 4,
},
tokenNamePlaceholder: {
...fontStyles.small600,
tokenNetwork: {
...typeScale.bodyXSmall,
color: Colors.gray4,
paddingHorizontal: 4,
color: Colors.primary,
},
tokenImage: {
width: 24,
height: 24,
borderRadius: 12,
tokenInfoText: {
paddingLeft: Spacing.Smallest8,
},
tokenNamePlaceholder: {
...typeScale.labelMedium,
paddingHorizontal: 4,
color: Colors.gray3,
},
fiatValue: {
...typeScale.bodyXSmall,
paddingLeft: Spacing.Smallest8,
color: Colors.gray4,
},
})
Expand Down
Loading
Loading