Skip to content

Commit ddd7ca9

Browse files
committed
Support for arguments in string resources
1 parent 0f8b740 commit ddd7ca9

1 file changed

Lines changed: 37 additions & 6 deletions

File tree

textvalue/src/main/kotlin/TextValue.kt

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import android.view.View
77
import androidx.annotation.StringRes
88
import androidx.compose.runtime.Immutable
99
import kotlinx.parcelize.Parcelize
10+
import kotlinx.parcelize.RawValue
1011

1112
/**
1213
* Wrapper to make it possible to work with plain [String] and [StringRes] in the same way.
@@ -37,8 +38,32 @@ public sealed interface TextValue : Parcelable {
3738

3839
/** String resource, requires [Resources] to get [String]. */
3940
@Parcelize
40-
public data class Resource(@StringRes public val resourceId: Int) : TextValue {
41-
override fun get(resources: Resources): String = resources.getString(resourceId)
41+
public data class Resource(
42+
@StringRes public val resourceId: Int,
43+
public val formatArgs: @RawValue Array<out Any>
44+
) : TextValue {
45+
@Suppress("SpreadOperator")
46+
override fun get(resources: Resources): String {
47+
return resources.getString(resourceId, *formatArgs)
48+
}
49+
50+
override fun equals(other: Any?): Boolean {
51+
if (this === other) return true
52+
if (javaClass != other?.javaClass) return false
53+
54+
other as Resource
55+
56+
if (resourceId != other.resourceId) return false
57+
if (!formatArgs.contentEquals(other.formatArgs)) return false
58+
59+
return true
60+
}
61+
62+
override fun hashCode(): Int {
63+
var result = resourceId
64+
result = 31 * result + formatArgs.contentHashCode()
65+
return result
66+
}
4267
}
4368

4469
public companion object {
@@ -48,15 +73,21 @@ public sealed interface TextValue : Parcelable {
4873
}
4974
}
5075

51-
/** Creates [TextValue] from the given [resourceId]. */
52-
public fun TextValue(@StringRes resourceId: Int): TextValue = TextValue.Resource(resourceId)
76+
/** Creates [TextValue] from the given [resourceId] and [formatArgs]. */
77+
public fun TextValue(@StringRes resourceId: Int, vararg formatArgs: Any): TextValue {
78+
return TextValue.Resource(resourceId, formatArgs)
79+
}
5380

5481
/** Creates [TextValue] from the given [string]. */
5582
public fun TextValue(string: String): TextValue = TextValue.Plain(string)
5683

5784
/** Creates [TextValue] from the given [string], or from the [defaultResourceId] if string is `null`. */
58-
public fun TextValue(string: String?, @StringRes defaultResourceId: Int): TextValue {
59-
return if (string != null) TextValue.Plain(string) else TextValue.Resource(defaultResourceId)
85+
public fun TextValue(string: String?, @StringRes defaultResourceId: Int, vararg formatArgs: Any): TextValue {
86+
return if (string != null) {
87+
TextValue.Plain(string)
88+
} else {
89+
TextValue.Resource(defaultResourceId, formatArgs)
90+
}
6091
}
6192

6293
/**

0 commit comments

Comments
 (0)