Skip to content

RedMadRobot/TextValue

Repository files navigation

TextValue

Version License

TextValue is an abstraction allowing to work with a String and a string resource ID the same way.


Installation

Add the dependency:

repositories {
    mavenCentral()
    google()
}

dependencies {
    // Views version
    implementation("com.redmadrobot.textvalue:textvalue:1.0.0")

    // Compose extensions for textvalue
    implementation("com.redmadrobot.textvalue:textvalue-compose:1.0.0")
}

Usage

TextValue is a wrapper to make it possible to work with plain String and StringRes in the same way. It may be useful for cases when you want to fallback to StringRes if desired string value is null.

You can wrap String and StringRes with TextValue using TextValue(String), TextValue(Int) or TextValue(String?, Int)), and use method TextValue.get(Resource) to retrieve String:

// in some place where we can't access Context
val errorMessage = TextValue(exception.message, defaultResourceId = R.string.unknown_error)
showMessage(errorMessage)

// in Activity, Fragment or View
fun showMessage(text: TextValue) {
    val messageText = text.get(resources)
    //...
}

TextValue also could be used with Jetpack Compose:

// in Composable functions
@Composable
fun Screen(title: TextValue) {
    // Remember to add com.redmadrobot.textvalue:textvalue-compose dependency
    Text(text = stringResource(title))
}

There are extensions to work with TextValue like with StringRes:

  • Context.getString(text: TextValue): String
  • View.getString(text: TextValue): String
  • Resources.getString(text: TextValue): String

Contributing

Merge requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

About

TextValue is an abstraction over Android text

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages