Skip to content

Commit

Permalink
fix: allow messages to be updated any time (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinpaypal committed Apr 19, 2024
1 parent dc8dd61 commit ef69ce9
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 38 deletions.
2 changes: 1 addition & 1 deletion demo/src/main/java/com/paypal/messagesdemo/XmlActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class XmlActivity : AppCompatActivity() {
binding = ActivityMessageBinding.inflate(layoutInflater)
setContentView(binding.root)

PayPalMessageConfig.setGlobalAnalytics()
PayPalMessageConfig.setGlobalAnalytics("", "")
val config = PayPalMessageConfig(data = PayPalMessageData(clientID = "someClientID"))
val message = PayPalMessageView(context = this, config = config)
message.getConfig()
Expand Down
31 changes: 12 additions & 19 deletions library/src/main/java/com/paypal/messages/PayPalMessageView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ class PayPalMessageView @JvmOverloads constructor(
) : FrameLayout(context, attributeSet, defStyleAttr), OnActionCompleted {
private val TAG = "PayPalMessage"
private var messageTextView: TextView
private var updateInProgress = false
private var instanceId = UUID.randomUUID()

fun getConfig(): MessageConfig {
Expand Down Expand Up @@ -441,23 +440,18 @@ class PayPalMessageView @JvmOverloads constructor(
* This function updates message content uses [Api.getMessageWithHash] to fetch the data.
*/
private fun updateMessageContent() {
LogCat.debug(TAG, "updateMessageContent config: ${getConfig()}")
if (!updateInProgress) {
// Call OnLoading callback and prepare view for the process
onLoading.invoke()
messageTextView.visibility = View.GONE
updateInProgress = true
LogCat.debug(TAG, "Firing request to get message")

requestDuration = measureTimeMillis {
Api.getMessageWithHash(
context,
getConfig(),
this.instanceId,
this,
)
}.toInt()
}
// Call OnLoading callback and prepare view for the process
onLoading.invoke()
LogCat.debug(TAG, "Firing request to get message with config: ${getConfig()}")

requestDuration = measureTimeMillis {
Api.getMessageWithHash(
context,
getConfig(),
this.instanceId,
this,
)
}.toInt()
}

override fun onActionCompleted(result: ApiResult) {
Expand Down Expand Up @@ -487,7 +481,6 @@ class PayPalMessageView @JvmOverloads constructor(
result.error?.let { this.onError(it) }
}
}
updateInProgress = false
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.paypal.messages.analytics

import android.content.Context
import com.paypal.messages.config.GlobalAnalytics
import com.paypal.messages.io.Api
import com.paypal.messages.io.LocalStorage
import com.paypal.messages.utils.LogCat
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.paypal.messages.config
package com.paypal.messages.analytics

object GlobalAnalytics {
var integrationName: String = ""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.paypal.messages.config.message

import com.paypal.messages.config.GlobalAnalytics
import com.paypal.messages.analytics.GlobalAnalytics

/**
* [PayPalMessageConfig] is the main configuration model for interacting with the PayPalMessage component
Expand All @@ -19,8 +19,8 @@ data class PayPalMessageConfig(

companion object {
fun setGlobalAnalytics(
integrationName: String = "",
integrationVersion: String = "",
integrationName: String,
integrationVersion: String,
deviceId: String = "",
sessionId: String = "",
) {
Expand Down
9 changes: 7 additions & 2 deletions library/src/main/java/com/paypal/messages/io/Api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ object Api {
return ApiResult.getFailureWithDebugId(response.headers)
}

LogCat.debug(TAG, "callMessageHashEndpoint response: $bodyJson")
LogCat.debug(
TAG,
"callMessageHashEndpoint response: ${bodyJson?.let { JSONObject(it).toString(2) }}}",
)

val hashResponse = gson.fromJson(bodyJson, ApiHashData.Response::class.java)
return ApiResult.Success(hashResponse)
Expand Down Expand Up @@ -221,12 +224,14 @@ object Api {
}

internal fun createLoggerRequest(json: String): Request {
LogCat.debug(TAG, "json after removal: ${JSONObject(json).toString(2)}")
val request = Request.Builder().apply {
url(env.url(Env.Endpoints.LOGGER))
post(json.toRequestBody("application/json".toMediaType()))
}.build()

val jsonNoFdata = json.replace(""""fdata":".*?"""".toRegex(), "")
val jsonNoFdata = JSONObject(json).toString(2)
.replace(""""fdata":.*?",""".toRegex(), "")
LogCat.debug(TAG, "createLoggerRequest: $request\npayloadJson: $jsonNoFdata")
return request
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.paypal.messages.config.message
package com.paypal.messages.utils

@Retention(AnnotationRetention.RUNTIME)
@Target(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.paypal.messages.config
package com.paypal.messages.analytics

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.paypal.messages.config.message

import com.paypal.messages.config.GlobalAnalytics
import com.paypal.messages.analytics.GlobalAnalytics
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotEquals
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -50,7 +50,7 @@ class PayPalMessageConfigTest {
// It is also required for full test coverage as calculated by kover
@Test
fun testSetGlobalAnalyticsWithNoValues() {
PayPalMessageConfig.setGlobalAnalytics()
PayPalMessageConfig.setGlobalAnalytics("", "")

assertEquals("", GlobalAnalytics.integrationName)
assertEquals("", GlobalAnalytics.integrationVersion)
Expand Down
10 changes: 3 additions & 7 deletions scripts/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,14 @@ if [[ "$stagedFilesCount" -eq "0" ]]; then
fi

echo "Found $stagedFilesCount staged files."
echo git diff --staged --name-only | grep -E '(library|demo)'
git diff --staged --name-only | grep -E '(library|demo)'
echo ""
echo "Running ktFormat to fix any auto-fixable issues"

git stash --keep-index --quiet

./gradlew ktFormat

git add library/
git add demo/

git stash pop --quiet
# Only re-add staged files that are in library/ or demo/ folders
git diff --staged --name-only | grep -E '(library|demo)' | xargs git add

echo "***********************************************"
echo "Added changed files to commit"
Expand Down

0 comments on commit ef69ce9

Please sign in to comment.