Skip to content

Commit

Permalink
refactor: Migrate Internal messages package classes to kotlin (#521)
Browse files Browse the repository at this point in the history
* refactor: Migrate Internal messages package classes to kotlin

* add support for kotlin classes

* address review comments

* Refactor code to use functional style
  • Loading branch information
Mansi-mParticle authored Nov 25, 2024
1 parent 6a9204c commit 0a4d64b
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 240 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.mparticle.internal.messages

import android.location.Location
import com.mparticle.internal.Constants
import com.mparticle.internal.InternalSession
import com.mparticle.internal.MPUtility
import org.json.JSONException
import org.json.JSONObject

open class BaseMPMessage : JSONObject {
var mpId: Long = 0
private set

protected constructor()

constructor(builder: BaseMPMessageBuilder, session: InternalSession, location: Location?, mpId: Long) : super(builder, builder.keys) {
this.mpId = mpId
if (!has(Constants.MessageKey.TIMESTAMP)) {
put(Constants.MessageKey.TIMESTAMP, session.mLastEventTime)
}
if (Constants.MessageType.SESSION_START == builder.messageType) {
put(Constants.MessageKey.ID, session.mSessionID)
} else {
put(Constants.MessageKey.SESSION_ID, session.mSessionID)

if (session.mSessionStartTime > 0) {
put(Constants.MessageKey.SESSION_START_TIMESTAMP, session.mSessionStartTime)
}
}

if (!(Constants.MessageType.ERROR == builder.messageType &&
Constants.MessageType.OPT_OUT != builder.messageType)
) {
if (location != null) {
val locJSON = JSONObject()
locJSON.put(Constants.MessageKey.LATITUDE, location.latitude)
locJSON.put(Constants.MessageKey.LONGITUDE, location.longitude)
locJSON.put(Constants.MessageKey.ACCURACY, location.accuracy.toDouble())
put(Constants.MessageKey.LOCATION, locJSON)
}
}
}

val attributes: JSONObject?
get() = optJSONObject(Constants.MessageKey.ATTRIBUTES)

val timestamp: Long
get() {
try {
return getLong(Constants.MessageKey.TIMESTAMP)
} catch (_: JSONException) {
}
return 0
}

val sessionId: String
get() =
if (Constants.MessageType.SESSION_START == messageType) {
optString(Constants.MessageKey.ID, Constants.NO_SESSION_ID)
} else {
optString(Constants.MessageKey.SESSION_ID, Constants.NO_SESSION_ID)
}

val messageType: String
get() = optString(Constants.MessageKey.TYPE)

val typeNameHash: Int
get() = MPUtility.mpHash(messageType + name)


val name: String
get() = optString(Constants.MessageKey.NAME)

class Builder(messageType: String) : BaseMPMessageBuilder(messageType)
}
Loading

0 comments on commit 0a4d64b

Please sign in to comment.