refactor: Migrate Internal Config class to kotlin#538
refactor: Migrate Internal Config class to kotlin#538Mansi-mParticle wants to merge 2 commits intoblackout-2024from
Conversation
35ae7c8 to
38adc3c
Compare
5de2f41 to
3a5292e
Compare
einsteinx2
left a comment
There was a problem hiding this comment.
Couple of questions, otherwise looks good
android-core/src/main/kotlin/com/mparticle/internal/ConfigManager.kt
Outdated
Show resolved
Hide resolved
| open val apiKey: String | ||
| get() = sPreferences?.getString(Constants.PrefKeys.API_KEY, null) ?: "" | ||
|
|
||
| open val apiSecret: String | ||
| get() = sPreferences?.getString(Constants.PrefKeys.API_SECRET, null) ?: "" |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
it is returning null it breaking lot's of existing code and test cases so decide to keep this as non null
BrandonStalnaker
left a comment
There was a problem hiding this comment.
LGTM but would appreciate another set of eyes to take a look
The base branch was changed.
thomson-t
left a comment
There was a problem hiding this comment.
Do we want to merge the blackout-2024 branch first and then this one to main?
| } | ||
|
|
||
| @get:WorkerThread | ||
| open val latestKitConfiguration: JSONArray? |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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()
}| 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 |
There was a problem hiding this comment.
This is probably the direct translation from Java to Kotlin, not an optimised Kotlin code. We can refactor it later.
Instructions
developmentSummary
Testing Plan
Reference Issue (For mParticle employees only. Ignore if you are an outside contributor)