Skip to content

Commit

Permalink
Merge pull request #11587 from woocommerce/11583-tapping-order-comple…
Browse files Browse the repository at this point in the history
…te-button-equivalent-tapping-mark-as-paid

[Payment Method Improvements] Tapping Order Complete button should be equivalent tapping Mark as Paid
  • Loading branch information
kidinov committed May 28, 2024
2 parents 2913a8c + d5747fb commit 0ca694b
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.woocommerce.android.ui.base.BaseFragment
import com.woocommerce.android.ui.main.AppBarStatus
import com.woocommerce.android.ui.payments.cardreader.onboarding.CardReaderFlowParam
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
Expand All @@ -30,14 +31,29 @@ class ChangeDueCalculatorFragment : BaseFragment() {
}
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return ComposeView(requireContext()).apply {
setContent {
val uiState by viewModel.uiState.collectAsState()
ChangeDueCalculatorScreen(
uiState = uiState,
onNavigateUp = { viewModel.onBackPressed() },
onCompleteOrderClick = {}
onCompleteOrderClick = {
val action = ChangeDueCalculatorFragmentDirections
.actionChangeDueCalculatorFragmentToSelectPaymentMethodFragment(
cardReaderFlowParam = CardReaderFlowParam.PaymentOrRefund.Payment(
viewModel.navArgs.orderId,
CardReaderFlowParam.PaymentOrRefund.Payment.PaymentType.ORDER
),
isOrderPaid = true
)
findNavController().navigate(action)
},
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) {
val currentState = _uiState.value
if (currentState is UiState.Success) {
val newChange = amount - currentState.amountDue
_uiState.value = currentState.copy(amountReceived = amount, change = newChange)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ class SelectPaymentMethodViewModel @Inject constructor(

init {
checkStatus()
if (FeatureFlag.OTHER_PAYMENT_METHODS.isEnabled()) {
handleIsOrderPaidNavigationArgs()
}
}

private fun checkStatus() {
Expand Down Expand Up @@ -120,6 +123,12 @@ class SelectPaymentMethodViewModel @Inject constructor(
}
}

private fun handleIsOrderPaidNavigationArgs() {
if (navArgs.isOrderPaid) {
onCashPaymentConfirmed()
}
}

private suspend fun showPaymentState() {
val isPaymentCollectableWithCardReader = cardPaymentCollectibilityChecker.isCollectable(order.first())
val isPaymentCollectableWithTapToPay = isTapToPayAvailable()
Expand Down
14 changes: 14 additions & 0 deletions WooCommerce/src/main/res/navigation/nav_graph_payment_flow.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
android:name="cardReaderFlowParam"
app:argType="com.woocommerce.android.ui.payments.cardreader.onboarding.CardReaderFlowParam"
app:nullable="false" />
<argument
android:name="isOrderPaid"
app:argType="boolean"
android:defaultValue="false" />
<action
android:id="@+id/action_selectPaymentMethodFragment_to_cardReaderPaymentFlow"
app:destination="@id/cardReaderStatusCheckerDialogFragment" />
Expand Down Expand Up @@ -70,6 +74,16 @@
<argument
android:name="orderId"
app:argType="long" />

<action
android:id="@+id/action_changeDueCalculatorFragment_to_selectPaymentMethodFragment"
app:destination="@id/selectPaymentMethodFragment"
app:popUpTo="@id/selectPaymentMethodFragment"
app:popUpToInclusive="true">
<argument
android:name="isOrderPaid"
app:argType="boolean" />
</action>
</fragment>


Expand Down

0 comments on commit 0ca694b

Please sign in to comment.