diff --git a/resources-ktx/CHANGELOG.md b/resources-ktx/CHANGELOG.md index 341caa7..4bb1864 100644 --- a/resources-ktx/CHANGELOG.md +++ b/resources-ktx/CHANGELOG.md @@ -2,9 +2,7 @@ ### Changed -- **Text**: added abstract methods `equals`, `hashCode` -- **Text**: added `Parcelable` implementation -- **Text**: added `companion object` and `Text.EMPTY` implementation +- **Breaking change**: `Text` moved to a separate library [com.redmadrobot.textvalue](https://github.com/RedMadRobot/TextValue). ## [1.3.1-0] (2021-10-03) diff --git a/resources-ktx/README.md b/resources-ktx/README.md index e518b2a..a2cee67 100644 --- a/resources-ktx/README.md +++ b/resources-ktx/README.md @@ -80,31 +80,6 @@ Dimension converters for `Context` (the same available for `Resources`): - `Context.pxToDp(px: Int): Float` - `Context.pxToDp(px: Float): Float` -### Wrapper `Text` - -**Text** 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` into `Text` using `Text.Plain(String)` and `Text.Resource(Int)`, accordingly and use method `Text.get(Context)` to retrieve `String`: - -```kotlin -// in some place where we can't access Context -val errorMessage = exception.message?.let(Text::Plain) ?: Text.Resource(R.string.unknown_error) -showMessage(errorMessage) - -// in Activity, Fragment or View -fun showMessage(text: Text) { - val messageText = text.get(context) - //... -} -``` - -There are extensions to work with `Text` like with `StringRes`: - -- `Context.getString(text: Text): String` -- `Fragment.getString(text: Text): String` -- `View.getString(text: Text): String` - ## Contributing Merge requests are welcome. diff --git a/resources-ktx/src/main/kotlin/Text.kt b/resources-ktx/src/main/kotlin/Text.kt deleted file mode 100644 index e61e4d9..0000000 --- a/resources-ktx/src/main/kotlin/Text.kt +++ /dev/null @@ -1,67 +0,0 @@ -@file:Suppress("NOTHING_TO_INLINE") - -package com.redmadrobot.extensions.resources - -import android.content.Context -import android.os.Parcelable -import android.view.View -import androidx.annotation.StringRes -import androidx.fragment.app.Fragment -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 = exception.message?.let(Text::Plain) ?: Text.Resource(R.string.unknown_error) - * showMessage(errorMessage) - * - * // in Activity, Fragment or View - * val messageText = getString(message) - * ``` - */ -public sealed class Text : Parcelable { - - /** Retrieves [String] using given [context]. */ - public abstract fun get(context: Context): String - - abstract override fun equals(other: Any?): Boolean - abstract override fun hashCode(): Int - - /** Plain string. */ - @Parcelize - public data class Plain(public val string: String) : Text() { - override fun get(context: Context): String = string - } - - /** String resource, requires [Context] to get [String]. */ - @Parcelize - public data class Resource(@StringRes public val resourceId: Int) : Text() { - override fun get(context: Context): String = context.getString(resourceId) - } - - public companion object { - - /** Empty [Text] */ - public val EMPTY: Text = Plain("") - } -} - -/** - * Unwraps and returns a string for the given [text]. - * @see Text - */ -public inline fun Context.getString(text: Text): String = text.get(this) - -/** - * Unwraps and returns a string for the given [text]. - * @see Text - */ -public inline fun Fragment.getString(text: Text): String = requireContext().getString(text) - -/** - * Unwraps and returns a string for the given [text]. - * @see Text - */ -public inline fun View.getString(text: Text): String = context.getString(text)