Skip to content

Commit 7143b6f

Browse files
authored
feat: xml demo (#6)
1 parent 8b5bcf1 commit 7143b6f

6 files changed

Lines changed: 433 additions & 113 deletions

File tree

demo/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ dependencies {
7171
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
7272
implementation 'androidx.appcompat:appcompat:1.6.1'
7373
implementation project(':library')
74+
implementation 'com.google.android.material:material:1.10.0'
7475

75-
testImplementation 'junit:junit:4.13.2'
76+
testImplementation 'junit:junit:4.13.2'
7677
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
7778
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
7879
androidTestImplementation platform('androidx.compose:compose-bom:2023.09.00')

demo/src/main/java/com/paypal/messagesdemo/XmlActivity.kt

Lines changed: 163 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,30 @@ package com.paypal.messagesdemo
33
import android.os.Bundle
44
import android.util.Log
55
import android.view.View
6+
import android.widget.Button
7+
import android.widget.EditText
8+
import android.widget.RadioGroup
69
import android.widget.Toast
10+
import android.widget.ToggleButton
711
import androidx.appcompat.app.AppCompatActivity
8-
import com.paypal.messages.PayPalMessageView
12+
import androidx.compose.ui.graphics.Color
13+
import com.google.android.material.switchmaterial.SwitchMaterial
914
import com.paypal.messages.config.PayPalMessageOfferType
10-
import com.paypal.messages.config.message.PayPalMessageConfig
11-
import com.paypal.messages.config.message.PayPalMessageData
12-
import com.paypal.messages.config.message.PayPalMessageEvents
1315
import com.paypal.messages.config.message.PayPalMessageStyle
1416
import com.paypal.messages.config.message.PayPalMessageViewState
1517
import com.paypal.messages.config.message.style.PayPalMessageAlign
1618
import com.paypal.messages.config.message.style.PayPalMessageColor
1719
import com.paypal.messages.config.message.style.PayPalMessageLogoType
20+
import com.paypal.messages.io.Api
1821
import com.paypal.messagesdemo.databinding.ActivityMessageBinding
19-
import com.paypal.messages.config.PayPalEnvironment as Environment
2022

2123
class XmlActivity : AppCompatActivity() {
2224
private lateinit var binding: ActivityMessageBinding
2325
private val TAG = "XmlActivity"
26+
private var logoType: PayPalMessageLogoType = PayPalMessageLogoType.PRIMARY
27+
private var color: PayPalMessageColor = PayPalMessageColor.BLACK
28+
private var alignment: PayPalMessageAlign = PayPalMessageAlign.LEFT
29+
private var offerType: PayPalMessageOfferType? = null
2430

2531
override fun onCreate(savedInstanceState: Bundle?) {
2632
super.onCreate(savedInstanceState)
@@ -29,23 +35,169 @@ class XmlActivity : AppCompatActivity() {
2935

3036
val payPalMessage = binding.payPalMessage
3137
val progressBar = binding.progressBar
32-
val reloadButton = binding.reloadButton
3338

34-
// TODO add example of adding MessageView here instead of in XML
39+
val editedClientId: EditText? = findViewById<EditText>(R.id.clientId)
40+
41+
val logoTypeRadioGroup = findViewById<RadioGroup>(R.id.logoTypeRadioGroup)
42+
logoTypeRadioGroup.setOnCheckedChangeListener { _, checkedId ->
43+
logoType = when (checkedId) {
44+
R.id.stylePrimary -> PayPalMessageLogoType.PRIMARY
45+
R.id.styleInline -> PayPalMessageLogoType.INLINE
46+
R.id.styleAlternative -> PayPalMessageLogoType.ALTERNATIVE
47+
R.id.styleNone -> PayPalMessageLogoType.NONE
48+
else -> PayPalMessageLogoType.PRIMARY
49+
}
50+
}
51+
52+
val colorRadioGroup = findViewById<RadioGroup>(R.id.colorRadioGroup)
53+
colorRadioGroup.setOnCheckedChangeListener { _, checkedId ->
54+
color = when (checkedId) {
55+
R.id.styleBlack -> PayPalMessageColor.BLACK
56+
R.id.styleWhite -> PayPalMessageColor.WHITE
57+
R.id.styleMonochrome -> PayPalMessageColor.MONOCHROME
58+
R.id.styleGrayscale -> PayPalMessageColor.GRAYSCALE
59+
else -> PayPalMessageColor.BLACK
60+
}
61+
}
62+
63+
val alignmentRadioGroup = findViewById<RadioGroup>(R.id.alignmentRadioGroup)
64+
alignmentRadioGroup.setOnCheckedChangeListener { _, checkedId ->
65+
alignment = when (checkedId) {
66+
R.id.styleLeft -> PayPalMessageAlign.LEFT
67+
R.id.styleCenter -> PayPalMessageAlign.CENTER
68+
R.id.styleRight -> PayPalMessageAlign.RIGHT
69+
else -> PayPalMessageAlign.LEFT
70+
}
71+
}
72+
73+
val shortTerm = findViewById<ToggleButton>(R.id.shortTerm)
74+
val longTerm = findViewById<ToggleButton>(R.id.longTerm)
75+
val payIn1 = findViewById<ToggleButton>(R.id.payIn1)
76+
val credit = findViewById<ToggleButton>(R.id.credit)
77+
78+
fun updateOfferUi (offerName: PayPalMessageOfferType?, isChecked: Boolean) {
79+
shortTerm.isChecked = false
80+
longTerm.isChecked = false
81+
payIn1.isChecked = false
82+
credit.isChecked = false
83+
offerType = null
84+
85+
if ( offerName == PayPalMessageOfferType.PAY_LATER_SHORT_TERM && isChecked) {
86+
shortTerm.isChecked = true
87+
offerType = PayPalMessageOfferType.PAY_LATER_SHORT_TERM
88+
} else if ( offerName == PayPalMessageOfferType.PAY_LATER_LONG_TERM && isChecked) {
89+
longTerm.isChecked = true
90+
offerType = PayPalMessageOfferType.PAY_LATER_LONG_TERM
91+
} else if ( offerName == PayPalMessageOfferType.PAY_LATER_PAY_IN_1 && isChecked) {
92+
payIn1.isChecked = true
93+
offerType = PayPalMessageOfferType.PAY_LATER_PAY_IN_1
94+
} else if ( offerName == PayPalMessageOfferType.PAYPAL_CREDIT_NO_INTEREST && isChecked) {
95+
credit.isChecked = true
96+
offerType = PayPalMessageOfferType.PAYPAL_CREDIT_NO_INTEREST
97+
}
98+
99+
payPalMessage.setOfferType(offerType = offerType)
100+
101+
}
102+
103+
shortTerm.setOnCheckedChangeListener { _, isChecked ->
104+
updateOfferUi(PayPalMessageOfferType.PAY_LATER_SHORT_TERM, isChecked)
105+
}
106+
longTerm.setOnCheckedChangeListener { _, isChecked ->
107+
updateOfferUi(PayPalMessageOfferType.PAY_LATER_LONG_TERM, isChecked)
108+
}
109+
payIn1.setOnCheckedChangeListener { _, isChecked ->
110+
updateOfferUi(PayPalMessageOfferType.PAY_LATER_PAY_IN_1, isChecked)
111+
}
112+
credit.setOnCheckedChangeListener { _, isChecked ->
113+
updateOfferUi(PayPalMessageOfferType.PAYPAL_CREDIT_NO_INTEREST, isChecked)
114+
}
115+
116+
val amount = findViewById<EditText>(R.id.amount)
117+
val buyerCountry = findViewById<EditText>(R.id.buyerCountry)
118+
val stageTag = findViewById<EditText>(R.id.stageTag)
119+
120+
121+
val ignoreCache = findViewById<SwitchMaterial>(R.id.ignoreCache)
122+
val devTouchpoint = findViewById<SwitchMaterial>(R.id.devTouchpoint)
123+
124+
// Get the data from the selected options
125+
fun updateMessageData() {
126+
Api.devTouchpoint = devTouchpoint.isChecked
127+
Api.ignoreCache = ignoreCache.isChecked
128+
129+
if ( editedClientId?.text.toString().isNotBlank() ) {
130+
payPalMessage.setClientId(editedClientId?.text.toString())
131+
} else {
132+
payPalMessage.setClientId("")
133+
}
134+
135+
if ( amount?.text.toString().isNotBlank() ) {
136+
payPalMessage.setAmount(amount?.text.toString().toDouble())
137+
} else {
138+
payPalMessage.setAmount(null)
139+
}
35140

141+
if ( buyerCountry?.text.toString().isNotBlank() ) {
142+
payPalMessage.setBuyerCountry(buyerCountry?.text.toString())
143+
} else {
144+
payPalMessage.setBuyerCountry("US")
145+
}
146+
147+
if ( color === PayPalMessageColor.WHITE ) {
148+
payPalMessage.setBackgroundColor(Color.Black.hashCode())
149+
} else {
150+
payPalMessage.setBackgroundColor(Color.White.hashCode())
151+
}
152+
153+
if ( stageTag?.text.toString().isNotBlank() ) {
154+
Api.stageTag = stageTag?.text.toString()
155+
} else {
156+
Api.stageTag = null
157+
}
158+
159+
payPalMessage.setStyle(PayPalMessageStyle(textAlign = alignment, color = color, logoType = logoType))
160+
payPalMessage.refresh()
161+
}
162+
163+
// Restore default options
164+
val resetButton = findViewById<Button>(R.id.reset)
165+
resetButton.setOnClickListener {
166+
// Reset UI
167+
logoTypeRadioGroup.check(R.id.stylePrimary)
168+
colorRadioGroup.check(R.id.styleBlack)
169+
alignmentRadioGroup.check(R.id.styleLeft)
170+
updateOfferUi(null, false)
171+
ignoreCache.isChecked = false
172+
devTouchpoint.isChecked = false
173+
amount.setText("")
174+
buyerCountry.setText("")
175+
176+
updateMessageData()
177+
}
178+
179+
// Request message based on options
180+
val submitButton = findViewById<Button>(R.id.submit)
181+
submitButton.setOnClickListener {
182+
updateMessageData()
183+
}
184+
185+
// TODO add example of adding MessageView here instead of in XML
36186
payPalMessage.setViewStates(
37187
PayPalMessageViewState(
38188
onLoading = {
39189
Log.d(TAG, "onLoading")
40190
progressBar.visibility = View.VISIBLE
41-
reloadButton.isEnabled = false
191+
resetButton.isEnabled = false
192+
submitButton.isEnabled = false
42193
Toast.makeText(this, "Loading Content...", Toast.LENGTH_SHORT).show()
43194
},
44195
onError = {
45196
Log.d(TAG, "onError")
46197
progressBar.visibility = View.INVISIBLE
47198
runOnUiThread {
48-
reloadButton.isEnabled = true
199+
resetButton.isEnabled = true
200+
submitButton.isEnabled = true
49201
Toast.makeText(this, it.javaClass.toString() + ":" + it.message + ":" + it.paypalDebugId, Toast.LENGTH_LONG).show()
50202
}
51203
it.message?.let { it1 -> Log.d("XmlActivity Error", it1) }
@@ -55,41 +207,13 @@ class XmlActivity : AppCompatActivity() {
55207
Log.d(TAG, "onSuccess")
56208
progressBar.visibility = View.INVISIBLE
57209
runOnUiThread {
58-
reloadButton.isEnabled = true
210+
resetButton.isEnabled = true
211+
submitButton.isEnabled = true
59212
Toast.makeText(this, "Success Getting Content", Toast.LENGTH_SHORT).show()
60213
}
61214
},
62215
),
63216
)
64217
}
65218

66-
/**
67-
* Prevents unused warnings inside of [PayPalMessageView] and [PayPalMessageConfig]
68-
*/
69-
@Suppress("unused")
70-
fun useUnusedFunctions() {
71-
binding = ActivityMessageBinding.inflate(layoutInflater)
72-
setContentView(binding.root)
73-
74-
val message = binding.payPalMessage
75-
val config = PayPalMessageConfig()
76-
config.setGlobalAnalytics("", "")
77-
message.setConfig(config)
78-
79-
message.setData(PayPalMessageData())
80-
message.setClientId(EnvVars.getClientId(Environment.LIVE))
81-
message.setAmount(1.0)
82-
message.setPlacement("placement")
83-
message.setOfferType(PayPalMessageOfferType.PAY_LATER_SHORT_TERM)
84-
message.setBuyerCountry("country")
85-
86-
message.setActionEventCallbacks(PayPalMessageEvents())
87-
88-
message.setViewStateCallbacks(PayPalMessageViewState())
89-
90-
message.setStyle(PayPalMessageStyle())
91-
message.setColor(PayPalMessageColor.BLACK)
92-
message.setLogoType(PayPalMessageLogoType.PRIMARY)
93-
message.setTextAlignment(PayPalMessageAlign.CENTER)
94-
}
95219
}

0 commit comments

Comments
 (0)