Fix warning 1.5: String.capitalize is now deprecated

This commit is contained in:
Benoit Marty 2021-05-14 12:45:33 +02:00
parent 0354151fa7
commit 58a2fd8c77
2 changed files with 14 additions and 1 deletions

View file

@ -20,6 +20,7 @@ import android.annotation.SuppressLint
import com.squareup.moshi.Json import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass import com.squareup.moshi.JsonClass
import org.matrix.android.sdk.api.util.JsonDict import org.matrix.android.sdk.api.util.JsonDict
import org.matrix.android.sdk.internal.util.safeCapitalize
/** /**
* Ref: https://github.com/matrix-org/matrix-doc/issues/1236 * Ref: https://github.com/matrix-org/matrix-doc/issues/1236
@ -39,6 +40,6 @@ data class WidgetContent(
@SuppressLint("DefaultLocale") @SuppressLint("DefaultLocale")
fun getHumanName(): String { fun getHumanName(): String {
return (name ?: type ?: "").capitalize() return (name ?: type ?: "").safeCapitalize()
} }
} }

View file

@ -17,6 +17,7 @@
package org.matrix.android.sdk.internal.util package org.matrix.android.sdk.internal.util
import timber.log.Timber import timber.log.Timber
import java.util.Locale
/** /**
* Convert a string to an UTF8 String * Convert a string to an UTF8 String
@ -78,3 +79,14 @@ internal val spaceChars = "[\u00A0\u2000-\u200B\u2800\u3000]".toRegex()
* Strip all the UTF-8 chars which are actually spaces * Strip all the UTF-8 chars which are actually spaces
*/ */
internal fun String.replaceSpaceChars() = replace(spaceChars, "") internal fun String.replaceSpaceChars() = replace(spaceChars, "")
// String.capitalize is now deprecated
internal fun String.safeCapitalize(): String {
return replaceFirstChar { char ->
if (char.isLowerCase()) {
char.titlecase(Locale.getDefault())
} else {
char.toString()
}
}
}