Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ enum class FocusedField { NONE, IN_AMOUNT, OUT_AMOUNT, PRICE }

private const val MAX_DISPLAY_ORDER_COUNT: Int = 10

internal fun shouldShowLimitOrderOpenOrdersCard(
inputText: String,
outputText: String,
limitPriceText: String,
focusedField: FocusedField,
isKeyboardVisible: Boolean,
): Boolean = inputText.isBlank() &&
outputText.isBlank() &&
limitPriceText.isBlank() &&
focusedField == FocusedField.NONE &&
!isKeyboardVisible

private fun formatBalanceInput(balance: String?, isWeb3: Boolean): String {
val amount = balance?.toBigDecimalOrNull() ?: return ""
if (amount <= BigDecimal.ZERO) return ""
Expand Down Expand Up @@ -161,7 +173,7 @@ fun LimitOrderContent(
var isReverse by remember { mutableStateOf(false) }
val walletId = if (inMixin) Session.getAccountId()!! else Web3Signer.currentWalletId

var focusedField by remember { mutableStateOf(FocusedField.PRICE) }
var focusedField by remember { mutableStateOf(FocusedField.NONE) }

var fromToken by remember(from, to, isReverse) {
mutableStateOf(if (isReverse) to else from)
Expand Down Expand Up @@ -240,6 +252,18 @@ fun LimitOrderContent(
}
val availableFromBalance = availableFromBalanceValue.stripTrailingZeros().toPlainString()
KeyboardAwareBox(modifier = Modifier.fillMaxHeight(), content = { availableHeight ->
LaunchedEffect(availableHeight, inputText, outputText, limitPriceText) {
if (availableHeight == null && inputText.isBlank() && outputText.isBlank() && limitPriceText.isBlank()) {
focusedField = FocusedField.NONE
}
}
val showOpenOrdersCard = shouldShowLimitOrderOpenOrdersCard(
inputText = inputText,
outputText = outputText,
limitPriceText = limitPriceText,
focusedField = focusedField,
isKeyboardVisible = availableHeight != null,
)
Column(
modifier = if (availableHeight != null) {
Modifier
Expand Down Expand Up @@ -314,10 +338,13 @@ fun LimitOrderContent(
InputArea(modifier = Modifier.onFocusChanged {
if (it.isFocused) {
focusedField = FocusedField.IN_AMOUNT
} else if (focusedField == FocusedField.IN_AMOUNT) {
focusedField = FocusedField.NONE
}
}, token = fromToken, text = inputText, title = stringResource(id = R.string.swap_send), readOnly = false, selectClick = {
keyboardController?.hide()
focusManager.clearFocus()
focusedField = FocusedField.NONE
onSelectToken(isReverse, if (isReverse) SelectTokenType.To else SelectTokenType.From)
}, onInputChanged = {
inputText = it
Expand Down Expand Up @@ -352,6 +379,8 @@ fun LimitOrderContent(
modifier = Modifier.onFocusChanged {
if (it.isFocused) {
focusedField = FocusedField.OUT_AMOUNT
} else if (focusedField == FocusedField.OUT_AMOUNT) {
focusedField = FocusedField.NONE
}
},
token = toToken,
Expand All @@ -361,6 +390,7 @@ fun LimitOrderContent(
selectClick = {
keyboardController?.hide()
focusManager.clearFocus()
focusedField = FocusedField.NONE
onSelectToken(isReverse, if (isReverse) SelectTokenType.From else SelectTokenType.To)
},
onInputChanged = {
Expand Down Expand Up @@ -398,7 +428,11 @@ fun LimitOrderContent(
Column {
PriceInputArea(
modifier = Modifier.onFocusChanged {
if (it.isFocused) focusedField = FocusedField.PRICE
if (it.isFocused) {
focusedField = FocusedField.PRICE
} else if (focusedField == FocusedField.PRICE) {
focusedField = FocusedField.NONE
}
},
fromToken = fromToken,
toToken = toToken,
Expand All @@ -425,7 +459,7 @@ fun LimitOrderContent(
)
}

if (availableHeight != null || inputText.isNotBlank()) {
if (!showOpenOrdersCard) {
Column(modifier = Modifier
.wrapContentHeight()
.padding(horizontal = 20.dp)
Expand All @@ -447,11 +481,13 @@ fun LimitOrderContent(
onClick = {
keyboardController?.hide()
focusManager.clearFocus()
focusedField = FocusedField.NONE
if (isButtonEnabled && !isBusy && toToken != null) {
isButtonEnabled = false
isSubmitting = true
keyboardController?.hide()
focusManager.clearFocus()
focusedField = FocusedField.NONE
scope.launch {
runCatching {
val fromTokenValue = requireNotNull(fromToken)
Expand Down Expand Up @@ -652,6 +688,7 @@ fun LimitOrderContent(
onDone = {
keyboardController?.hide()
focusManager.clearFocus()
focusedField = FocusedField.NONE
},
onMarketPriceClick = {
marketPriceClickTime = System.currentTimeMillis()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,12 @@ fun SwapContent(
var inputText by remember {
mutableStateOf(limitTradeInputDecimalPlaces(initialAmount ?: "", fromMaxDecimalPlaces))
}
LaunchedEffect(lastOrderTime, fromMaxDecimalPlaces) {
LaunchedEffect(lastOrderTime) {
inputText = limitTradeInputDecimalPlaces(initialAmount ?: "", fromMaxDecimalPlaces)
}
LaunchedEffect(fromMaxDecimalPlaces) {
inputText = swapInputTextForMaxDecimalPlacesChange(inputText, fromMaxDecimalPlaces)
}

val shouldRefreshQuote = remember { MutableStateFlow(inputText) }
var isButtonEnabled by remember { mutableStateOf(true) }
Expand Down Expand Up @@ -231,6 +234,11 @@ fun SwapContent(
isSendFocused = isSendFocused,
isKeyboardVisible = availableHeight != null,
)
LaunchedEffect(availableHeight, inputText) {
if (availableHeight == null && inputText.isBlank()) {
isSendFocused = false
}
}
Column(
modifier = Modifier
.fillMaxWidth()
Expand Down Expand Up @@ -427,6 +435,11 @@ internal fun shouldShowSwapRecommendedMarketCards(
isKeyboardVisible: Boolean,
): Boolean = inMixin && hasRecommendedCards && inputText.isBlank() && !isSendFocused && !isKeyboardVisible

internal fun swapInputTextForMaxDecimalPlacesChange(
currentInput: String,
maxDecimalPlaces: Int?,
): String = limitTradeInputDecimalPlaces(currentInput, maxDecimalPlaces)

@Composable
fun ReviewButton(
inputText: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,9 @@ import one.mixin.android.tip.wc.internal.TipGas
import one.mixin.android.tip.wc.internal.buildTipGas
import one.mixin.android.ui.common.BaseFragment
import one.mixin.android.ui.home.web3.Web3ViewModel
import one.mixin.android.ui.home.web3.trade.perps.AllPositionsFragment
import one.mixin.android.ui.home.web3.trade.perps.PerpetualGuideBottomSheetDialogFragment
import one.mixin.android.ui.home.web3.trade.perps.PerpsActivity
import one.mixin.android.ui.home.web3.trade.perps.PerpsMarketListBottomSheetDialogFragment
import one.mixin.android.ui.home.web3.trade.perps.PositionDetailFragment
import one.mixin.android.ui.home.web3.trade.perps.PerpsRouteNavigator
import one.mixin.android.ui.wallet.AllOrdersFragment
import one.mixin.android.ui.wallet.DepositFragment
import one.mixin.android.ui.wallet.LimitTransferBottomSheetDialogFragment
Expand Down Expand Up @@ -578,37 +576,39 @@ class TradeFragment : BaseFragment() {
PerpsMarketListBottomSheetDialogFragment.newInstance(initialCategory, initialSort).show(parentFragmentManager, PerpsMarketListBottomSheetDialogFragment.TAG)
},
onShowAllOpenPositions = {
navTo(AllPositionsFragment.newOpenInstance(), AllPositionsFragment.TAG)
PerpsRouteNavigator.showPositionList(
fragmentManager = parentFragmentManager,
showOpenPositions = true,
)
},
onShowAllClosedPositions = {
navTo(AllPositionsFragment.newClosedInstance(), AllPositionsFragment.TAG)
PerpsRouteNavigator.showPositionList(
fragmentManager = parentFragmentManager,
showOpenPositions = false,
)
},
onOpenPositionClick = { position ->
navTo(
PositionDetailFragment.newInstance(
position,
AnalyticsTracker.PerpsSource.PERPS_HOME_LIST,
),
PositionDetailFragment.TAG,
PerpsRouteNavigator.showPositionDetail(
fragmentManager = parentFragmentManager,
position = position,
source = AnalyticsTracker.PerpsSource.PERPS_HOME_LIST,
)
},
onMarketItemClick = { market ->
PerpsActivity.showDetail(
requireContext(),
market.marketId,
market.displaySymbol,
market.displaySymbol,
market.tokenSymbol,
AnalyticsTracker.PerpsSource.MORE_EXPLORE,
PerpsRouteNavigator.showMarketDetail(
fragmentManager = parentFragmentManager,
marketId = market.marketId,
marketSymbol = market.displaySymbol,
displaySymbol = market.displaySymbol,
tokenSymbol = market.tokenSymbol,
source = AnalyticsTracker.PerpsSource.MORE_EXPLORE,
)
},
onClosedPositionClick = { position ->
navTo(
PositionDetailFragment.newInstance(
position,
AnalyticsTracker.PerpsSource.PERPS_HOME_LIST,
),
PositionDetailFragment.TAG,
PerpsRouteNavigator.showPositionDetail(
fragmentManager = parentFragmentManager,
order = position,
source = AnalyticsTracker.PerpsSource.PERPS_HOME_LIST,
)
}
)
Expand Down
Loading