Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deletion of saved PM #5592

Merged
merged 3 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## X.X.X - 2022-XX-XX

### PaymentSheet

* [FIXED][5592](https://github.com/stripe/stripe-android/pull/5592) Fix deletion of the last used payment method.

## 20.13.0 - 2022-09-19
This release makes the `PaymentMethod.Card.networks` field public, fixes the Alipay integration and the card scan form encoding.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ internal abstract class BasePaymentMethodsListFragment(
}

private fun deletePaymentMethod(item: PaymentOptionsAdapter.Item.SavedPaymentMethod) {
adapter.removeItem(item)
sheetViewModel.removePaymentMethod(item.paymentMethod)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,16 +267,18 @@ internal class PaymentOptionsAdapter(
parent,
width,
lpmRepository,
::onItemSelected
) { position ->
onItemSelected(
position = findInitialSelectedPosition(savedSelection),
isClick = false,
force = true
)
paymentMethodDeleteListener(items[position] as Item.SavedPaymentMethod)
notifyItemRemoved(position)
}
onItemSelectedListener = ::onItemSelected,
onRemoveListener = { position ->
paymentMethodDeleteListener(items[position] as Item.SavedPaymentMethod)
removeItem(items[position])
notifyItemRemoved(position)
onItemSelected(
position = findInitialSelectedPosition(savedSelection),
isClick = false,
force = true
)
}
)
}
}

Expand Down Expand Up @@ -310,7 +312,7 @@ internal class PaymentOptionsAdapter(
private val composeView: ComposeView,
private val width: Dp,
private val lpmRepository: LpmRepository,
private val onRemoveListener: (Int) -> Unit,
@get:VisibleForTesting internal val onRemoveListener: (Int) -> Unit,
private val onItemSelectedListener: ((Int, Boolean) -> Unit)
) : PaymentOptionViewHolder(
composeView
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.stripe.android.paymentsheet

import android.widget.LinearLayout
import androidx.test.core.app.ApplicationProvider
import com.google.common.truth.Truth.assertThat
import com.stripe.android.model.PaymentMethod
import com.stripe.android.model.PaymentMethodFixtures
Expand Down Expand Up @@ -374,6 +376,33 @@ class PaymentOptionsAdapterTest {
.isNull()
}

@Test
fun `Selected item should be updated when deleted`() {
val savedPaymentMethod = paymentMethods[2]
val adapter = createConfiguredAdapter(
fragmentConfig = CONFIG.copy(
isGooglePayReady = true,
savedSelection = SavedSelection.PaymentMethod(savedPaymentMethod.id!!)
),
showGooglePay = false
)

assertThat((adapter.selectedItem as PaymentOptionsAdapter.Item.SavedPaymentMethod).paymentMethod)
.isEqualTo(savedPaymentMethod)

val position = adapter.items.indexOf(adapter.selectedItem)
val viewHolder = adapter.onCreateViewHolder(
LinearLayout(ApplicationProvider.getApplicationContext()),
adapter.getItemViewType(position)
) as PaymentOptionsAdapter.SavedPaymentMethodViewHolder
adapter.bindViewHolder(viewHolder, position)
viewHolder.onRemoveListener(position)

assertThat(paymentMethodsDeleted.map { it.paymentMethod }).containsExactly(savedPaymentMethod)
assertThat((adapter.selectedItem as PaymentOptionsAdapter.Item.SavedPaymentMethod).paymentMethod)
.isEqualTo(paymentMethods[0])
}

private fun createConfiguredAdapter(
fragmentConfig: FragmentConfig = CONFIG,
paymentMethods: List<PaymentMethod> = this.paymentMethods,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,26 +257,6 @@ internal class PaymentSheetListFragmentTest : PaymentSheetViewModelTestInjection
}
}

@Test
fun `deletePaymentMethod() removes item from adapter`() {
createScenario(
initialState = Lifecycle.State.INITIALIZED,
paymentMethods = PAYMENT_METHODS
).moveToState(Lifecycle.State.STARTED).onFragment { fragment ->
idleLooper()

val adapter = recyclerView(fragment).adapter as PaymentOptionsAdapter
assertThat(adapter.itemCount).isEqualTo(3)

fragment.isEditing = true
adapter.paymentMethodDeleteListener(
adapter.items[2] as PaymentOptionsAdapter.Item.SavedPaymentMethod
)

assertThat(adapter.itemCount).isEqualTo(2)
}
}

private fun recyclerView(it: PaymentSheetListFragment) =
it.requireView().findViewById<RecyclerView>(R.id.recycler)

Expand Down