Skip to content

Commit

Permalink
Span PMs across the PaymentSheet when there are only two of them. (#4432
Browse files Browse the repository at this point in the history
)

* fix width of PMs when only two methods

* spread across for all number of PMs rather than just 2
  • Loading branch information
skyler-stripe committed Dec 6, 2021
1 parent f609719 commit a20da21
Showing 1 changed file with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,21 @@ internal class AddPaymentMethodsAdapter(
val targetWidth = parent.measuredWidth - parent.paddingStart - parent.paddingEnd
val minItemWidth = 100 * parent.context.resources.displayMetrics.density +
itemView.marginEnd + itemView.marginStart
// numVisibleItems is incremented in steps of 0.5 items (1, 1.5, 2, 2.5, 3, ...)
val numVisibleItems = (targetWidth * 2 / minItemWidth).toInt() / 2f

// if all items fit at min width, then span them across the sheet evenly filling it.
// otherwise the number of items visible should be a multiple of .5
val viewWidth =
targetWidth / numVisibleItems - itemView.marginEnd - itemView.marginStart
itemView.layoutParams.width = viewWidth.toInt()
if (minItemWidth * paymentMethods.size < targetWidth) {
targetWidth / paymentMethods.size
} else {
// numVisibleItems is incremented in steps of 0.5 items
// (1, 1.5, 2, 2.5, 3, ...)
val numVisibleItems = (targetWidth * 2 / minItemWidth).toInt() / 2f
targetWidth / numVisibleItems
}

itemView.layoutParams.width =
viewWidth.toInt() - itemView.marginEnd - itemView.marginStart
itemView.setOnClickListener {
onItemSelected(bindingAdapterPosition)
}
Expand Down

0 comments on commit a20da21

Please sign in to comment.