-
Notifications
You must be signed in to change notification settings - Fork 68
refactor: Migrate Internal Config class to kotlin #538
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
base: blackout-2024
Are you sure you want to change the base?
refactor: Migrate Internal Config class to kotlin #538
Conversation
35ae7c8 to
38adc3c
Compare
5de2f41 to
3a5292e
Compare
einsteinx2
left a comment
There was a problem hiding this 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
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.
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?
There was a problem hiding this comment.
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
BrandonStalnaker
left a comment
There was a problem hiding this 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
The base branch was changed.
thomson-t
left a comment
There was a problem hiding this 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? |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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()
}| 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.
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.
Instructions
developmentSummary
Testing Plan
Reference Issue (For mParticle employees only. Ignore if you are an outside contributor)