Skip to content

Conversation

@Mansi-mParticle
Copy link
Collaborator

Instructions

  1. PR target branch should be against development
  2. PR title name should follow this format: https://github.com/mParticle/mparticle-workflows/blob/main/.github/workflows/pr-title-check.yml
  3. PR branch prefix should follow this format: https://github.com/mParticle/mparticle-workflows/blob/main/.github/workflows/pr-branch-check-name.yml

Summary

  • Migrate internal package’s MPUtility classe to Kotlin.

Testing Plan

  • Was this tested locally? If not, explain why.
  • Tested with sample application and ran the unit test cases

Reference Issue (For mParticle employees only. Ignore if you are an outside contributor)

@Mansi-mParticle Mansi-mParticle force-pushed the refactor/SQDSDKS-6708-Configmanager-class branch from 5de2f41 to 3a5292e Compare February 5, 2025 21:51
Copy link
Contributor

@einsteinx2 einsteinx2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of questions, otherwise looks good

Comment on lines +524 to +528
open val apiKey: String
get() = sPreferences?.getString(Constants.PrefKeys.API_KEY, null) ?: ""

open val apiSecret: String
get() = sPreferences?.getString(Constants.PrefKeys.API_SECRET, null) ?: ""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice in the original Java we return null for both of these if it's not stored. Should these properties be nullable and return null instead of an empty string? Are there any null checks in other places that assume this can return null?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is returning null it breaking lot's of existing code and test cases so decide to keep this as non null

@Mansi-mParticle Mansi-mParticle requested review from BrandonStalnaker, mmustafa-tse and rmi22186 and removed request for einsteinx2 March 3, 2025 17:43
Copy link
Contributor

@BrandonStalnaker BrandonStalnaker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but would appreciate another set of eyes to take a look

@rmi22186 rmi22186 changed the base branch from blackout-2024 to main January 22, 2026 15:19
@rmi22186 rmi22186 dismissed BrandonStalnaker’s stale review January 22, 2026 15:19

The base branch was changed.

@rmi22186 rmi22186 requested a review from a team as a code owner January 22, 2026 15:19
@rmi22186 rmi22186 changed the base branch from main to blackout-2024 January 22, 2026 15:19
Copy link
Collaborator

@thomson-t thomson-t left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to merge the blackout-2024 branch first and then this one to main?

}

@get:WorkerThread
open val latestKitConfiguration: JSONArray?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we keep it as a function, considering the read operation and side effects?

?.putLong(CONFIG_JSON_TIMESTAMP, if (timestamp != null) timestamp else System.currentTimeMillis())
?.putString(Constants.PrefKeys.ETAG, etag)
?.putString(Constants.PrefKeys.IF_MODIFIED, lastModified)
?.apply()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not the ideal way in Kotlin. If we include androidx.core:core-ktx, we could use it like

sPreferences?.edit {
                putString(CONFIG_JSON, coreConfig.toString())
                putLong(CONFIG_JSON_TIMESTAMP, if (timestamp != null) timestamp else System.currentTimeMillis())
                putString(Constants.PrefKeys.ETAG, etag)
                putString(Constants.PrefKeys.IF_MODIFIED, lastModified)
            }

Even without that dependency, we can use kotlin features to avoid chained nullable checks, like

sPreferences?.edit()?.apply {
                putString(CONFIG_JSON, coreConfig.toString())
                putLong(CONFIG_JSON_TIMESTAMP, if (timestamp != null) timestamp else System.currentTimeMillis())
                putString(Constants.PrefKeys.ETAG, etag)
                putString(Constants.PrefKeys.IF_MODIFIED, lastModified)
                apply()
            }

Comment on lines +780 to +836
fun shouldTrigger(message: BaseMPMessage): Boolean {
val messageMatches: JSONArray? = triggerMessageMatches
val triggerHashes: JSONArray? = triggerMessageHashes

var isBackgroundAst: Boolean = false
try {
isBackgroundAst =
(message.messageType == Constants.MessageType.APP_STATE_TRANSITION && message.get(Constants.MessageKey.STATE_TRANSITION_TYPE) == Constants.StateTransitionType.STATE_TRANS_BG)
} catch (ex: JSONException) {
}
var shouldTrigger: Boolean = message.messageType == Constants.MessageType.PUSH_RECEIVED ||
message.messageType == Constants.MessageType.COMMERCE_EVENT ||
isBackgroundAst

if (!shouldTrigger && messageMatches != null && messageMatches.length() > 0) {
shouldTrigger = true
var i: Int = 0
while (shouldTrigger && i < messageMatches.length()) {
try {
val messageMatch: JSONObject = messageMatches.getJSONObject(i)
val keys: Iterator<*> = messageMatch.keys()
while (shouldTrigger && keys.hasNext()) {
val key: String = keys.next() as String
shouldTrigger = message.has(key)
if (shouldTrigger) {
try {
shouldTrigger = messageMatch.getString(key).equals(message.getString(key), ignoreCase = true)
} catch (stringex: JSONException) {
try {
shouldTrigger = message.getBoolean(key) == messageMatch.getBoolean(key)
} catch (boolex: JSONException) {
try {
shouldTrigger = message.getDouble(key) == messageMatch.getDouble(key)
} catch (doubleex: JSONException) {
shouldTrigger = false
}
}
}
}
}
} catch (e: Exception) {
}
i++
}
}
if (!shouldTrigger && triggerHashes != null) {
for (i in 0 until triggerHashes.length()) {
try {
if (triggerHashes.getInt(i) == message.typeNameHash) {
shouldTrigger = true
break
}
} catch (jse: JSONException) {
}
}
}
return shouldTrigger
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably the direct translation from Java to Kotlin, not an optimised Kotlin code. We can refactor it later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants