Last active
April 30, 2019 21:48
-
-
Save kakai248/52adcba7437eb3a830e601c90678e331 to your computer and use it in GitHub Desktop.
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
import android.annotation.SuppressLint | |
import android.app.Activity | |
import android.content.Context | |
import android.content.res.ColorStateList | |
import android.content.res.Resources | |
import android.graphics.Typeface | |
import android.graphics.drawable.Drawable | |
import android.view.View | |
import android.view.animation.Animation | |
import android.view.animation.AnimationUtils | |
import androidx.annotation.* | |
import androidx.core.content.ContextCompat | |
import androidx.core.content.res.ResourcesCompat | |
import androidx.fragment.app.Fragment | |
@Suppress("NOTHING_TO_INLINE") | |
@SuppressLint("SupportAnnotationUsage") | |
inline class ResourceProvider(val context: Context) { | |
inline fun text(@StringRes resId: Int): CharSequence = context.getText(resId) | |
inline fun textArray(@ArrayRes resId: Int): Array<out CharSequence> = | |
context.resources.getTextArray(resId) | |
inline fun quantityText(@PluralsRes resId: Int, quantity: Int): CharSequence = | |
context.resources.getQuantityText(resId, quantity) | |
inline fun string(@StringRes resId: Int): String = context.getString(resId) | |
inline fun string(@StringRes resId: Int, vararg formatArgs: Any): String = | |
context.getString(resId, *formatArgs) | |
inline fun stringArray(@ArrayRes resId: Int): Array<out String> = | |
context.resources.getStringArray(resId) | |
inline fun quantityString(@PluralsRes resId: Int, quantity: Int): String = | |
context.resources.getQuantityString(resId, quantity) | |
inline fun quantityString( | |
@PluralsRes resId: Int, quantity: Int, | |
vararg formatArgs: Any | |
): String = | |
context.resources.getQuantityString(resId, quantity, *formatArgs) | |
inline fun integer(@IntegerRes resId: Int) = context.resources.getInteger(resId) | |
inline fun intArray(@ArrayRes resId: Int): IntArray = context.resources.getIntArray(resId) | |
inline fun boolean(@BoolRes resId: Int) = context.resources.getBoolean(resId) | |
inline fun dimension(@DimenRes resId: Int) = context.resources.getDimension(resId) | |
inline fun dimensionPixelSize(@DimenRes resId: Int) = | |
context.resources.getDimensionPixelSize(resId) | |
inline fun dimensionPixelOffset(@DimenRes resId: Int) = | |
context.resources.getDimensionPixelOffset(resId) | |
inline fun drawable(@DrawableRes resId: Int): Drawable? = | |
ContextCompat.getDrawable(context, resId) | |
@ColorInt | |
inline fun color(@ColorRes resId: Int) = ContextCompat.getColor(context, resId) | |
@Throws(Resources.NotFoundException::class) | |
inline fun colorStateList(@ColorRes resId: Int): ColorStateList? = | |
ContextCompat.getColorStateList(context, resId) | |
@Throws(Resources.NotFoundException::class) | |
inline fun font(@FontRes id: Int): Typeface? = ResourcesCompat.getFont(context, id) | |
@Throws(Resources.NotFoundException::class) | |
inline fun animation(@AnimRes id: Int): Animation = AnimationUtils.loadAnimation(context, id) | |
} | |
inline val Activity.resourceProvider: ResourceProvider | |
get() = ResourceProvider(this) | |
inline val Fragment.resourceProvider: ResourceProvider | |
get() = ResourceProvider(requireContext()) | |
inline val View.resourceProvider: ResourceProvider | |
get() = ResourceProvider(context) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment