Skip to content

Commit 85cf2aa

Browse files
authored
Merge pull request #167 from code-payments/chore/currency-ux
chore: improve ux in currency selection sheet
2 parents c32f7ac + f70f831 commit 85cf2aa

3 files changed

Lines changed: 47 additions & 21 deletions

File tree

app/src/main/java/com/getcode/util/Currency.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,13 @@ fun formatAmountString(resources: ResourceHelper, currency: Currency, amount: Do
105105
resources.getString(R.string.core_kin)
106106
}"
107107
} else {
108-
"${currency.symbol}${FormatUtils.format(amount)} ${
109-
resources.getString(R.string.core_ofKin)
110-
}"
108+
when {
109+
currency.code != currency.symbol -> {
110+
"$${FormatUtils.format(amount)} ${resources.getString(R.string.core_ofKin)}"
111+
}
112+
else -> {
113+
"${currency.symbol}${FormatUtils.format(amount)} ${resources.getString(R.string.core_ofKin)}"
114+
}
115+
}
111116
}
112117
}

app/src/main/java/com/getcode/view/main/currency/CurrencySelectionSheet.kt

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.getcode.view.main.currency
22

3+
import androidx.compose.animation.core.animateDpAsState
34
import androidx.compose.foundation.Image
45
import androidx.compose.foundation.background
56
import androidx.compose.foundation.layout.Box
@@ -43,7 +44,10 @@ import androidx.compose.ui.platform.LocalSoftwareKeyboardController
4344
import androidx.compose.ui.res.painterResource
4445
import androidx.compose.ui.res.stringResource
4546
import androidx.compose.ui.text.input.TextFieldValue
47+
import androidx.compose.ui.unit.Dp
48+
import androidx.compose.ui.unit.DpSize
4649
import androidx.compose.ui.unit.dp
50+
import androidx.compose.ui.unit.isUnspecified
4751
import androidx.compose.ui.unit.sp
4852
import androidx.lifecycle.Lifecycle
4953
import com.getcode.R
@@ -58,6 +62,9 @@ import com.getcode.ui.utils.RepeatOnLifecycle
5862
import com.getcode.ui.utils.rememberedClickable
5963
import com.getcode.ui.components.CodeCircularProgressIndicator
6064
import com.getcode.ui.components.SwipeableView
65+
import com.getcode.ui.utils.addIf
66+
import com.getcode.ui.utils.keyboardAsState
67+
import com.getcode.ui.utils.measured
6168
import com.getcode.view.main.giveKin.CurrencyListItem
6269
import kotlinx.coroutines.delay
6370
import kotlinx.coroutines.flow.distinctUntilChanged
@@ -87,20 +94,7 @@ fun CurrencySelectionSheet(
8794
}
8895
}
8996

90-
RepeatOnLifecycle(targetState = Lifecycle.State.RESUMED) {
91-
viewModel.eventFlow
92-
.filterIsInstance<CurrencyViewModel.Event.OnSelectedCurrencyChanged>()
93-
.filter { it.fromUser }
94-
.map { it.currency }
95-
.distinctUntilChanged()
96-
.onEach {
97-
composeScope.launch {
98-
keyboardController?.hide()
99-
delay(500)
100-
navigator.popWithResult(it)
101-
}
102-
}.launchIn(this)
103-
}
97+
val keyboard by keyboardAsState()
10498

10599
Column(
106100
modifier = Modifier.imePadding()
@@ -205,9 +199,25 @@ fun CurrencySelectionSheet(
205199
}
206200

207201
is CurrencyListItem.RegionCurrencyItem -> {
202+
var isSwipedAway by remember(listItem) {
203+
mutableStateOf(false)
204+
}
205+
206+
var height by remember {
207+
mutableStateOf(0.dp)
208+
}
209+
210+
val animatedHeight by animateDpAsState(targetValue = if (!isSwipedAway) height else 0.dp)
211+
208212
SwipeableView(
213+
modifier = Modifier.measured {
214+
if (height == 0.dp) {
215+
height = it.height
216+
}
217+
}.addIf(height != 0.dp) { Modifier.height(animatedHeight) },
209218
isSwipeEnabled = listItem.isRecent,
210219
leftSwiped = {
220+
isSwipedAway = true
211221
viewModel.dispatchEvent(
212222
CurrencyViewModel.Event.OnRecentCurrencyRemoved(
213223
listItem.currency
@@ -224,6 +234,13 @@ fun CurrencySelectionSheet(
224234
state.selectedCurrencyCode.orEmpty() == currencyCode,
225235
isDisabled,
226236
) {
237+
composeScope.launch {
238+
if (keyboard) {
239+
keyboardController?.hide()
240+
delay(500)
241+
}
242+
navigator.popWithResult(listItem.currency)
243+
}
227244
viewModel.dispatchEvent(
228245
CurrencyViewModel.Event.OnSelectedCurrencyChanged(
229246
listItem.currency

app/src/main/java/com/getcode/view/main/currency/CurrencyViewModel.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,14 @@ class CurrencyViewModel @Inject constructor(
283283

284284
is Event.OnRecentCurrencyRemoved -> { state -> state }
285285
is Event.OnSelectedCurrencyChanged -> { state ->
286-
state.copy(
287-
selectedCurrencyCode = event.currency.code,
288-
selectedCurrencyResId = event.currency.resId
289-
)
286+
if (event.fromUser) {
287+
state
288+
} else {
289+
state.copy(
290+
selectedCurrencyCode = event.currency.code,
291+
selectedCurrencyResId = event.currency.resId
292+
)
293+
}
290294
}
291295

292296
is Event.RemovedLocalFromRecents -> { state -> state.copy(wasLocalRemovedFromRecents = true) }

0 commit comments

Comments
 (0)