https://github.com/redmadrobot/textvalue
TextValue is an abstraction over Android text
https://github.com/redmadrobot/textvalue
Last synced: over 1 year ago
JSON representation
TextValue is an abstraction over Android text
- Host: GitHub
- URL: https://github.com/redmadrobot/textvalue
- Owner: RedMadRobot
- License: mit
- Created: 2024-04-27T13:03:58.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-05-13T15:43:11.000Z (about 2 years ago)
- Last Synced: 2025-01-16T05:55:05.707Z (over 1 year ago)
- Language: Kotlin
- Size: 63.5 KB
- Stars: 2
- Watchers: 9
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# TextValue
[][mavenCentral]
[][license]
TextValue is an abstraction allowing to work with a `String` and a string resource ID the same way.
---
- [Installation](#installation)
- [Usage](#usage)
- [Contributing](#contributing)
## Installation
Add the dependency:
```groovy
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`:
```kotlin
// 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:
```kotlin
// 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.
[mavenCentral]: https://search.maven.org/artifact/com.redmadrobot.textvalue/textvalue
[license]: LICENSE