Skip to content

Commit

Permalink
feat: xml demo (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
grablack authored Oct 18, 2023
1 parent 1550f44 commit 4b33504
Show file tree
Hide file tree
Showing 6 changed files with 433 additions and 113 deletions.
3 changes: 2 additions & 1 deletion demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ dependencies {
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation project(':library')
implementation 'com.google.android.material:material:1.10.0'

testImplementation 'junit:junit:4.13.2'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation platform('androidx.compose:compose-bom:2023.09.00')
Expand Down
202 changes: 163 additions & 39 deletions demo/src/main/java/com/paypal/messagesdemo/XmlActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,30 @@ package com.paypal.messagesdemo
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.RadioGroup
import android.widget.Toast
import android.widget.ToggleButton
import androidx.appcompat.app.AppCompatActivity
import com.paypal.messages.PayPalMessageView
import androidx.compose.ui.graphics.Color
import com.google.android.material.switchmaterial.SwitchMaterial
import com.paypal.messages.config.PayPalMessageOfferType
import com.paypal.messages.config.message.PayPalMessageConfig
import com.paypal.messages.config.message.PayPalMessageData
import com.paypal.messages.config.message.PayPalMessageEvents
import com.paypal.messages.config.message.PayPalMessageStyle
import com.paypal.messages.config.message.PayPalMessageViewState
import com.paypal.messages.config.message.style.PayPalMessageAlign
import com.paypal.messages.config.message.style.PayPalMessageColor
import com.paypal.messages.config.message.style.PayPalMessageLogoType
import com.paypal.messages.io.Api
import com.paypal.messagesdemo.databinding.ActivityMessageBinding
import com.paypal.messages.config.PayPalEnvironment as Environment

class XmlActivity : AppCompatActivity() {
private lateinit var binding: ActivityMessageBinding
private val TAG = "XmlActivity"
private var logoType: PayPalMessageLogoType = PayPalMessageLogoType.PRIMARY
private var color: PayPalMessageColor = PayPalMessageColor.BLACK
private var alignment: PayPalMessageAlign = PayPalMessageAlign.LEFT
private var offerType: PayPalMessageOfferType? = null

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

val payPalMessage = binding.payPalMessage
val progressBar = binding.progressBar
val reloadButton = binding.reloadButton

// TODO add example of adding MessageView here instead of in XML
val editedClientId: EditText? = findViewById<EditText>(R.id.clientId)

val logoTypeRadioGroup = findViewById<RadioGroup>(R.id.logoTypeRadioGroup)
logoTypeRadioGroup.setOnCheckedChangeListener { _, checkedId ->
logoType = when (checkedId) {
R.id.stylePrimary -> PayPalMessageLogoType.PRIMARY
R.id.styleInline -> PayPalMessageLogoType.INLINE
R.id.styleAlternative -> PayPalMessageLogoType.ALTERNATIVE
R.id.styleNone -> PayPalMessageLogoType.NONE
else -> PayPalMessageLogoType.PRIMARY
}
}

val colorRadioGroup = findViewById<RadioGroup>(R.id.colorRadioGroup)
colorRadioGroup.setOnCheckedChangeListener { _, checkedId ->
color = when (checkedId) {
R.id.styleBlack -> PayPalMessageColor.BLACK
R.id.styleWhite -> PayPalMessageColor.WHITE
R.id.styleMonochrome -> PayPalMessageColor.MONOCHROME
R.id.styleGrayscale -> PayPalMessageColor.GRAYSCALE
else -> PayPalMessageColor.BLACK
}
}

val alignmentRadioGroup = findViewById<RadioGroup>(R.id.alignmentRadioGroup)
alignmentRadioGroup.setOnCheckedChangeListener { _, checkedId ->
alignment = when (checkedId) {
R.id.styleLeft -> PayPalMessageAlign.LEFT
R.id.styleCenter -> PayPalMessageAlign.CENTER
R.id.styleRight -> PayPalMessageAlign.RIGHT
else -> PayPalMessageAlign.LEFT
}
}

val shortTerm = findViewById<ToggleButton>(R.id.shortTerm)
val longTerm = findViewById<ToggleButton>(R.id.longTerm)
val payIn1 = findViewById<ToggleButton>(R.id.payIn1)
val credit = findViewById<ToggleButton>(R.id.credit)

fun updateOfferUi (offerName: PayPalMessageOfferType?, isChecked: Boolean) {
shortTerm.isChecked = false
longTerm.isChecked = false
payIn1.isChecked = false
credit.isChecked = false
offerType = null

if ( offerName == PayPalMessageOfferType.PAY_LATER_SHORT_TERM && isChecked) {
shortTerm.isChecked = true
offerType = PayPalMessageOfferType.PAY_LATER_SHORT_TERM
} else if ( offerName == PayPalMessageOfferType.PAY_LATER_LONG_TERM && isChecked) {
longTerm.isChecked = true
offerType = PayPalMessageOfferType.PAY_LATER_LONG_TERM
} else if ( offerName == PayPalMessageOfferType.PAY_LATER_PAY_IN_1 && isChecked) {
payIn1.isChecked = true
offerType = PayPalMessageOfferType.PAY_LATER_PAY_IN_1
} else if ( offerName == PayPalMessageOfferType.PAYPAL_CREDIT_NO_INTEREST && isChecked) {
credit.isChecked = true
offerType = PayPalMessageOfferType.PAYPAL_CREDIT_NO_INTEREST
}

payPalMessage.setOfferType(offerType = offerType)

}

shortTerm.setOnCheckedChangeListener { _, isChecked ->
updateOfferUi(PayPalMessageOfferType.PAY_LATER_SHORT_TERM, isChecked)
}
longTerm.setOnCheckedChangeListener { _, isChecked ->
updateOfferUi(PayPalMessageOfferType.PAY_LATER_LONG_TERM, isChecked)
}
payIn1.setOnCheckedChangeListener { _, isChecked ->
updateOfferUi(PayPalMessageOfferType.PAY_LATER_PAY_IN_1, isChecked)
}
credit.setOnCheckedChangeListener { _, isChecked ->
updateOfferUi(PayPalMessageOfferType.PAYPAL_CREDIT_NO_INTEREST, isChecked)
}

val amount = findViewById<EditText>(R.id.amount)
val buyerCountry = findViewById<EditText>(R.id.buyerCountry)
val stageTag = findViewById<EditText>(R.id.stageTag)


val ignoreCache = findViewById<SwitchMaterial>(R.id.ignoreCache)
val devTouchpoint = findViewById<SwitchMaterial>(R.id.devTouchpoint)

// Get the data from the selected options
fun updateMessageData() {
Api.devTouchpoint = devTouchpoint.isChecked
Api.ignoreCache = ignoreCache.isChecked

if ( editedClientId?.text.toString().isNotBlank() ) {
payPalMessage.setClientId(editedClientId?.text.toString())
} else {
payPalMessage.setClientId("")
}

if ( amount?.text.toString().isNotBlank() ) {
payPalMessage.setAmount(amount?.text.toString().toDouble())
} else {
payPalMessage.setAmount(null)
}

if ( buyerCountry?.text.toString().isNotBlank() ) {
payPalMessage.setBuyerCountry(buyerCountry?.text.toString())
} else {
payPalMessage.setBuyerCountry("US")
}

if ( color === PayPalMessageColor.WHITE ) {
payPalMessage.setBackgroundColor(Color.Black.hashCode())
} else {
payPalMessage.setBackgroundColor(Color.White.hashCode())
}

if ( stageTag?.text.toString().isNotBlank() ) {
Api.stageTag = stageTag?.text.toString()
} else {
Api.stageTag = null
}

payPalMessage.setStyle(PayPalMessageStyle(textAlign = alignment, color = color, logoType = logoType))
payPalMessage.refresh()
}

// Restore default options
val resetButton = findViewById<Button>(R.id.reset)
resetButton.setOnClickListener {
// Reset UI
logoTypeRadioGroup.check(R.id.stylePrimary)
colorRadioGroup.check(R.id.styleBlack)
alignmentRadioGroup.check(R.id.styleLeft)
updateOfferUi(null, false)
ignoreCache.isChecked = false
devTouchpoint.isChecked = false
amount.setText("")
buyerCountry.setText("")

updateMessageData()
}

// Request message based on options
val submitButton = findViewById<Button>(R.id.submit)
submitButton.setOnClickListener {
updateMessageData()
}

// TODO add example of adding MessageView here instead of in XML
payPalMessage.setViewStates(
PayPalMessageViewState(
onLoading = {
Log.d(TAG, "onLoading")
progressBar.visibility = View.VISIBLE
reloadButton.isEnabled = false
resetButton.isEnabled = false
submitButton.isEnabled = false
Toast.makeText(this, "Loading Content...", Toast.LENGTH_SHORT).show()
},
onError = {
Log.d(TAG, "onError")
progressBar.visibility = View.INVISIBLE
runOnUiThread {
reloadButton.isEnabled = true
resetButton.isEnabled = true
submitButton.isEnabled = true
Toast.makeText(this, it.javaClass.toString() + ":" + it.message + ":" + it.paypalDebugId, Toast.LENGTH_LONG).show()
}
it.message?.let { it1 -> Log.d("XmlActivity Error", it1) }
Expand All @@ -55,41 +207,13 @@ class XmlActivity : AppCompatActivity() {
Log.d(TAG, "onSuccess")
progressBar.visibility = View.INVISIBLE
runOnUiThread {
reloadButton.isEnabled = true
resetButton.isEnabled = true
submitButton.isEnabled = true
Toast.makeText(this, "Success Getting Content", Toast.LENGTH_SHORT).show()
}
},
),
)
}

/**
* Prevents unused warnings inside of [PayPalMessageView] and [PayPalMessageConfig]
*/
@Suppress("unused")
fun useUnusedFunctions() {
binding = ActivityMessageBinding.inflate(layoutInflater)
setContentView(binding.root)

val message = binding.payPalMessage
val config = PayPalMessageConfig()
config.setGlobalAnalytics("", "")
message.setConfig(config)

message.setData(PayPalMessageData())
message.setClientId(EnvVars.getClientId(Environment.LIVE))
message.setAmount(1.0)
message.setPlacement("placement")
message.setOfferType(PayPalMessageOfferType.PAY_LATER_SHORT_TERM)
message.setBuyerCountry("country")

message.setActionEventCallbacks(PayPalMessageEvents())

message.setViewStateCallbacks(PayPalMessageViewState())

message.setStyle(PayPalMessageStyle())
message.setColor(PayPalMessageColor.BLACK)
message.setLogoType(PayPalMessageLogoType.PRIMARY)
message.setTextAlignment(PayPalMessageAlign.CENTER)
}
}
Loading

0 comments on commit 4b33504

Please sign in to comment.