-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AND-145: add TextValue #63
Open
EmogurovAnton
wants to merge
3
commits into
main
Choose a base branch
from
feature/AND-145-add-textvalue
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
## [Unreleased] | ||
|
||
*No changes* | ||
|
||
## [1.0.0] | ||
|
||
- Public release textvalue library and textvalue-compose extensions library | ||
|
||
[unreleased]: https://github.com/RedMadRobot/TextValue/compare/1.0.0...main | ||
[1.0.0]: https://github.com/RedMadRobot/TextValue/compare/d5d1d9...1.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# TextValue | ||
|
||
[![Version](https://img.shields.io/maven-central/v/com.redmadrobot.textvalue/textvalue?style=flat-square)][mavenCentral] | ||
[![License](https://img.shields.io/github/license/RedMadRobot/gears-android?style=flat-square)][license] | ||
|
||
TextValue is an abstraction allowing to work with a `String` and a string resource ID the same way. | ||
|
||
--- | ||
<!-- START doctoc generated TOC please keep comment here to allow auto update --> | ||
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> | ||
|
||
- [Installation](#installation) | ||
- [Usage](#usage) | ||
- [Contributing](#contributing) | ||
|
||
<!-- END doctoc generated TOC please keep comment here to allow auto update --> | ||
|
||
## Installation | ||
|
||
Add the dependency: | ||
```groovy | ||
repositories { | ||
mavenCentral() | ||
google() | ||
} | ||
|
||
dependencies { | ||
// Views version | ||
implementation("com.redmadrobot.textvalue:textvalue:<version>") | ||
|
||
// Compose extensions for textvalue | ||
implementation("com.redmadrobot.textvalue:textvalue-compose:<version>") | ||
} | ||
``` | ||
|
||
## 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://central.sonatype.com/artifact/com.redmadrobot.textvalue/textvalue | ||
[license]: ../LICENSE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// For some reason gradle.properties in this project doesn't affect its subprojects | ||
val textValueGroup = group | ||
subprojects { group = textValueGroup } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
group=com.redmadrobot.textvalue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
plugins { | ||
convention.library.android | ||
alias(stack.plugins.kotlin.compose) | ||
} | ||
|
||
description = "Compose extensions for TextValue" | ||
|
||
dependencies { | ||
api(project(":textvalue:textvalue")) | ||
api(androidx.compose.ui) | ||
} | ||
|
||
android { | ||
namespace = "$group.compose" | ||
|
||
buildFeatures { | ||
compose = true | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
textvalue/textvalue-compose/src/main/kotlin/StringResources.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.redmadrobot.textvalue | ||
|
||
import android.content.res.Resources | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.ReadOnlyComposable | ||
import androidx.compose.ui.platform.LocalConfiguration | ||
import androidx.compose.ui.platform.LocalContext | ||
|
||
/** | ||
* Unwraps and returns a string for the given [text]. | ||
* @see TextValue | ||
*/ | ||
@Composable | ||
@ReadOnlyComposable | ||
public fun stringResource(text: TextValue): String { | ||
return resources().getString(text) | ||
} | ||
|
||
/** | ||
* A composable function that returns the [Resources]. It will be recomposed when [Configuration] | ||
* gets updated. | ||
*/ | ||
@Composable | ||
@ReadOnlyComposable | ||
private fun resources(): Resources { | ||
LocalConfiguration.current | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. зачем это нужно? |
||
return LocalContext.current.resources | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
plugins { | ||
convention.library.android | ||
id("kotlin-parcelize") | ||
} | ||
|
||
description = "TextValue is an abstraction over Android text" | ||
|
||
android { | ||
namespace = "$group" | ||
} | ||
|
||
dependencies { | ||
api(kotlin("stdlib")) | ||
api(androidx.annotation) | ||
compileOnly(androidx.compose.runtime) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.redmadrobot.textvalue | ||
|
||
import android.content.Context | ||
import android.content.res.Resources | ||
import android.os.Parcelable | ||
import android.view.View | ||
import androidx.annotation.StringRes | ||
import androidx.compose.runtime.Immutable | ||
import kotlinx.parcelize.Parcelize | ||
|
||
/** | ||
* Wrapper to make it possible to work with plain [String] and [StringRes] in the same way. | ||
* | ||
* ``` | ||
* // 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 | ||
* val messageText = getString(message) | ||
* ``` | ||
*/ | ||
@Immutable | ||
public sealed interface TextValue : Parcelable { | ||
|
||
/** Retrieves [String] using the given [resources]. */ | ||
public fun get(resources: Resources): String | ||
|
||
override fun equals(other: Any?): Boolean | ||
override fun hashCode(): Int | ||
|
||
/** Plain string. */ | ||
@Parcelize | ||
public data class Plain(public val string: String) : TextValue { | ||
override fun get(resources: Resources): String = string | ||
} | ||
|
||
/** 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 companion object { | ||
|
||
/** Empty [TextValue]. */ | ||
public val EMPTY: TextValue = TextValue("") | ||
} | ||
} | ||
|
||
/** Creates [TextValue] from the given [resourceId]. */ | ||
public fun TextValue(@StringRes resourceId: Int): TextValue = TextValue.Resource(resourceId) | ||
|
||
/** 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) | ||
} | ||
|
||
/** | ||
* Unwraps and returns a string for the given [text]. | ||
* @see TextValue | ||
*/ | ||
public fun Context.getString(text: TextValue): String = resources.getString(text) | ||
|
||
/** | ||
* Unwraps and returns a string for the given [text]. | ||
* @see TextValue | ||
*/ | ||
public fun View.getString(text: TextValue): String = resources.getString(text) | ||
|
||
/** | ||
* Unwraps and returns a string for the given [text]. | ||
* @see TextValue | ||
*/ | ||
public fun Resources.getString(text: TextValue): String = text.get(this) | ||
|
||
/** | ||
* Returns TextValue itself if it is not `null`, or the [TextValue.EMPTY] otherwise. | ||
* @see TextValue | ||
*/ | ||
public fun TextValue?.orEmpty(): TextValue = this ?: TextValue.EMPTY |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.