Nathan Retta
11/18/2020, 6:22 AMinline fun <reified T> HashMap<String, T>.toAnalyticsBundle(): Bundle {
val analyticsRegex = Regex(FirebaseAnalyticsManager.ANALYTICS_REGEX_STRING)
return Bundle().apply {
for (key in keys) {
val value = this@toAnalyticsBundle[key]
val firebaseKey = FirebaseAnalyticsManager.googleAnalyticsifyString(key, analyticsRegex)
when (value) {
is Int -> this@apply.putInt(firebaseKey, value)
is Float -> this@apply.putFloat(firebaseKey, value)
is Double -> this@apply.putDouble(firebaseKey, value)
is Boolean -> this@apply.putBoolean(firebaseKey, value)
is Char -> this@apply.putChar(firebaseKey, value)
is String -> this@apply.putString(firebaseKey, value)
is Parcelable -> this@apply.putParcelable(firebaseKey, value)
is Serializable -> this@apply.putSerializable(firebaseKey, value)
}
}
}
}
fun googleAnalyticsifyString(stringToConvert: String, regex: Regex = Regex(ANALYTICS_REGEX_STRING)): String {
val charArray = stringToConvert.toLowerCase().replace(regex, "").toCharArray()
for((index, char) in charArray.withIndex()) {
if(char == ' ') {
val nextCharIndex = index + 1
if(nextCharIndex < stringToConvert.length) {
charArray[nextCharIndex] = charArray[nextCharIndex].toUpperCase()
}
}
}
return charArray
.joinToString("")
.replace(" ", "")
}
georg
11/18/2020, 10:29 AM