@@ -2,19 +2,25 @@ package com.zettle.payments.android.kotlin_example
22
33import android.app.Activity
44import android.content.Intent
5+ import android.os.Build
56import android.os.Bundle
7+ import android.os.Parcelable
68import android.text.SpannableStringBuilder
79import android.view.ViewGroup
810import android.widget.Button
911import android.widget.CheckBox
1012import android.widget.EditText
13+ import android.widget.TextView
1114import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
1215import androidx.appcompat.app.AppCompatActivity
1316import androidx.lifecycle.MutableLiveData
1417import com.google.android.material.snackbar.Snackbar
15- import com.zettle.sdk.feature.cardreader.payment.TippingStyle
18+ import com.zettle.sdk.feature.cardreader.payment.PayPalReaderTippingStyle
19+ import com.zettle.sdk.feature.cardreader.payment.TippingConfiguration
20+ import com.zettle.sdk.feature.cardreader.payment.TippingPercentageOptions
1621import com.zettle.sdk.feature.cardreader.payment.Transaction
1722import com.zettle.sdk.feature.cardreader.payment.TransactionReference
23+ import com.zettle.sdk.feature.cardreader.payment.ZettleReaderTippingStyle
1824import com.zettle.sdk.feature.cardreader.payment.refunds.CardPaymentPayload
1925import com.zettle.sdk.feature.cardreader.payment.refunds.RefundPayload
2026import com.zettle.sdk.feature.cardreader.ui.CardReaderAction
@@ -38,10 +44,18 @@ class CardReaderActivity : AppCompatActivity() {
3844 private lateinit var refundAmountEditText: EditText
3945 private lateinit var settingsButton: Button
4046 private lateinit var amountEditText: EditText
41- private lateinit var tippingStyleButton: Button
4247 private lateinit var installmentsCheckBox: CheckBox
4348 private lateinit var lastPaymentTraceId: MutableLiveData <String ?>
44- private var tippingStyle: TippingStyle = TippingStyle .None
49+
50+ private lateinit var selectTippingStyleButton: Button
51+ private lateinit var ztrTippingStyleLabel: TextView
52+ private lateinit var pprTippingStyleLabel: TextView
53+ private lateinit var openPprTippingSettingsButton: Button
54+
55+ private var tippingConfiguration: TippingConfiguration ? = TippingConfiguration (
56+ ZettleReaderTippingStyle .None ,
57+ PayPalReaderTippingStyle .None
58+ )
4559
4660 override fun onCreate (savedInstanceState : Bundle ? ) {
4761 super .onCreate(savedInstanceState)
@@ -51,10 +65,16 @@ class CardReaderActivity : AppCompatActivity() {
5165 retrieveButton = findViewById(R .id.retrieve_btn)
5266 settingsButton = findViewById(R .id.settings_btn)
5367 amountEditText = findViewById(R .id.amount_input)
54- tippingStyleButton = findViewById(R .id.tipping_style_btn)
5568 refundAmountEditText = findViewById(R .id.refund_amount_input)
5669 installmentsCheckBox = findViewById(R .id.installments_check_box)
5770
71+ selectTippingStyleButton = findViewById(R .id.select_tipping_style_btn)
72+ selectTippingStyleButton.setOnClickListener { selectTippingStyle() }
73+ ztrTippingStyleLabel = findViewById(R .id.ztr_tipping_style_label)
74+ pprTippingStyleLabel = findViewById(R .id.ppr_tipping_style_label)
75+ openPprTippingSettingsButton = findViewById(R .id.open_ppr_tipping_settings_btn)
76+ openPprTippingSettingsButton.setOnClickListener { onTippingSettingsClicked() }
77+
5878 lastPaymentTraceId = MutableLiveData ()
5979 lastPaymentTraceId.observe(this ) { value: String? ->
6080 refundButton.isEnabled = value != null
@@ -65,13 +85,8 @@ class CardReaderActivity : AppCompatActivity() {
6585 refundButton.setOnClickListener { onRefundClicked() }
6686 retrieveButton.setOnClickListener { onRetrieveClicked() }
6787 settingsButton.setOnClickListener { onSettingsClicked() }
68- tippingStyleButton.setOnClickListener { onTippingStyleClicked() }
6988
7089 setTippingStyleTitle()
71- supportFragmentManager.setFragmentResultListener(TippingStyleBottomSheet .REQUEST_KEY , this ) { _, result ->
72- tippingStyle = result.getSerializable(TippingStyleBottomSheet .TIPPING_STYLE_KEY ) as ? TippingStyle ? : TippingStyle .None
73- setTippingStyleTitle()
74- }
7590 }
7691
7792 private val paymentLauncher =
@@ -158,9 +173,10 @@ class CardReaderActivity : AppCompatActivity() {
158173 val intent: Intent = CardReaderAction .Payment (
159174 reference = reference,
160175 amount = amount,
161- tippingStyle = tippingStyle ,
176+ tippingConfiguration = tippingConfiguration ,
162177 enableInstallments = enableInstallments
163178 ).charge(this )
179+
164180 paymentLauncher.launch(intent)
165181 }
166182
@@ -209,13 +225,50 @@ class CardReaderActivity : AppCompatActivity() {
209225 startActivity(CardReaderAction .Settings .show(this ))
210226 }
211227
212- private fun onTippingStyleClicked () {
213- TippingStyleBottomSheet .newInstance()
214- .show(supportFragmentManager, TippingStyleBottomSheet ::class .java.simpleName)
228+ private val selectTippingStyleLauncher =
229+ registerForActivityResult(StartActivityForResult ()) { result ->
230+ if (result.resultCode != Activity .RESULT_OK ) {
231+ return @registerForActivityResult
232+ }
233+
234+ val data = result.data ? : return @registerForActivityResult
235+ data.parcelableExtra<TippingConfiguration >(
236+ SelectTippingStyleActivity .TIPPING_CONFIGS_KEY
237+ )?.let {
238+ tippingConfiguration = it
239+ setTippingStyleTitle()
240+ }
241+ }
242+
243+ private fun selectTippingStyle () {
244+ val intent = Intent (this , SelectTippingStyleActivity ::class .java)
245+ intent.putExtra(SelectTippingStyleActivity .TIPPING_CONFIGS_KEY , tippingConfiguration)
246+ selectTippingStyleLauncher.launch(intent)
215247 }
216248
217249 private fun setTippingStyleTitle () {
218- val tippingStyleTitle = " Tipping Style - ${tippingStyle.name} "
219- tippingStyleButton.text = tippingStyleTitle
250+ tippingConfiguration?.let {
251+ ztrTippingStyleLabel.text = " Zettle Reader: ${it.zettleReaderTippingStyle.name} "
252+ val pprStyle = when (val style = it.payPalReaderTippingStyle) {
253+ is PayPalReaderTippingStyle .PredefinedPercentage -> style.javaClass.simpleName + style.options.displayText()
254+ else -> style.javaClass.simpleName
255+ }
256+ pprTippingStyleLabel.text = " PayPal Reader: $pprStyle "
257+ }
220258 }
259+
260+ private fun onTippingSettingsClicked () {
261+ val intent = CardReaderAction .TippingSettings ().show(this )
262+ startActivity(intent)
263+ }
264+
265+ private fun TippingPercentageOptions?.displayText () = when (this ) {
266+ null -> " (Options = Null)"
267+ else -> " ($option1 , $option2 , $option3 )"
268+ }
269+ }
270+
271+ inline fun <reified T : Parcelable > Intent.parcelableExtra (key : String ): T ? = when {
272+ Build .VERSION .SDK_INT >= 33 -> getParcelableExtra(key, T ::class .java)
273+ else -> @Suppress(" DEPRECATION" ) getParcelableExtra(key) as ? T
221274}
0 commit comments