From 87e50b954edc535e71940b93ef058f65ddb7fb48 Mon Sep 17 00:00:00 2001 From: Andrey Date: Mon, 29 Jan 2024 14:55:03 +0100 Subject: [PATCH 1/9] Model to include loading status --- .../ui/orders/details/OrderDetailFragment.kt | 2 +- .../ui/orders/details/OrderDetailViewModel.kt | 25 +++++++++---------- .../ui/orders/details/OrderDetailViewState.kt | 6 ++++- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/OrderDetailFragment.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/OrderDetailFragment.kt index f652d0c4dd5..148976a6bd6 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/OrderDetailFragment.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/OrderDetailFragment.kt @@ -254,7 +254,7 @@ class OrderDetailFragment : private fun setupObservers(viewModel: OrderDetailViewModel) { viewModel.viewStateData.observe(viewLifecycleOwner) { old, new -> new.orderInfo?.takeIfNotEqualTo(old?.orderInfo) { - showOrderDetail(it.order!!, it.isPaymentCollectableWithCardReader, it.isReceiptButtonsVisible) + showOrderDetail(it.order!!, it.isPaymentCollectableWithCardReader, it.receiptButtonStatus) requireActivity().invalidateOptionsMenu() } new.orderStatus?.takeIfNotEqualTo(old?.orderStatus) { showOrderStatus(it) } diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/OrderDetailViewModel.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/OrderDetailViewModel.kt index b018d182f78..9ff82749b1a 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/OrderDetailViewModel.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/OrderDetailViewModel.kt @@ -49,7 +49,6 @@ import com.woocommerce.android.ui.orders.OrderStatusUpdateSource import com.woocommerce.android.ui.orders.details.customfields.CustomOrderFieldsHelper import com.woocommerce.android.ui.payments.cardreader.onboarding.CardReaderFlowParam import com.woocommerce.android.ui.payments.cardreader.payment.CardReaderPaymentCollectibilityChecker -import com.woocommerce.android.ui.payments.receipt.PaymentReceiptHelper import com.woocommerce.android.ui.payments.tracking.PaymentsFlowTracker import com.woocommerce.android.ui.products.ProductDetailRepository import com.woocommerce.android.ui.products.addons.AddonRepository @@ -92,8 +91,7 @@ class OrderDetailViewModel @Inject constructor( private val getOrderSubscriptions: GetOrderSubscriptions, private val giftCardRepository: GiftCardRepository, private val orderProductMapper: OrderProductMapper, - private val productDetailRepository: ProductDetailRepository, - private val paymentReceiptHelper: PaymentReceiptHelper, + private val productDetailRepository: ProductDetailRepository ) : ScopedViewModel(savedState), OnProductFetchedListener { private val navArgs: OrderDetailFragmentArgs by savedState.navArgs() @@ -345,15 +343,10 @@ class OrderDetailViewModel @Inject constructor( } fun onSeeReceiptClicked() { - launch { - tracker.trackReceiptViewTapped(order.id, order.status) - val receiptResult = paymentReceiptHelper.getReceiptUrl(order.id) - if (receiptResult.isSuccess) { - triggerEvent(PreviewReceipt(order.billingAddress.email, receiptResult.getOrThrow(), order.id)) - } else { - triggerEvent(ShowSnackbar(string.receipt_fetching_error)) - } - } + tracker.trackReceiptViewTapped(order.id, order.status) + loadReceiptUrl()?.let { + triggerEvent(PreviewReceipt(order.billingAddress.email, it, order.id)) + } ?: WooLog.e(T.ORDERS, "ReceiptUrl is null, but SeeReceipt button is visible") } fun onPrintingInstructionsClicked() { @@ -379,6 +372,12 @@ class OrderDetailViewModel @Inject constructor( triggerEvent(ShowSnackbar(message)) } + private fun loadReceiptUrl(): String? { + return selectedSite.getIfExists()?.let { + appPrefs.getReceiptUrl(it.id, it.siteId, it.selfHostedSiteId, order.id) + } + } + fun onViewRefundedProductsClicked() { triggerEvent(ViewRefundedProducts(orderId = order.id)) } @@ -581,7 +580,7 @@ class OrderDetailViewModel @Inject constructor( orderInfo = OrderDetailViewState.OrderInfo( order = order, isPaymentCollectableWithCardReader = isPaymentCollectable, - isReceiptButtonsVisible = paymentReceiptHelper.isReceiptAvailable(order.id) && order.isOrderPaid, + receiptButtonStatus = !loadReceiptUrl().isNullOrEmpty() ), orderStatus = orderStatus, toolbarTitle = resourceProvider.getString( diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/OrderDetailViewState.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/OrderDetailViewState.kt index 77a0558726a..78d2d9736fb 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/OrderDetailViewState.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/OrderDetailViewState.kt @@ -33,6 +33,10 @@ data class OrderDetailViewState( data class OrderInfo( val order: Order? = null, val isPaymentCollectableWithCardReader: Boolean = false, - val isReceiptButtonsVisible: Boolean = false + val receiptButtonStatus: ReceiptButtonStatus = ReceiptButtonStatus.Hidden, ) : Parcelable + + enum class ReceiptButtonStatus { + Loading, Hidden, Visible + } } From 1f85a93faee1dc51ed6f1f6adcf155eb7937857b Mon Sep 17 00:00:00 2001 From: Andrey Date: Mon, 29 Jan 2024 14:57:30 +0100 Subject: [PATCH 2/9] Pass the status to the view --- .../ui/orders/details/OrderDetailFragment.kt | 4 +-- .../views/OrderDetailPaymentInfoView.kt | 25 +++++++++++-------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/OrderDetailFragment.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/OrderDetailFragment.kt index 148976a6bd6..9590be95f3b 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/OrderDetailFragment.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/OrderDetailFragment.kt @@ -437,7 +437,7 @@ class OrderDetailFragment : private fun showOrderDetail( order: Order, isPaymentCollectableWithCardReader: Boolean, - isReceiptButtonsVisible: Boolean + receiptButtonStatus: OrderDetailViewState.ReceiptButtonStatus ) { binding.orderDetailOrderStatus.updateOrder(order) binding.orderDetailShippingMethodNotice.isVisible = order.hasMultipleShippingLines @@ -449,7 +449,7 @@ class OrderDetailFragment : binding.orderDetailPaymentInfo.updatePaymentInfo( order = order, isPaymentCollectableWithCardReader = isPaymentCollectableWithCardReader, - isReceiptAvailable = isReceiptButtonsVisible, + receiptButtonStatus = receiptButtonStatus, formatCurrencyForDisplay = currencyFormatter.buildBigDecimalFormatter(order.currency), onIssueRefundClickListener = { viewModel.onIssueOrderRefundClicked() }, onSeeReceiptClickListener = { diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/views/OrderDetailPaymentInfoView.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/views/OrderDetailPaymentInfoView.kt index bc5384b9fc2..4f67cee39b9 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/views/OrderDetailPaymentInfoView.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/views/OrderDetailPaymentInfoView.kt @@ -17,6 +17,7 @@ import com.woocommerce.android.extensions.show import com.woocommerce.android.model.GiftCardSummary import com.woocommerce.android.model.Order import com.woocommerce.android.model.Refund +import com.woocommerce.android.ui.orders.details.OrderDetailViewState import com.woocommerce.android.ui.orders.details.adapter.OrderDetailRefundsAdapter import com.woocommerce.android.ui.orders.details.adapter.OrderDetailRefundsLineBuilder import dagger.hilt.android.AndroidEntryPoint @@ -36,7 +37,7 @@ class OrderDetailPaymentInfoView @JvmOverloads constructor( @Suppress("LongParameterList") fun updatePaymentInfo( order: Order, - isReceiptAvailable: Boolean, + receiptButtonStatus: OrderDetailViewState.ReceiptButtonStatus, isPaymentCollectableWithCardReader: Boolean, formatCurrencyForDisplay: (BigDecimal) -> String, onSeeReceiptClickListener: (view: View) -> Unit, @@ -88,7 +89,7 @@ class OrderDetailPaymentInfoView @JvmOverloads constructor( updateFeesSection(order, formatCurrencyForDisplay) updateRefundSection(order, formatCurrencyForDisplay, onIssueRefundClickListener) updateCollectPaymentSection(order, onCollectPaymentClickListener) - updateSeeReceiptSection(isReceiptAvailable, onSeeReceiptClickListener) + updateSeeReceiptSection(receiptButtonStatus, onSeeReceiptClickListener) updatePrintingInstructionSection(isPaymentCollectableWithCardReader, onPrintingInstructionsClickListener) } @@ -192,16 +193,20 @@ class OrderDetailPaymentInfoView @JvmOverloads constructor( } private fun updateSeeReceiptSection( - isReceiptAvailable: Boolean, + receiptButtonStatus: OrderDetailViewState.ReceiptButtonStatus, onSeeReceiptClickListener: (view: View) -> Unit ) { - if (isReceiptAvailable) { - binding.paymentInfoSeeReceiptButton.visibility = VISIBLE - binding.paymentInfoSeeReceiptButton.setOnClickListener( - onSeeReceiptClickListener - ) - } else { - binding.paymentInfoSeeReceiptButton.visibility = GONE + when (receiptButtonStatus) { + OrderDetailViewState.ReceiptButtonStatus.Loading -> { + + } + OrderDetailViewState.ReceiptButtonStatus.Hidden -> binding.paymentInfoSeeReceiptButton.visibility = GONE + OrderDetailViewState.ReceiptButtonStatus.Visible -> { + binding.paymentInfoSeeReceiptButton.visibility = VISIBLE + binding.paymentInfoSeeReceiptButton.setOnClickListener( + onSeeReceiptClickListener + ) + } } } From 493ca09fe96972c4305fb080ed7d192b8e6718ae Mon Sep 17 00:00:00 2001 From: Andrey Date: Mon, 29 Jan 2024 15:11:12 +0100 Subject: [PATCH 3/9] Visible/Hidden status determination --- .../ui/orders/details/OrderDetailViewModel.kt | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/OrderDetailViewModel.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/OrderDetailViewModel.kt index 9ff82749b1a..d112c6bb020 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/OrderDetailViewModel.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/OrderDetailViewModel.kt @@ -49,6 +49,7 @@ import com.woocommerce.android.ui.orders.OrderStatusUpdateSource import com.woocommerce.android.ui.orders.details.customfields.CustomOrderFieldsHelper import com.woocommerce.android.ui.payments.cardreader.onboarding.CardReaderFlowParam import com.woocommerce.android.ui.payments.cardreader.payment.CardReaderPaymentCollectibilityChecker +import com.woocommerce.android.ui.payments.receipt.PaymentReceiptHelper import com.woocommerce.android.ui.payments.tracking.PaymentsFlowTracker import com.woocommerce.android.ui.products.ProductDetailRepository import com.woocommerce.android.ui.products.addons.AddonRepository @@ -91,7 +92,8 @@ class OrderDetailViewModel @Inject constructor( private val getOrderSubscriptions: GetOrderSubscriptions, private val giftCardRepository: GiftCardRepository, private val orderProductMapper: OrderProductMapper, - private val productDetailRepository: ProductDetailRepository + private val productDetailRepository: ProductDetailRepository, + private val paymentReceiptHelper: PaymentReceiptHelper, ) : ScopedViewModel(savedState), OnProductFetchedListener { private val navArgs: OrderDetailFragmentArgs by savedState.navArgs() @@ -343,10 +345,15 @@ class OrderDetailViewModel @Inject constructor( } fun onSeeReceiptClicked() { - tracker.trackReceiptViewTapped(order.id, order.status) - loadReceiptUrl()?.let { - triggerEvent(PreviewReceipt(order.billingAddress.email, it, order.id)) - } ?: WooLog.e(T.ORDERS, "ReceiptUrl is null, but SeeReceipt button is visible") + launch { + tracker.trackReceiptViewTapped(order.id, order.status) + val receiptResult = paymentReceiptHelper.getReceiptUrl(order.id) + if (receiptResult.isSuccess) { + triggerEvent(PreviewReceipt(order.billingAddress.email, receiptResult.getOrThrow(), order.id)) + } else { + triggerEvent(ShowSnackbar(string.receipt_fetching_error)) + } + } } fun onPrintingInstructionsClicked() { @@ -372,12 +379,6 @@ class OrderDetailViewModel @Inject constructor( triggerEvent(ShowSnackbar(message)) } - private fun loadReceiptUrl(): String? { - return selectedSite.getIfExists()?.let { - appPrefs.getReceiptUrl(it.id, it.siteId, it.selfHostedSiteId, order.id) - } - } - fun onViewRefundedProductsClicked() { triggerEvent(ViewRefundedProducts(orderId = order.id)) } @@ -580,7 +581,11 @@ class OrderDetailViewModel @Inject constructor( orderInfo = OrderDetailViewState.OrderInfo( order = order, isPaymentCollectableWithCardReader = isPaymentCollectable, - receiptButtonStatus = !loadReceiptUrl().isNullOrEmpty() + receiptButtonStatus = if (paymentReceiptHelper.isReceiptAvailable(order.id) && order.isOrderPaid) { + OrderDetailViewState.ReceiptButtonStatus.Visible + } else { + OrderDetailViewState.ReceiptButtonStatus.Hidden + } ), orderStatus = orderStatus, toolbarTitle = resourceProvider.getString( From 899ec5b2027c8ba1ae8fe984ccc9ff0da6cb5f25 Mon Sep 17 00:00:00 2001 From: Andrey Date: Mon, 29 Jan 2024 15:30:57 +0100 Subject: [PATCH 4/9] Pass loading status to the view --- .../ui/orders/details/OrderDetailViewModel.kt | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/OrderDetailViewModel.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/OrderDetailViewModel.kt index d112c6bb020..1b80d6660cb 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/OrderDetailViewModel.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/OrderDetailViewModel.kt @@ -347,7 +347,21 @@ class OrderDetailViewModel @Inject constructor( fun onSeeReceiptClicked() { launch { tracker.trackReceiptViewTapped(order.id, order.status) + + viewState = viewState.copy( + orderInfo = viewState.orderInfo?.copy( + receiptButtonStatus = OrderDetailViewState.ReceiptButtonStatus.Loading + ) + ) + val receiptResult = paymentReceiptHelper.getReceiptUrl(order.id) + + viewState = viewState.copy( + orderInfo = viewState.orderInfo?.copy( + receiptButtonStatus = OrderDetailViewState.ReceiptButtonStatus.Visible + ) + ) + if (receiptResult.isSuccess) { triggerEvent(PreviewReceipt(order.billingAddress.email, receiptResult.getOrThrow(), order.id)) } else { From 0f997c3102696e778d42f2f2f588530d5593034b Mon Sep 17 00:00:00 2001 From: Andrey Date: Mon, 29 Jan 2024 15:44:44 +0100 Subject: [PATCH 5/9] Constraint layout usage to show loading indicator --- .../res/layout/order_detail_payment_info.xml | 120 +++++++++--------- 1 file changed, 62 insertions(+), 58 deletions(-) diff --git a/WooCommerce/src/main/res/layout/order_detail_payment_info.xml b/WooCommerce/src/main/res/layout/order_detail_payment_info.xml index 71df778356f..c3b8728e0e1 100644 --- a/WooCommerce/src/main/res/layout/order_detail_payment_info.xml +++ b/WooCommerce/src/main/res/layout/order_detail_payment_info.xml @@ -1,7 +1,7 @@ - @@ -370,65 +370,69 @@ tools:text="$34.00"/> - - - - - - - - - + - - - + android:layout_height="wrap_content"> + + + + + + + + + + + + + + - - + From e5e2ab625cd90a5dcf36353c44388a914a80fe58 Mon Sep 17 00:00:00 2001 From: Andrey Date: Mon, 29 Jan 2024 15:56:48 +0100 Subject: [PATCH 6/9] Loading indicator to the receipt button --- .../views/OrderDetailPaymentInfoView.kt | 10 ++++- .../res/layout/order_detail_payment_info.xml | 42 ++++++++++++------- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/views/OrderDetailPaymentInfoView.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/views/OrderDetailPaymentInfoView.kt index 4f67cee39b9..1a2bf575cb5 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/views/OrderDetailPaymentInfoView.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/views/OrderDetailPaymentInfoView.kt @@ -198,10 +198,16 @@ class OrderDetailPaymentInfoView @JvmOverloads constructor( ) { when (receiptButtonStatus) { OrderDetailViewState.ReceiptButtonStatus.Loading -> { - + binding.paymentInfoSeeReceiptButton.visibility = VISIBLE + binding.paymentInfoSeeReceiptButton.isEnabled = false + binding.paymentInfoSeeReceiptButtonProgressBar.visibility = VISIBLE + } + OrderDetailViewState.ReceiptButtonStatus.Hidden -> { + binding.paymentInfoSeeReceiptButton.visibility = GONE + binding.paymentInfoSeeReceiptButtonProgressBar.visibility = GONE } - OrderDetailViewState.ReceiptButtonStatus.Hidden -> binding.paymentInfoSeeReceiptButton.visibility = GONE OrderDetailViewState.ReceiptButtonStatus.Visible -> { + binding.paymentInfoSeeReceiptButtonProgressBar.visibility = GONE binding.paymentInfoSeeReceiptButton.visibility = VISIBLE binding.paymentInfoSeeReceiptButton.setOnClickListener( onSeeReceiptClickListener diff --git a/WooCommerce/src/main/res/layout/order_detail_payment_info.xml b/WooCommerce/src/main/res/layout/order_detail_payment_info.xml index c3b8728e0e1..1152c5feec6 100644 --- a/WooCommerce/src/main/res/layout/order_detail_payment_info.xml +++ b/WooCommerce/src/main/res/layout/order_detail_payment_info.xml @@ -382,15 +382,24 @@ style="@style/Woo.Button.TextButton" android:layout_width="0dp" android:layout_height="wrap_content" + android:layout_marginTop="@dimen/minor_100" + android:gravity="start|center_vertical" android:paddingStart="@dimen/major_100" android:paddingEnd="@dimen/major_100" - android:gravity="start" android:text="@string/orderdetail_see_receipt_button" android:textAllCaps="false" + app:layout_constraintEnd_toStartOf="@id/paymentInfo_seeReceiptButton_progressBar" app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent" /> + + + app:layout_constraintTop_toTopOf="@id/paymentInfo_seeReceiptButton" /> + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@id/paymentInfo_seeReceiptButton" /> + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@id/paymentInfo_collectCardPresentPaymentButton" /> - + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@id/paymentInfo_issueRefundButton" /> From 2b6bc61337e72c48634b18ba23a9455bdf4d0353 Mon Sep 17 00:00:00 2001 From: Andrey Date: Mon, 29 Jan 2024 16:15:43 +0100 Subject: [PATCH 7/9] Fixed blinking of the receipt details button issue --- .../android/ui/orders/details/OrderDetailViewModel.kt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/OrderDetailViewModel.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/OrderDetailViewModel.kt index 1b80d6660cb..8902a5da997 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/OrderDetailViewModel.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/orders/details/OrderDetailViewModel.kt @@ -103,9 +103,13 @@ class OrderDetailViewModel @Inject constructor( get() = requireNotNull(viewState.orderInfo?.order) set(value) { viewState = viewState.copy( - orderInfo = OrderDetailViewState.OrderInfo( + orderInfo = viewState.orderInfo?.copy( + order = value, + isPaymentCollectableWithCardReader = viewState.orderInfo?.isPaymentCollectableWithCardReader + ?: false + ) ?: OrderDetailViewState.OrderInfo( value, - viewState.orderInfo?.isPaymentCollectableWithCardReader ?: false + isPaymentCollectableWithCardReader = false ) ) } From c537a9d0004cbc8a7ada1910f6202a274a3df056 Mon Sep 17 00:00:00 2001 From: Andrey Date: Mon, 29 Jan 2024 16:15:59 +0100 Subject: [PATCH 8/9] Better displaying of the progress bar --- WooCommerce/src/main/res/layout/order_detail_payment_info.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WooCommerce/src/main/res/layout/order_detail_payment_info.xml b/WooCommerce/src/main/res/layout/order_detail_payment_info.xml index 1152c5feec6..0566a777164 100644 --- a/WooCommerce/src/main/res/layout/order_detail_payment_info.xml +++ b/WooCommerce/src/main/res/layout/order_detail_payment_info.xml @@ -388,7 +388,7 @@ android:paddingEnd="@dimen/major_100" android:text="@string/orderdetail_see_receipt_button" android:textAllCaps="false" - app:layout_constraintEnd_toStartOf="@id/paymentInfo_seeReceiptButton_progressBar" + app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> From 3821eb77231a1819e7f8c86ea49c0ba564d271af Mon Sep 17 00:00:00 2001 From: Andrey Date: Mon, 29 Jan 2024 18:03:16 +0100 Subject: [PATCH 9/9] Fixed and added tests --- .../ui/orders/OrderDetailViewModelTest.kt | 45 ++++++++++++++++--- 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/orders/OrderDetailViewModelTest.kt b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/orders/OrderDetailViewModelTest.kt index bdbafe61f95..ad4ad6ae310 100644 --- a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/orders/OrderDetailViewModelTest.kt +++ b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/orders/OrderDetailViewModelTest.kt @@ -40,6 +40,7 @@ import com.woocommerce.android.ui.payments.tracking.PaymentsFlowTracker import com.woocommerce.android.ui.products.ProductDetailRepository import com.woocommerce.android.ui.products.addons.AddonRepository import com.woocommerce.android.util.ContinuationWrapper +import com.woocommerce.android.util.captureValues import com.woocommerce.android.viewmodel.BaseUnitTest import com.woocommerce.android.viewmodel.MultiLiveEvent.Event.ShowSnackbar import com.woocommerce.android.viewmodel.MultiLiveEvent.Event.ShowUndoSnackbar @@ -326,7 +327,7 @@ class OrderDetailViewModelTest : BaseUnitTest() { } @Test - fun `given receipt is available and order is paid, when view model started, then state with receipt isReceiptButtonsVisible true emitted`() = + fun `given receipt is available and order is paid, when view model started, then state with receipt is visible emitted`() = testBlocking { // GIVEN whenever(paymentReceiptHelper.isReceiptAvailable(any())).thenReturn(true) @@ -350,11 +351,13 @@ class OrderDetailViewModelTest : BaseUnitTest() { viewModel.start() // THEN - assertThat(detailViewState!!.orderInfo!!.isReceiptButtonsVisible).isTrue() + assertThat(detailViewState!!.orderInfo!!.receiptButtonStatus).isEqualTo( + OrderDetailViewState.ReceiptButtonStatus.Visible + ) } @Test - fun `given receipt is available and order not paid, when view model started, then state with receipt isReceiptButtonsVisible false emitted`() = + fun `given receipt is available and order not paid, when view model started, then state with receipt is hidden emitted`() = testBlocking { // GIVEN whenever(paymentReceiptHelper.isReceiptAvailable(any())).thenReturn(true) @@ -378,11 +381,13 @@ class OrderDetailViewModelTest : BaseUnitTest() { viewModel.start() // THEN - assertThat(detailViewState!!.orderInfo!!.isReceiptButtonsVisible).isFalse() + assertThat(detailViewState!!.orderInfo!!.receiptButtonStatus).isEqualTo( + OrderDetailViewState.ReceiptButtonStatus.Hidden + ) } @Test - fun `given receipt is not available, when view model started, then state with receipt isReceiptButtonsVisible false emitted`() = + fun `given receipt is not available, when view model started, then state with receipt is hidden emitted`() = testBlocking { // GIVEN whenever(paymentReceiptHelper.isReceiptAvailable(any())).thenReturn(false) @@ -402,7 +407,9 @@ class OrderDetailViewModelTest : BaseUnitTest() { viewModel.start() // THEN - assertThat(detailViewState!!.orderInfo!!.isReceiptButtonsVisible).isFalse() + assertThat(detailViewState!!.orderInfo!!.receiptButtonStatus).isEqualTo( + OrderDetailViewState.ReceiptButtonStatus.Hidden + ) } @Test @@ -1275,6 +1282,32 @@ class OrderDetailViewModelTest : BaseUnitTest() { assertThat((viewModel.event.value as PreviewReceipt).billingEmail).isEqualTo(order.billingAddress.email) } + @Test + fun `when onSeeReceiptClicked clicked, then loading receipt status emitted`() = + testBlocking { + // GIVEN + whenever(orderDetailRepository.getOrderById(any())).thenReturn(order) + whenever(orderDetailRepository.fetchOrderNotes(any())).thenReturn(false) + whenever(addonsRepository.containsAddonsFrom(any())).thenReturn(false) + val receiptUrl = "https://example.com" + whenever(paymentReceiptHelper.getReceiptUrl(order.id)).thenReturn(Result.success(receiptUrl)) + + // WHEN + viewModel.start() + + val states = viewModel.viewStateData.liveData.captureValues() + + viewModel.onSeeReceiptClicked() + + // THEN + assertThat((states.last()).orderInfo!!.receiptButtonStatus).isEqualTo( + OrderDetailViewState.ReceiptButtonStatus.Visible + ) + assertThat((states[states.size - 2]).orderInfo!!.receiptButtonStatus).isEqualTo( + OrderDetailViewState.ReceiptButtonStatus.Loading + ) + } + @Test fun `given order is paid, when status is processing order complete button should be visible`() = testBlocking {