@@ -11,7 +11,6 @@ import android.os.Bundle
1111import android.util.AttributeSet
1212import android.view.KeyEvent
1313import android.view.LayoutInflater
14- import android.view.View
1514import android.widget.*
1615import androidx.appcompat.app.AlertDialog
1716import androidx.core.view.isVisible
@@ -25,6 +24,7 @@ import io.snabble.sdk.ui.Keyguard
2524import io.snabble.sdk.ui.R
2625import io.snabble.sdk.ui.SnabbleUI
2726import io.snabble.sdk.ui.checkout.CheckoutHelper
27+ import io.snabble.sdk.ui.databinding.SnabbleViewCheckoutBarBinding
2828import io.snabble.sdk.ui.payment.PaymentInputViewHelper
2929import io.snabble.sdk.ui.payment.SEPALegalInfoHelper
3030import io.snabble.sdk.ui.payment.SelectPaymentMethodFragment
@@ -36,14 +36,9 @@ class CheckoutBar @JvmOverloads constructor(
3636 context : Context , attrs : AttributeSet ? = null , defStyleAttr : Int = 0
3737) : LinearLayout(context, attrs, defStyleAttr), Checkout.OnCheckoutStateChangedListener {
3838 private lateinit var progressDialog: DelayedProgressDialog
39- private val paySelector: View
40- private val paySelectorButton: View
41- private val payIcon: ImageView
42- private val payButton: Button
43- private val googlePayButton: View
44- private val articleCount: TextView
45- private val priceSum: TextView
46- private val priceContainer: FrameLayout
39+
40+ private val binding: SnabbleViewCheckoutBarBinding
41+
4742 private val paymentSelectionHelper by lazy { PaymentSelectionHelper .getInstance() }
4843 private val project by lazy { SnabbleUI .getProject() }
4944 private val cart: ShoppingCart by lazy { project.shoppingCart }
@@ -52,19 +47,13 @@ class CheckoutBar @JvmOverloads constructor(
5247 }
5348
5449 val priceHeight: Int
55- get() = priceSum.height + priceContainer .marginTop * 2
50+ get() = binding. priceSum.height + binding.sumContainer .marginTop * 2
5651
5752 init {
5853 LayoutInflater .from(context).inflate(R .layout.snabble_view_checkout_bar, this , true )
54+ binding = SnabbleViewCheckoutBarBinding .bind(this )
55+
5956 orientation = VERTICAL
60- paySelector = findViewById(R .id.payment_selector)
61- paySelectorButton = findViewById(R .id.payment_selector_button)
62- payIcon = findViewById(R .id.payment_icon)
63- payButton = findViewById(R .id.pay)
64- googlePayButton = findViewById(R .id.google_pay_button)
65- articleCount = findViewById(R .id.article_count)
66- priceSum = findViewById(R .id.price_sum)
67- priceContainer = findViewById(R .id.sum_container)
6857
6958 if (! isInEditMode) {
7059 initBusinessLogic()
@@ -76,21 +65,25 @@ class CheckoutBar @JvmOverloads constructor(
7665 update()
7766 })
7867
79- paySelectorButton .setOnClickListener {
68+ binding.paymentSelectorButton .setOnClickListener {
8069 paymentSelectionHelper.showDialog(UIUtils .getHostFragmentActivity(context))
8170 }
8271
83- payButton.setOneShotClickListener {
72+ binding.paymentSelectorButtonBig.setOnClickListener {
73+ paymentSelectionHelper.showDialog(UIUtils .getHostFragmentActivity(context))
74+ }
75+
76+ binding.pay.setOneShotClickListener {
8477 cart.taxation = ShoppingCart .Taxation .UNDECIDED
85- payButtonClick ()
78+ payClick ()
8679 }
8780
88- googlePayButton.setOneShotClickListener {
81+ binding.googlePayButtonLayout. googlePayButton.setOneShotClickListener {
8982 val packageName = " com.google.android.apps.walletnfcrel"
9083 val pm = context.packageManager
9184 try {
9285 pm.getPackageInfo(packageName, PackageManager .GET_ACTIVITIES )
93- payButtonClick ()
86+ payClick ()
9487 } catch (e: PackageManager .NameNotFoundException ) {
9588 try {
9689 context.startActivity(Intent (Intent .ACTION_VIEW ,
@@ -107,7 +100,7 @@ class CheckoutBar @JvmOverloads constructor(
107100
108101 progressDialog = DelayedProgressDialog (context)
109102 progressDialog.setProgressStyle(ProgressDialog .STYLE_SPINNER )
110- progressDialog.setMessage(getContext() .getString(R .string.Snabble_pleaseWait ))
103+ progressDialog.setMessage(context .getString(R .string.Snabble_pleaseWait ))
111104 progressDialog.setCanceledOnTouchOutside(false )
112105 progressDialog.setCancelable(true )
113106 progressDialog.setOnKeyListener(DialogInterface .OnKeyListener { dialogInterface: DialogInterface , _, keyEvent: KeyEvent ->
@@ -134,7 +127,7 @@ class CheckoutBar @JvmOverloads constructor(
134127 })
135128 }
136129
137- private fun payButtonClick () {
130+ private fun payClick () {
138131 if (cart.isRestorable) {
139132 cart.restore()
140133 update()
@@ -144,50 +137,62 @@ class CheckoutBar @JvmOverloads constructor(
144137 }
145138
146139 private fun update () {
147- updatePaySelector ()
148- updatePayButtonAndText ()
140+ updatePaymentSelector ()
141+ updatePayAndText ()
149142 }
150143
151- private fun updatePaySelector () {
144+ private fun updatePaymentSelector () {
152145 val entry = paymentSelectionHelper.selectedEntry.value
153146 if (entry == null ) {
154- paySelector .visibility = GONE
147+ binding.paymentSelector .visibility = GONE
155148 } else {
156149 val pcs = Snabble .getInstance().paymentCredentialsStore
157150 val hasNoPaymentMethods = pcs.usablePaymentCredentialsCount == 0
158151 val isHidden = project.paymentMethodDescriptors.size == 1 && hasNoPaymentMethods
159- paySelector .visibility = if (isHidden) GONE else VISIBLE
160- payIcon .setImageResource(entry.iconResId)
152+ binding.paymentSelector .visibility = if (isHidden) GONE else VISIBLE
153+ binding.paymentIcon .setImageResource(entry.iconResId)
161154 }
162155 }
163156
164- private fun updatePayButtonAndText () {
157+ private fun updatePayAndText () {
165158 cart.let { cart ->
166159 val quantity = cart.totalQuantity
167160 val price = cart.totalPrice
168161 val articlesText = resources.getQuantityText(R .plurals.Snabble_Shoppingcart_numberOfItems , quantity)
169- articleCount.text = String .format(articlesText.toString(), quantity)
170- priceSum.text = project.priceFormatter.format(price)
171-
172-
162+ binding.articleCount.text = String .format(articlesText.toString(), quantity)
163+ binding.priceSum.text = project.priceFormatter.format(price)
173164
174165 val onlinePaymentAvailable = cart.availablePaymentMethods != null && cart.availablePaymentMethods.isNotEmpty()
175- payButton .isEnabled = price > 0 && (onlinePaymentAvailable || paymentSelectionHelper.selectedEntry.value != null )
166+ binding.pay .isEnabled = price > 0 && (onlinePaymentAvailable || paymentSelectionHelper.selectedEntry.value != null )
176167
177- val entry = paymentSelectionHelper.selectedEntry.value
178- if (entry?.paymentMethod == PaymentMethod .GOOGLE_PAY && price > 0 ) {
179- payButton.isVisible = false
180- googlePayButton.isVisible = true
168+ var showBigSelector = paymentSelectionHelper.shouldShowBigSelector()
169+ val showSmallSelector = paymentSelectionHelper.shouldShowSmallSelector()
170+
171+ if (paymentSelectionHelper.shouldShowPayButton()) {
172+ binding.pay.isEnabled = true
173+ if (paymentSelectionHelper.shouldShowGooglePayButton()) {
174+ showBigSelector = false
175+ binding.pay.isVisible = false
176+ binding.googlePayButtonLayout.root.isVisible = ! showBigSelector
177+ } else {
178+ binding.pay.isVisible = ! showBigSelector
179+ binding.googlePayButtonLayout.root.isVisible = false
180+ }
181181 } else {
182- payButton.isVisible = true
183- googlePayButton.isVisible = false
182+ binding.pay.isVisible = true
183+ binding.pay.isEnabled = false
184+ binding.googlePayButtonLayout.root.isVisible = false
184185 }
185186
187+ binding.paymentSelectorButtonBig.isVisible = showBigSelector
188+ binding.paymentSelector.isVisible = showSmallSelector
189+ binding.paymentActive.isVisible = ! showBigSelector
190+
186191 if (cart.isRestorable) {
187- payButton .isEnabled = true
188- payButton .setText(R .string.Snabble_Shoppingcart_emptyState_restoreButtonTitle )
192+ binding.pay .isEnabled = true
193+ binding.pay .setText(R .string.Snabble_Shoppingcart_emptyState_restoreButtonTitle )
189194 } else {
190- payButton .setText(I18nUtils .getIdentifierForProject(resources, project, R .string.Snabble_Shoppingcart_buyProducts_now ))
195+ binding.pay .setText(I18nUtils .getIdentifierForProject(resources, project, R .string.Snabble_Shoppingcart_buyProducts_now ))
191196 }
192197 }
193198 }
0 commit comments