Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 37 additions & 6 deletions textvalue/src/main/kotlin/TextValue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.view.View
import androidx.annotation.StringRes
import androidx.compose.runtime.Immutable
import kotlinx.parcelize.Parcelize
import kotlinx.parcelize.RawValue

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

/** String resource, requires [Resources] to get [String]. */
@Parcelize
public data class Resource(@StringRes public val resourceId: Int) : TextValue {
override fun get(resources: Resources): String = resources.getString(resourceId)
public data class Resource(
@StringRes public val resourceId: Int,
public val formatArgs: @RawValue Array<out Any>
) : TextValue {
@Suppress("SpreadOperator")
override fun get(resources: Resources): String {
return resources.getString(resourceId, *formatArgs)
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as Resource

if (resourceId != other.resourceId) return false
if (!formatArgs.contentEquals(other.formatArgs)) return false

return true
}

override fun hashCode(): Int {
var result = resourceId
result = 31 * result + formatArgs.contentHashCode()
return result
}
}

public companion object {
Expand All @@ -48,15 +73,21 @@ public sealed interface TextValue : Parcelable {
}
}

/** Creates [TextValue] from the given [resourceId]. */
public fun TextValue(@StringRes resourceId: Int): TextValue = TextValue.Resource(resourceId)
/** Creates [TextValue] from the given [resourceId] and [formatArgs]. */
public fun TextValue(@StringRes resourceId: Int, vararg formatArgs: Any): TextValue {
return TextValue.Resource(resourceId, formatArgs)
}

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

/** Creates [TextValue] from the given [string], or from the [defaultResourceId] if string is `null`. */
public fun TextValue(string: String?, @StringRes defaultResourceId: Int): TextValue {
return if (string != null) TextValue.Plain(string) else TextValue.Resource(defaultResourceId)
public fun TextValue(string: String?, @StringRes defaultResourceId: Int, vararg formatArgs: Any): TextValue {
return if (string != null) {
TextValue.Plain(string)
} else {
TextValue.Resource(defaultResourceId, formatArgs)
}
}

/**
Expand Down
Loading