Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: modal tracking updates #51

Open
wants to merge 2 commits into
base: feat/modal
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions demo/src/main/java/com/paypal/messagesdemo/XmlActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ class XmlActivity : AppCompatActivity() {
val messagesModalText = binding.messagesModalText
val modalConfig = PayPalMessagesModalConfig(
clientID = getString(R.string.client_id),
amount = 300.0,
offerType = PayPalMessageOfferType.PAY_LATER_LONG_TERM,
environment = environment,
)
val modal = PayPalMessagesModalFragment(context = this, config = modalConfig)
messagesModalText.setOnClickListener {
Expand Down Expand Up @@ -183,6 +182,12 @@ class XmlActivity : AppCompatActivity() {
payPalMessage.color = color
payPalMessage.logoType = logoType
payPalMessage.textAlignment = textAlignment

modal.clientID = clientId
modal.amount = amount
modal.buyerCountry = buyerCountry
modal.offerType = offerType
modal.environment = environment
}

// Restore default options and reset UI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ import androidx.core.content.res.use
import com.paypal.messages.analytics.AnalyticsComponent
import com.paypal.messages.analytics.AnalyticsEvent
import com.paypal.messages.analytics.AnalyticsLogger
import com.paypal.messages.analytics.ComponentType
import com.paypal.messages.config.PayPalEnvironment
import com.paypal.messages.config.PayPalMessageOfferType
import com.paypal.messages.config.PayPalMessagePageType
import com.paypal.messages.config.modal.ModalCloseButton
import com.paypal.messages.config.modal.ModalConfig
import com.paypal.messages.config.modal.ModalEvents
import com.paypal.messages.io.Api
import com.paypal.messages.io.ApiResult
import com.paypal.messages.io.OnActionCompleted
import com.paypal.messages.utils.LogCat
import com.paypal.messages.utils.PayPalErrors
import kotlinx.coroutines.CoroutineScope
Expand All @@ -45,8 +44,8 @@ class PayPalMessagesModalFragment @JvmOverloads constructor(
attributeSet: AttributeSet? = null,
defStyleAttr: Int = 0,
config: MessagesModalConfig = MessagesModalConfig(clientID = ""),
) : FrameLayout(context, attributeSet, defStyleAttr), OnActionCompleted {
private val TAG = "PayPalMessagesModalView"
) : FrameLayout(context, attributeSet, defStyleAttr) {
private val TAG = "PayPalMessagesModalFragment"
private var instanceId = UUID.randomUUID()

fun getConfig(): MessagesModalConfig {
Expand Down Expand Up @@ -239,7 +238,7 @@ class PayPalMessagesModalFragment @JvmOverloads constructor(
private var modal: ModalFragment? = null

init {
context.obtainStyledAttributes(attributeSet, R.styleable.PayPalMessagesModalView).use { typedArray ->
context.obtainStyledAttributes(attributeSet, R.styleable.PayPalMessagesModalFragment).use { typedArray ->
updateFromAttributes(typedArray)
}
if (config.clientID === "") LogCat.error(TAG, "ClientID is an empty string")
Expand Down Expand Up @@ -296,69 +295,54 @@ class PayPalMessagesModalFragment @JvmOverloads constructor(
* This function will update the local config related values based on what is provided from the [PayPalMessageView] xml custom view.
*/
private fun updateFromAttributes(typedArray: TypedArray) {
if (typedArray.hasValue(R.styleable.PayPalMessagesModalView_paypal_client_id)) {
clientID = typedArray.getString(R.styleable.PayPalMessagesModalView_paypal_client_id).toString()
if (typedArray.hasValue(R.styleable.PayPalMessagesModalFragment_paypal_client_id)) {
clientID = typedArray.getString(R.styleable.PayPalMessagesModalFragment_paypal_client_id).toString()
}

if (typedArray.hasValue(R.styleable.PayPalMessagesModalView_paypal_amount)) {
if (typedArray.hasValue(R.styleable.PayPalMessagesModalFragment_paypal_amount)) {
amount = try {
typedArray.getFloatOrThrow(R.styleable.PayPalMessagesModalView_paypal_amount).toDouble()
typedArray.getFloatOrThrow(R.styleable.PayPalMessagesModalFragment_paypal_amount).toDouble()
}
catch (ex: Exception) {
LogCat.error(TAG, "Error parsing amount attribute")
null
}
}

if (typedArray.hasValue(R.styleable.PayPalMessagesModalView_paypal_offer_type)) {
if (typedArray.hasValue(R.styleable.PayPalMessagesModalFragment_paypal_offer_type)) {
offerType = try {
PayPalMessageOfferType(typedArray.getIntOrThrow(R.styleable.PayPalMessagesModalView_paypal_offer_type))
PayPalMessageOfferType(typedArray.getIntOrThrow(R.styleable.PayPalMessagesModalFragment_paypal_offer_type))
}
catch (ex: Exception) {
LogCat.error(TAG, "Error parsing offer_type attribute")
null
}
}

if (typedArray.hasValue(R.styleable.PayPalMessageView_paypal_page_type)) {
if (typedArray.hasValue(R.styleable.PayPalMessagesModalFragment_paypal_page_type)) {
pageType = try {
PayPalMessagePageType(typedArray.getIntOrThrow(R.styleable.PayPalMessageView_paypal_page_type))
PayPalMessagePageType(typedArray.getIntOrThrow(R.styleable.PayPalMessagesModalFragment_paypal_page_type))
}
catch (ex: Exception) {
LogCat.error(TAG, "Error parsing page_type attribute")
null
}
}

if (typedArray.hasValue(R.styleable.PayPalMessageView_paypal_buyer_country)) {
buyerCountry = typedArray.getString(R.styleable.PayPalMessageView_paypal_buyer_country)
if (typedArray.hasValue(R.styleable.PayPalMessagesModalFragment_paypal_buyer_country)) {
buyerCountry = typedArray.getString(R.styleable.PayPalMessagesModalFragment_paypal_buyer_country)
}
}

override fun onActionCompleted(result: ApiResult) {
when (result) {
is ApiResult.Success<*> -> {
LogCat.debug(TAG, "onActionCompleted Success")
}

is ApiResult.Failure<*> -> {
LogCat.debug(TAG, "onActionCompleted Failure")
// If we encountered a failure, we expect an exception to be returned.
result.error?.let { this.onError(it) }
}
}
}

@Suppress("unused")
private fun logEvent(event: AnalyticsEvent) {
val component = AnalyticsComponent(
offerType = this.offerType,
amount = this.amount.toString(),
pageType = this.pageType,
buyerCountryCode = this.buyerCountry,
instanceId = this.instanceId.toString(),
originatingInstanceId = Api.originatingInstanceId.toString(),
// TODO: what type to use
// type = ComponentType.MESSAGE.toString(),
type = ComponentType.MODAL.toString(),
componentEvents = mutableListOf(event),
)

Expand Down
2 changes: 1 addition & 1 deletion library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@

<attr name="paypal_buyer_country" />
</declare-styleable>
<declare-styleable name="PayPalMessagesModalView">
<declare-styleable name="PayPalMessagesModalFragment">
<attr name="paypal_client_id" />

<attr name="paypal_offer_type" />
Expand Down
Loading