Skip to content

Commit

Permalink
resources-ktx: Add inline modifier back
Browse files Browse the repository at this point in the history
  • Loading branch information
osipxd committed Oct 3, 2021
1 parent 87cd893 commit 7a609b9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
4 changes: 4 additions & 0 deletions resources-ktx/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
- **Fragment**: new extensions `getQuantityString`
- **View**: new extensions `getQuantityString`

### Fixed

- `Inline` modifier added back to all resource accessors

### Dependencies

- androidx.core 1.5.0 -> 1.6.0
Expand Down
18 changes: 9 additions & 9 deletions resources-ktx/src/main/kotlin/FragmentResourcesAccessors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import androidx.fragment.app.Fragment
* @see ContextCompat.getColor
*/
@ColorInt
public fun Fragment.getColor(@ColorRes resId: Int): Int = ContextCompat.getColor(requireContext(), resId)
public inline fun Fragment.getColor(@ColorRes resId: Int): Int = ContextCompat.getColor(requireContext(), resId)

/**
* Returns a color state list associated with given [resId].
* @see ContextCompat.getColorStateList
*/
public fun Fragment.getColorStateList(@ColorRes resId: Int): ColorStateList? {
public inline fun Fragment.getColorStateList(@ColorRes resId: Int): ColorStateList? {
return ContextCompat.getColorStateList(requireContext(), resId)
}

Expand All @@ -31,7 +31,7 @@ public fun Fragment.getColorStateList(@ColorRes resId: Int): ColorStateList? {
* @see AppCompatResources.getDrawable
* @see getDrawableWithTint
*/
public fun Fragment.getDrawable(@DrawableRes resId: Int): Drawable? {
public inline fun Fragment.getDrawable(@DrawableRes resId: Int): Drawable? {
return AppCompatResources.getDrawable(requireContext(), resId)
}

Expand All @@ -41,36 +41,36 @@ public fun Fragment.getDrawable(@DrawableRes resId: Int): Drawable? {
* @see getDrawable
* @see getDrawableWithTint
*/
public fun Fragment.getDrawableWithTint(@DrawableRes resId: Int, @ColorInt tint: Int): Drawable? {
public inline fun Fragment.getDrawableWithTint(@DrawableRes resId: Int, @ColorInt tint: Int): Drawable? {
return getDrawable(resId)?.withTint(tint)
}

/**
* Returns a dimension value associated with given [resId] in pixels.
* @see Resources.getDimension
*/
public fun Fragment.getDimension(@DimenRes resId: Int): Float = resources.getDimension(resId)
public inline fun Fragment.getDimension(@DimenRes resId: Int): Float = resources.getDimension(resId)

/**
* Returns a dimension value associated with given [resId] in integer pixels.
* A size conversion involves rounding the base value, and ensuring that
* a non-zero base value is at least one pixel in size.
* @see Resources.getDimensionPixelSize
*/
public fun Fragment.getDimensionPixelSize(@DimenRes resId: Int): Int = resources.getDimensionPixelSize(resId)
public inline fun Fragment.getDimensionPixelSize(@DimenRes resId: Int): Int = resources.getDimensionPixelSize(resId)

/**
* Returns a dimension value associated with given [resId] in integer pixels.
* An offset conversion involves simply truncating the base value to an integer.
* @see Resources.getDimensionPixelOffset
*/
public fun Fragment.getDimensionPixelOffset(@DimenRes resId: Int): Int = resources.getDimensionPixelOffset(resId)
public inline fun Fragment.getDimensionPixelOffset(@DimenRes resId: Int): Int = resources.getDimensionPixelOffset(resId)

/**
* Returns a string associated with given [resId] and pluralized according to [quantity].
* @see Resources.getQuantityString
*/
public fun Fragment.getQuantityString(@PluralsRes resId: Int, quantity: Int): String {
public inline fun Fragment.getQuantityString(@PluralsRes resId: Int, quantity: Int): String {
return resources.getQuantityString(resId, quantity)
}

Expand All @@ -79,6 +79,6 @@ public fun Fragment.getQuantityString(@PluralsRes resId: Int, quantity: Int): St
* substituting the [formatArgs] as defined in [String.format].
* @see Resources.getQuantityString
*/
public fun Fragment.getQuantityString(@PluralsRes resId: Int, quantity: Int, vararg formatArgs: Any): String {
public inline fun Fragment.getQuantityString(@PluralsRes resId: Int, quantity: Int, vararg formatArgs: Any): String {
return resources.getQuantityString(resId, quantity, *formatArgs)
}
8 changes: 5 additions & 3 deletions resources-ktx/src/main/kotlin/Text.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("NOTHING_TO_INLINE")

package com.redmadrobot.extensions.resources

import android.content.Context
Expand Down Expand Up @@ -37,16 +39,16 @@ public sealed class Text {
* Unwraps and returns a string for the given [text].
* @see Text
*/
public fun Context.getString(text: Text): String = text.get(this)
public inline fun Context.getString(text: Text): String = text.get(this)

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

/**
* Unwraps and returns a string for the given [text].
* @see Text
*/
public fun View.getString(text: Text): String = context.getString(text)
public inline fun View.getString(text: Text): String = context.getString(text)
24 changes: 12 additions & 12 deletions resources-ktx/src/main/kotlin/ViewResourcesAccessors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,82 +15,82 @@ import androidx.core.content.ContextCompat
* @see ContextCompat.getColor
*/
@ColorInt
public fun View.getColor(@ColorRes resId: Int): Int = ContextCompat.getColor(context, resId)
public inline fun View.getColor(@ColorRes resId: Int): Int = ContextCompat.getColor(context, resId)

/**
* Returns a color state list associated with given [resId].
* @see ContextCompat.getColorStateList
*/
public fun View.getColorStateList(@ColorRes resId: Int): ColorStateList? =
public inline fun View.getColorStateList(@ColorRes resId: Int): ColorStateList? =
ContextCompat.getColorStateList(context, resId)

/**
* Returns a dimension value associated with given [resId] in pixels.
* @see Resources.getDimension
*/
public fun View.getDimension(@DimenRes resId: Int): Float = resources.getDimension(resId)
public inline fun View.getDimension(@DimenRes resId: Int): Float = resources.getDimension(resId)

/**
* Returns a dimension value associated with given [resId] in integer pixels.
* A size conversion involves rounding the base value, and ensuring that
* a non-zero base value is at least one pixel in size.
* @see Resources.getDimensionPixelSize
*/
public fun View.getDimensionPixelSize(@DimenRes resId: Int): Int = resources.getDimensionPixelSize(resId)
public inline fun View.getDimensionPixelSize(@DimenRes resId: Int): Int = resources.getDimensionPixelSize(resId)

/**
* Returns a dimension value associated with given [resId] in integer pixels.
* An offset conversion involves simply truncating the base value to an integer.
* @see Resources.getDimensionPixelOffset
*/
public fun View.getDimensionPixelOffset(@DimenRes resId: Int): Int = resources.getDimensionPixelOffset(resId)
public inline fun View.getDimensionPixelOffset(@DimenRes resId: Int): Int = resources.getDimensionPixelOffset(resId)

/**
* Returns a drawable associated with given [resId],
* or `null` if there is no such drawable.
* @see AppCompatResources.getDrawable
* @see getDrawableWithTint
*/
public fun View.getDrawable(@DrawableRes resId: Int): Drawable? = AppCompatResources.getDrawable(context, resId)
public inline fun View.getDrawable(@DrawableRes resId: Int): Drawable? = AppCompatResources.getDrawable(context, resId)

/**
* Returns a drawable associated with given [resId],
* or throws [IllegalArgumentException] if there is no such drawable.
* @see getDrawable
* @see getDrawableWithTint
*/
public fun View.requireDrawable(@DrawableRes resId: Int): Drawable = requireNotNull(getDrawable(resId))
public inline fun View.requireDrawable(@DrawableRes resId: Int): Drawable = requireNotNull(getDrawable(resId))

/**
* Returns a drawable associated with given [resId] and tinted with specified [tint] color,
* or `null` if there is no such drawable.
* @see getDrawable
* @see withTint
*/
public fun View.getDrawableWithTint(@DrawableRes resId: Int, @ColorInt tint: Int): Drawable? {
public inline fun View.getDrawableWithTint(@DrawableRes resId: Int, @ColorInt tint: Int): Drawable? {
return getDrawable(resId)?.withTint(tint)
}

/**
* Returns a localized string associated with given [resId].
* @see Resources.getString
*/
public fun View.getString(@StringRes resId: Int): String = resources.getString(resId)
public inline fun View.getString(@StringRes resId: Int): String = resources.getString(resId)

/**
* Returns a localized formatted string associated with given [resId],
* substituting the [formatArgs] as defined in [String.format].
* @see Resources.getString
*/
@Suppress("SpreadOperator") // Doesn't affect performance
public fun View.getString(@StringRes resId: Int, vararg formatArgs: Any): String =
public inline fun View.getString(@StringRes resId: Int, vararg formatArgs: Any): String =
resources.getString(resId, *formatArgs)

/**
* Returns a string associated with given [resId] and pluralized according to [quantity].
* @see Resources.getQuantityString
*/
public fun View.getQuantityString(@PluralsRes resId: Int, quantity: Int): String {
public inline fun View.getQuantityString(@PluralsRes resId: Int, quantity: Int): String {
return resources.getQuantityString(resId, quantity)
}

Expand All @@ -99,6 +99,6 @@ public fun View.getQuantityString(@PluralsRes resId: Int, quantity: Int): String
* substituting the [formatArgs] as defined in [String.format].
* @see Resources.getQuantityString
*/
public fun View.getQuantityString(@PluralsRes resId: Int, quantity: Int, vararg formatArgs: Any): String {
public inline fun View.getQuantityString(@PluralsRes resId: Int, quantity: Int, vararg formatArgs: Any): String {
return resources.getQuantityString(resId, quantity, *formatArgs)
}

0 comments on commit 7a609b9

Please sign in to comment.