From 58a2fd8c77b0662124ed20edf883d1a15c9fe451 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 14 May 2021 12:45:33 +0200 Subject: [PATCH] Fix warning 1.5: String.capitalize is now deprecated --- .../sdk/api/session/widgets/model/WidgetContent.kt | 3 ++- .../matrix/android/sdk/internal/util/StringUtils.kt | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/widgets/model/WidgetContent.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/widgets/model/WidgetContent.kt index 2c4c03b7d4..7a4231c277 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/widgets/model/WidgetContent.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/widgets/model/WidgetContent.kt @@ -20,6 +20,7 @@ import android.annotation.SuppressLint import com.squareup.moshi.Json import com.squareup.moshi.JsonClass 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 @@ -39,6 +40,6 @@ data class WidgetContent( @SuppressLint("DefaultLocale") fun getHumanName(): String { - return (name ?: type ?: "").capitalize() + return (name ?: type ?: "").safeCapitalize() } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/util/StringUtils.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/util/StringUtils.kt index 2fabca4be8..05fb732001 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/util/StringUtils.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/util/StringUtils.kt @@ -17,6 +17,7 @@ package org.matrix.android.sdk.internal.util import timber.log.Timber +import java.util.Locale /** * 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 */ 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() + } + } +}