Skip to content

Commit

Permalink
Remove LiveData from PaymentSessionViewModel. (#7526)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaynewstrom-stripe authored Nov 13, 2023
1 parent 93907e7 commit dd30bf1
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 163 deletions.
37 changes: 20 additions & 17 deletions payments-core/src/main/java/com/stripe/android/PaymentSession.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.OnLifecycleEvent
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStoreOwner
import androidx.lifecycle.lifecycleScope
import com.stripe.android.PaymentSession.PaymentSessionListener
import com.stripe.android.view.ActivityStarter
import com.stripe.android.view.PaymentFlowActivity
import com.stripe.android.view.PaymentFlowActivityStarter
import com.stripe.android.view.PaymentMethodsActivity
import com.stripe.android.view.PaymentMethodsActivityStarter
import kotlinx.coroutines.launch

/**
* Represents a single start-to-finish payment operation.
Expand Down Expand Up @@ -63,26 +65,23 @@ class PaymentSession @VisibleForTesting internal constructor(

init {
lifecycleOwner.lifecycle.addObserver(lifecycleObserver)
viewModel.networkState.observe(
lifecycleOwner,
{
it?.let { networkState ->
listener?.onCommunicatingStateChanged(
when (networkState) {
PaymentSessionViewModel.NetworkState.Active -> true
PaymentSessionViewModel.NetworkState.Inactive -> false
}
)
}

lifecycleOwner.lifecycleScope.launch {
viewModel.networkState.collect { networkState ->
listener?.onCommunicatingStateChanged(
when (networkState) {
PaymentSessionViewModel.NetworkState.Active -> true
PaymentSessionViewModel.NetworkState.Inactive -> false
}
)
}
)
}

viewModel.paymentSessionDataLiveData.observe(
lifecycleOwner,
{
listener?.onPaymentSessionDataChanged(it)
lifecycleOwner.lifecycleScope.launch {
viewModel.paymentSessionDataStateFlow.collect { sessionData ->
listener?.onPaymentSessionDataChanged(sessionData)
}
)
}
}

/**
Expand Down Expand Up @@ -160,21 +159,25 @@ class PaymentSession @VisibleForTesting internal constructor(
}
false
}

Activity.RESULT_OK -> when (requestCode) {
PaymentMethodsActivityStarter.REQUEST_CODE -> {
onPaymentMethodResult(data)
true
}

PaymentFlowActivityStarter.REQUEST_CODE -> {
data.getParcelableExtra<PaymentSessionData>(EXTRA_PAYMENT_SESSION_DATA)?.let {
viewModel.onPaymentFlowResult(it)
}
true
}

else -> {
false
}
}

else -> false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ import com.stripe.android.model.Customer
import com.stripe.android.model.PaymentMethod
import com.stripe.android.utils.requireApplication
import com.stripe.android.view.PaymentMethodsActivityStarter
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow

internal class PaymentSessionViewModel(
application: Application,
Expand All @@ -29,12 +36,15 @@ internal class PaymentSessionViewModel(
if (value != field) {
field = value
savedStateHandle.set(KEY_PAYMENT_SESSION_DATA, value)
_paymentSessionDataLiveData.value = value
_paymentSessionDataStateFlow.tryEmit(value)
}
}

private val _paymentSessionDataLiveData = MutableLiveData<PaymentSessionData>()
val paymentSessionDataLiveData: LiveData<PaymentSessionData> = _paymentSessionDataLiveData
private val _paymentSessionDataStateFlow = MutableSharedFlow<PaymentSessionData>(
replay = 1,
onBufferOverflow = BufferOverflow.DROP_OLDEST
)
val paymentSessionDataStateFlow: SharedFlow<PaymentSessionData> = _paymentSessionDataStateFlow.asSharedFlow()

init {
// read from saved state handle
Expand All @@ -43,8 +53,8 @@ internal class PaymentSessionViewModel(
}
}

private val _networkState: MutableLiveData<NetworkState> = MutableLiveData()
internal val networkState: LiveData<NetworkState> = _networkState
private val _networkState: MutableStateFlow<NetworkState> = MutableStateFlow(NetworkState.Inactive)
internal val networkState: StateFlow<NetworkState> = _networkState.asStateFlow()

@JvmSynthetic
fun updateCartTotal(@IntRange(from = 0) cartTotal: Long) {
Expand Down Expand Up @@ -217,7 +227,7 @@ internal class PaymentSessionViewModel(

@JvmSynthetic
fun onListenerAttached() {
_paymentSessionDataLiveData.value = paymentSessionData
_paymentSessionDataStateFlow.tryEmit(paymentSessionData)
}

sealed class FetchCustomerResult {
Expand Down
Loading

0 comments on commit dd30bf1

Please sign in to comment.