Skip to content

[Payment Method Improvements] Update change due based on user input #11586

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

Merged
merged 7 commits into from
May 28, 2024
Merged
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 @@ -37,7 +37,8 @@ class ChangeDueCalculatorFragment : BaseFragment() {
ChangeDueCalculatorScreen(
uiState = uiState,
onNavigateUp = { viewModel.onBackPressed() },
onCompleteOrderClick = {}
onCompleteOrderClick = {},
onAmountReceivedChanged = { viewModel.updateAmountReceived(it) }
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
Expand All @@ -39,6 +40,7 @@ import androidx.compose.ui.unit.TextUnitType
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import com.woocommerce.android.R
import com.woocommerce.android.extensions.filterNotNull
import com.woocommerce.android.ui.compose.theme.WooThemeWithBackground
import com.woocommerce.android.widgets.WCMaterialOutlinedCurrencyEditTextView
import java.math.BigDecimal
Expand All @@ -47,9 +49,11 @@ import java.math.BigDecimal
fun ChangeDueCalculatorScreen(
uiState: ChangeDueCalculatorViewModel.UiState,
onNavigateUp: () -> Unit,
onCompleteOrderClick: () -> Unit
onCompleteOrderClick: () -> Unit,
onAmountReceivedChanged: (BigDecimal) -> Unit
) {
val context = LocalContext.current
val lifecycleOwner = LocalLifecycleOwner.current

WooThemeWithBackground {
Scaffold(
Expand Down Expand Up @@ -109,6 +113,9 @@ fun ChangeDueCalculatorScreen(
supportsNegativeValues = false
hint = hintString
setValueIfDifferent(uiState.amountDue)
value.filterNotNull().observe(lifecycleOwner) {
onAmountReceivedChanged(it)
}
view = this
}
},
Expand All @@ -130,7 +137,7 @@ fun ChangeDueCalculatorScreen(
)
Spacer(modifier = Modifier.height(8.dp))
Text(
text = "$0.00",
text = if (uiState.change < BigDecimal.ZERO) "-" else uiState.change.toPlainString(),
style = LocalTextStyle.current.copy(
fontWeight = FontWeight.Bold,
fontSize = TextUnit(44f, TextUnitType.Sp)
Expand Down Expand Up @@ -217,9 +224,11 @@ fun ChangeDueCalculatorScreenSuccessPreview() {
ChangeDueCalculatorScreen(
uiState = ChangeDueCalculatorViewModel.UiState.Success(
amountDue = BigDecimal("666.00"),
change = BigDecimal("0.00")
change = BigDecimal("0.00"),
amountReceived = BigDecimal("0.00")
),
onNavigateUp = {},
onCompleteOrderClick = {}
onCompleteOrderClick = {},
onAmountReceivedChanged = {}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ class ChangeDueCalculatorViewModel @Inject constructor(

sealed class UiState {
data object Loading : UiState()
data class Success(val amountDue: BigDecimal, val change: BigDecimal) : UiState()
data class Success(
val amountDue: BigDecimal,
val change: BigDecimal,
val amountReceived: BigDecimal
) : UiState()

data object Error : UiState()
}

Expand All @@ -42,7 +47,8 @@ class ChangeDueCalculatorViewModel @Inject constructor(
launch {
val order = orderDetailRepository.getOrderById(orderId)
order?.let {
_uiState.value = UiState.Success(amountDue = order.total, change = BigDecimal.ZERO)
_uiState.value =
UiState.Success(amountDue = order.total, change = BigDecimal.ZERO, amountReceived = BigDecimal.ZERO)
} ?: run {
_uiState.value = UiState.Error
}
Expand All @@ -52,4 +58,12 @@ class ChangeDueCalculatorViewModel @Inject constructor(
fun onBackPressed() {
_navigationEvent.value = Unit
}

fun updateAmountReceived(amount: BigDecimal) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be nice to add a test on this logic

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For sure, thanks for the suggestion. I will add it in this draft PR:
#11594

val currentState = _uiState.value
if (currentState is UiState.Success) {
val newChange = amount - currentState.amountDue
_uiState.value = currentState.copy(amountReceived = amount, change = newChange)
}
}
}
Loading