Skip to content
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

draft: resources-ktx: Make Text compatible with compose #49

Closed
wants to merge 10 commits into from
5 changes: 5 additions & 0 deletions buildSrc/src/main/kotlin/dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ object androidx {
val resources by this
}

object compose {
const val version = "1.1.1"
osipxd marked this conversation as resolved.
Show resolved Hide resolved
const val ui = "androidx.compose.ui:ui:$version"
}

object core : Group("androidx.core", version = "1.6.0") {
val ktx by this
}
Expand Down
1 change: 0 additions & 1 deletion resources-ktx/src/main/AndroidManifest.xml

This file was deleted.

67 changes: 0 additions & 67 deletions resources-ktx/src/main/kotlin/Text.kt

This file was deleted.

File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions resources/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
subprojects {
version = "1.3.1-0"
description = "A set of Kotlin extensions for accessing resources"
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ plugins {
id("kotlin-parcelize")
}

version = "1.3.1-0"
description = "A set of Kotlin extensions for accessing resources"

dependencies {
api(jetbrains.kotlin.stdlib)
api(androidx.annotation)
api(androidx.fragment)
implementation(androidx.core)
implementation(androidx.appcompat.resources)
}

android {
namespace = "com.redmadrobot.extensions.resources.common"
}
1 change: 1 addition & 0 deletions resources/resources-common/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest />
osipxd marked this conversation as resolved.
Show resolved Hide resolved
93 changes: 93 additions & 0 deletions resources/resources-common/src/main/kotlin/TextValue.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
@file:Suppress("NOTHING_TO_INLINE")

package com.redmadrobot.extensions.resources

import android.content.Context
import android.content.res.Resources
import android.os.Parcelable
import android.view.View
import androidx.annotation.StringRes
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)
* ```
*/
public sealed class TextValue : Parcelable {

/** Retrieves [String] using the given [context]. */
@Deprecated("Use get with Resources instead.", ReplaceWith("this.get(context.resources)"))
public fun get(context: Context): String = get(context.resources)

/** Retrieves [String] using the given [resources]. */
public abstract fun get(resources: Resources): String

abstract override fun equals(other: Any?): Boolean
abstract 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 inline fun TextValue(@StringRes resourceId: Int): TextValue = TextValue.Resource(resourceId)

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

/** Creates [TextValue] from the given [string], or from the [defaultResourceId] if string is `null`. */
public inline 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 inline fun Context.getString(text: TextValue): String = resources.getString(text)

/**
* Unwraps and returns a string for the given [text].
* @see TextValue
*/
public inline fun View.getString(text: TextValue): String = resources.getString(text)

/**
* Unwraps and returns a string for the given [text].
* @see TextValue
*/
public inline fun Resources.getString(text: TextValue): String = text.get(this)

@Deprecated("Text renamed to TextValue for compatibility with compose", ReplaceWith("TextValue"))
public typealias Text = TextValue

@Suppress("FunctionName")
@Deprecated("Text renamed to TextValue for compatibility with compose", ReplaceWith("TextValue(resourceId)"))
public fun TextValue.Companion.Resource(@StringRes resourceId: Int): TextValue.Resource = TextValue.Resource(resourceId)

@Suppress("FunctionName")
@Deprecated("Text renamed to TextValue for compatibility with compose", ReplaceWith("TextValue(string)"))
public fun TextValue.Companion.Plain(string: String): TextValue.Plain = TextValue.Plain(string)
23 changes: 23 additions & 0 deletions resources/resources-compose/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import com.redmadrobot.build.dependencies.androidx

plugins {
id("com.redmadrobot.android-library")
id("com.redmadrobot.publish")
}

dependencies {
api(projects.resources.resourcesCommon)
api(androidx.compose.ui)
}

android {
namespace = "com.redmadrobot.extensions.resources.compose"

buildFeatures {
compose = true
}

composeOptions {
kotlinCompilerExtensionVersion = androidx.compose.version
}
}
1 change: 1 addition & 0 deletions resources/resources-compose/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest />
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this one

28 changes: 28 additions & 0 deletions resources/resources-compose/src/main/kotlin/StringResources.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.redmadrobot.extensions.resources

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
public fun stringResource(text: TextValue): String {
val resources = resources()
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
return LocalContext.current.resources
}
17 changes: 17 additions & 0 deletions resources/resources-ktx/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import com.redmadrobot.build.dependencies.androidx

plugins {
id("com.redmadrobot.android-library")
id("com.redmadrobot.publish")
}

dependencies {
api(projects.resources.resourcesCommon)
api(androidx.fragment)
implementation(androidx.core)
implementation(androidx.appcompat.resources)
}

android {
namespace = "com.redmadrobot.extensions.resources"
}
1 change: 1 addition & 0 deletions resources/resources-ktx/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest />
osipxd marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,9 @@ public inline fun Fragment.getQuantityString(@PluralsRes resId: Int, quantity: I
public inline fun Fragment.getQuantityString(@PluralsRes resId: Int, quantity: Int, vararg formatArgs: Any): String {
return resources.getQuantityString(resId, quantity, *formatArgs)
}

/**
* Unwraps and returns a string for the given [text].
* @see TextValue
*/
public inline fun Fragment.getString(text: TextValue): String = requireContext().getString(text)
6 changes: 5 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS")

pluginManagement {
repositories {
google()
Expand All @@ -12,6 +14,8 @@ include(
"fragment-ktx",
"fragment-args-ktx",
"lifecycle-livedata-ktx",
"resources-ktx",
"resources:resources-common",
"resources:resources-compose",
"resources:resources-ktx",
"viewbinding-ktx",
)