Skip to content

Commit

Permalink
Remove LiveData from PaymentSessionViewModel.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaynewstrom-stripe committed Oct 27, 2023
1 parent 12f5150 commit 5331af5
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 155 deletions.
21 changes: 12 additions & 9 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,9 +65,9 @@ class PaymentSession @VisibleForTesting internal constructor(

init {
lifecycleOwner.lifecycle.addObserver(lifecycleObserver)
viewModel.networkState.observe(
lifecycleOwner,
{

lifecycleOwner.lifecycleScope.launch {
viewModel.networkState.collect {
it?.let { networkState ->
listener?.onCommunicatingStateChanged(
when (networkState) {
Expand All @@ -75,14 +77,15 @@ class PaymentSession @VisibleForTesting internal constructor(
)
}
}
)
}

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ 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.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow

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

private val _paymentSessionDataLiveData = MutableLiveData<PaymentSessionData>()
val paymentSessionDataLiveData: LiveData<PaymentSessionData> = _paymentSessionDataLiveData
private val _paymentSessionDataStateFlow = MutableStateFlow<PaymentSessionData?>(null)
val paymentSessionDataStateFlow: StateFlow<PaymentSessionData?> = _paymentSessionDataStateFlow.asStateFlow()

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

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

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

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

sealed class FetchCustomerResult {
Expand Down
Loading

0 comments on commit 5331af5

Please sign in to comment.