Skip to content

Commit

Permalink
Merge pull request #13038 from woocommerce/custom-payment-iu-remove-c…
Browse files Browse the repository at this point in the history
…ollect-payment-button

[POS][Custom payment UI] – Remove "collect payment" button
  • Loading branch information
samiuelson authored Dec 11, 2024
2 parents 31271ac + d3b8dfa commit b96a66b
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.asLiveData
import androidx.lifecycle.map
import androidx.lifecycle.viewModelScope
import com.woocommerce.android.AppPrefs
import com.woocommerce.android.cardreader.CardReaderManager
import com.woocommerce.android.cardreader.payments.PaymentData
Expand Down Expand Up @@ -68,7 +67,6 @@ class CardReaderPaymentViewModel @Inject constructor(
}

private val paymentController = CardReaderPaymentController(
scope = viewModelScope,
cardReaderManager = cardReaderManager,
orderRepository = orderRepository,
selectedSite = selectedSite,
Expand Down Expand Up @@ -120,15 +118,12 @@ class CardReaderPaymentViewModel @Inject constructor(
fun retry(orderId: Long, billingEmail: String, paymentData: PaymentData, amountLabel: String) =
paymentController.retry(orderId, billingEmail, paymentData, amountLabel)

@VisibleForTesting
fun reFetchOrder() = paymentController.reFetchOrder()

fun onViewCreated() = paymentController.onViewCreated()

fun onPrintResult(result: PrintJobResult) = paymentController.onPrintResult(result)

@VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
public override fun onCleared() = paymentController.onCleared()
public override fun onCleared() = paymentController.stop()

fun onBackPressed() = paymentController.onBackPressed()
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,16 @@ import com.woocommerce.android.util.PrintHtmlHelper.PrintJobResult.FAILED
import com.woocommerce.android.util.PrintHtmlHelper.PrintJobResult.STARTED
import com.woocommerce.android.util.WooLog
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.wordpress.android.fluxc.store.WooCommerceStore
Expand All @@ -88,7 +92,6 @@ private const val CANADA_FEE_FLAT_IN_CENTS = 15L

@Suppress("LongParameterList", "LargeClass")
class CardReaderPaymentController(
private val scope: CoroutineScope,
private val cardReaderManager: CardReaderManager,
private val orderRepository: OrderDetailRepository,
private val selectedSite: SelectedSite,
Expand All @@ -113,6 +116,8 @@ class CardReaderPaymentController(
private val cardReaderType: CardReaderType,
private val isTTPPaymentInProgress: KMutableProperty0<Boolean>,
) {
private var scope: CoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)

private val _paymentState: MutableStateFlow<CardReaderPaymentOrRefundState> =
MutableStateFlow(CardReaderPaymentState.LoadingData(::onCancelPaymentFlow))
val paymentState: StateFlow<CardReaderPaymentOrRefundState> = _paymentState
Expand All @@ -131,6 +136,8 @@ class CardReaderPaymentController(
}

fun start() {
scope.cancel()
scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
if (cardReaderManager.readerStatus.value is CardReaderStatus.Connected) {
startFlowWhenReaderConnected()
} else {
Expand Down Expand Up @@ -778,10 +785,11 @@ class CardReaderPaymentController(
}
}

fun onCleared() {
fun stop() {
paymentDataForRetry?.let {
cardReaderManager.cancelPayment(it)
}
scope.cancel()
}

fun onBackPressed() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import com.woocommerce.android.ui.payments.tracking.CardReaderTrackingInfoKeeper
import com.woocommerce.android.ui.payments.tracking.PaymentsFlowTracker
import com.woocommerce.android.util.CoroutineDispatchers
import com.woocommerce.android.util.CurrencyFormatter
import kotlinx.coroutines.CoroutineScope
import org.wordpress.android.fluxc.store.WooCommerceStore
import javax.inject.Inject
import kotlin.reflect.KMutableProperty0
Expand Down Expand Up @@ -50,10 +49,8 @@ class CardReaderPaymentControllerFactory @Inject constructor(
fun create(
orderId: Long,
paymentType: PaymentType,
coroutineScope: CoroutineScope,
isTTPPaymentInProgress: KMutableProperty0<Boolean>,
): CardReaderPaymentController = CardReaderPaymentController(
scope = coroutineScope,
cardReaderManager = cardReaderManager,
orderRepository = orderRepository,
selectedSite = selectedSite,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import androidx.compose.animation.AnimatedVisibilityScope
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.slideInVertically
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
Expand All @@ -24,12 +23,7 @@ import androidx.compose.material.Divider
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
Expand All @@ -42,12 +36,10 @@ import com.woocommerce.android.R
import com.woocommerce.android.ui.woopos.common.composeui.WooPosPreview
import com.woocommerce.android.ui.woopos.common.composeui.WooPosTheme
import com.woocommerce.android.ui.woopos.common.composeui.component.Button
import com.woocommerce.android.ui.woopos.common.composeui.component.WooPosButtonLarge
import com.woocommerce.android.ui.woopos.common.composeui.component.WooPosErrorScreen
import com.woocommerce.android.ui.woopos.common.composeui.component.WooPosShimmerBox
import com.woocommerce.android.ui.woopos.common.composeui.toAdaptivePadding
import com.woocommerce.android.ui.woopos.home.totals.payment.success.WooPosPaymentSuccessScreen
import kotlinx.coroutines.delay

@Composable
fun WooPosTotalsScreen(modifier: Modifier = Modifier) {
Expand All @@ -65,7 +57,7 @@ private fun WooPosTotalsScreen(
Box(modifier = modifier) {
StateChangeAnimated(visible = state is WooPosTotalsViewState.Totals) {
if (state is WooPosTotalsViewState.Totals) {
TotalsLoaded(state = state, onUIEvent = onUIEvent)
TotalsLoaded(state = state)
}
}

Expand Down Expand Up @@ -109,15 +101,7 @@ private fun StateChangeAnimated(
@Composable
private fun TotalsLoaded(
state: WooPosTotalsViewState.Totals,
onUIEvent: (WooPosTotalsUIEvent) -> Unit
) {
var isButtonVisible by remember { mutableStateOf(false) }

LaunchedEffect(Unit) {
delay(300)
isButtonVisible = true
}

Column(
modifier = Modifier
.fillMaxSize()
Expand All @@ -144,17 +128,6 @@ private fun TotalsLoaded(
style = MaterialTheme.typography.body1,
)
}

AnimatedVisibility(visible = isButtonVisible) {
WooPosButtonLarge(
text = stringResource(R.string.woopos_payment_collect_payment_label),
onClick = { onUIEvent(WooPosTotalsUIEvent.CollectPaymentClicked) },
modifier = Modifier
.animateEnterExit(
enter = slideInVertically { it },
)
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.woocommerce.android.ui.woopos.home.totals

sealed class WooPosTotalsUIEvent {
data object CollectPaymentClicked : WooPosTotalsUIEvent()
data object OnNewTransactionClicked : WooPosTotalsUIEvent()
data object RetryOrderCreationClicked : WooPosTotalsUIEvent()
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ class WooPosTotalsViewModel @Inject constructor(
cardReaderPaymentController = cardReaderPaymentControllerFactory.create(
orderId = orderId,
paymentType = PaymentOrRefund.Payment.PaymentType.WOO_POS,
coroutineScope = viewModelScope,
isTTPPaymentInProgress = ::isTTPPaymentInProgress,
)
}
Expand All @@ -90,7 +89,6 @@ class WooPosTotalsViewModel @Inject constructor(

fun onUIEvent(event: WooPosTotalsUIEvent) {
when (event) {
is WooPosTotalsUIEvent.CollectPaymentClicked -> collectPayment()
is WooPosTotalsUIEvent.OnNewTransactionClicked -> {
viewModelScope.launch {
childrenToParentEventSender.sendToParent(
Expand Down Expand Up @@ -136,6 +134,7 @@ class WooPosTotalsViewModel @Inject constructor(

is ParentToChildrenEvent.BackFromCheckoutToCartClicked -> {
cardReaderPaymentController?.onBackPressed()
cardReaderPaymentController?.stop()
uiState.value = InitialState
}

Expand Down Expand Up @@ -166,6 +165,10 @@ class WooPosTotalsViewModel @Inject constructor(
}
}

override fun onCleared() {
cardReaderPaymentController?.stop()
}

private fun createOrderDraft(productIds: List<Long>) {
viewModelScope.launch {
uiState.value = WooPosTotalsViewState.Loading
Expand All @@ -176,6 +179,7 @@ class WooPosTotalsViewModel @Inject constructor(
dataState.value = dataState.value.copy(orderId = order.id)
uiState.value = buildWooPosTotalsViewState(order)
analyticsTracker.track(WooPosAnalyticsEvent.Event.OrderCreationSuccess)
collectPayment()
},
onFailure = { error ->
WooLog.e(T.POS, "Order creation failed - $error")
Expand Down Expand Up @@ -207,10 +211,6 @@ class WooPosTotalsViewModel @Inject constructor(
)
}

override fun onCleared() {
cardReaderPaymentController?.onCleared()
}

@Parcelize
private data class TotalsDataState(
val orderId: Long = EMPTY_ORDER_ID,
Expand Down
Loading

0 comments on commit b96a66b

Please sign in to comment.