/kite

🪁 Android Resources Wrapper Library

Primary LanguageKotlinApache License 2.0Apache-2.0

Kite

Android Resource Wrapper Library.

Download from MavenCentral Android Arsenal CircleCI Android CI Codacy Kotlin Android Min Sdk Android Compile Version License

TLDR

Fed up with typing ContextCompat, resources and context all over your apps to access your resources? Say no more.

Gradle
dependencies {
    implementation 'com.github.cioccarellia:kite:1.2.1'
}
Kotlin DSL
dependencies {
    implementation("com.github.cioccarellia:kite:1.2.1")
}
Maven
<dependency>
    <groupId>com.github.cioccarellia</groupId>
    <artifactId>kite</artifactId>
    <version>1.2.1</version>
    <type>pom</type>
</dependency>
  • 🪁 Access all app resources with one unified syntax.
  • 🧬 Null safe layer between the Android framework java code and your app.
  • 🧊 Transparent and lightweight wrapper.
  • 🔒 Extensive built-in checks.
  • ⚡ Easy to implement in existing apps.
  • ❤️ Kotlin powered, 100%.
val text = Kite.string[R.string.welcome_back]

Usage

Kite is a handy and lightweight android library which aims at reducing redundancy and decreasing android code complexity. It encloses and simplifies resource access within the Kite domain, and abstracts away the implementation logic needed to fetch the desired value, making interactions with the android framework smooth and frictionless. To get started initialize kite (ideally inside your Application class) and pass to it the application context.

class App : Application() {
    override fun onCreate() {
        super.onCreate()
        Kite.fly(this)
    }
}

You're all set. You can now import the Kite object, select whichever resource category you want to access and fetch it using the resource id and the bracket notation [].

Beware: kite can not and will not save you from the mess that is dealing with Android Context. After all, kite is some cleverly placed syntactic sugar over those same android methods you are used to: kite itself holds a reference to Context. It will, however, unify and thoroughly uniform your experience with dealing with all android related resource extraction operations, which can turn to be extremely practical. It may also save you from typing again ContextCompat in your life. That's the precise reason kite was created.

Samples

// 🪁 Kite
fab.rippleColor =         Kite.color[R.color.md_light_lime]
fab.backgroundTintList =  Kite.colorStateList[R.color.md_lime]
fab.text =                Kite.string[R.string.unread_notifications, "69"]
fab.isVisible =           Kite.bools[R.bool.show_fab]

// Standard
fab.rippleColor =         ContextCompat.getColor(context, R.color.md_light_lime)
fab.backgroundTintList =  ColorStateList.valueOf(R.color.md_lime)
fab.text =                appContext.getString(R.string.unread_notifications, "69")
fab.isVisible =           resources.getBoolean(R.bool.show_fab)

Context Switching

One clear disadvantage to using kite with respect to doing things the old way is choosing which context to use. That's why Kite comes packed with extension functions for permanent change / temporary switch the in-use context, so that you have full control over which context is used to do what.

  • runWith can be invoked on any KiteFetcher object, it is chainable and it temporarily runs the passed lambda in the desired context.
Kite.color.runWith(this) { color ->
    button.setBackground(
        color[R.color.colorAccent]
    )
}

Supported Resources

Kite delegates resource collection to KiteFetchers. Those classes contain a well defined implementation of the actual process of converting the given id to the output type.

Resource Type AAPT class Fetcher Input Output Implementation API Variants
Strings R.string Kite.string @StringRes string: Int String Context.getString() / formatArgs
Plurals R.plurals Kite.plural @PluralRes plural: Int, quantity: Int String Resources.getQuantityString() / formatArgs
Texts R.string Kite.text @StringRes text: Int CharSequence Context.getText() / /
Color R.color Kite.color @ColorRes color: Int @ColorInt Color ContextCompat.getColor() / /
ColorStateLists R.color Kite.colorStateList @ColorRes colorStateList: Int ColorStateList ContextCompat.getColorStateList() / /
Drawables R.drawable Kite.drawable @DrawableRes drawable: Int Drawable ContextCompat.getDrawable() / Resources.Theme?
Layouts R.layout Kite.layout @LayoutRes layout: Int XmlResourceParser Resources.getLayout() / /
Integer R.integer Kite.integer @IntegerRes integer: Int Int Resources.getInteger() / /
Booleans R.bool Kite.booleans @BoolRes boolean: Int Boolean Resources.getBoolean() / /
Dimensions R.dimen Kite.dimension @DimenRes dimensions: Int Float Resources.getDimensions() / /
Fractions R.fraction Kite.fraction @FractionRes fraction: Int, base: Int, pbase: Int Float Resources.getFraction() / /
Fonts R.font Kite.font @FontRes font: Int Typeface Resources.getFont() 26 /
Animations R.anim Kite.animation @AnimRes animation: Int Animation AnimationUtils.loadAnimation() / /
Interpolators R.interpolator Kite.interpolation @InterpolatorRes interpolator: Int Interpolator AnimationUtils.loadInterpolator() / /
IntArray R.array Kite.intArray @ArrayRes intArray: Int IntArray Resources.getIntArray() / /
StringArray R.array Kite.stringArray @ArrayRes stringArray: Int Array<out String> Resources.getStringArray() / /
TypedArray R.array Kite.typedArray @ArrayRes typedArray: Int TypedArray Resources.obtainTypedArray() / /
Identifiers R.id Kite.identifier name: String, defType: String, defPackage: String Int Resources.getIdentifier() / /
Xmls R.xml Kite.xml @XmlRes xml: Int XmlResourceParser Resources.getXml() / /
Raws R.raw Kite.raw @RawRes raw: Int InputStream Resources.openRawResource() / TypedValue

⚠️ Known Issues

  • Annotation checks over resource function parameters (functions which take as argument some annotated value, like @ColorRes, @StringRes) are not extensively performed by Android Studio, when using kotlin operator functions. I created a bug report @ google issue tracker here. Hope to see it fixed soon, by 2020 2021 Marked as fixed

Kite Puns

  • A kite usually comes with different colors
  • A kite is flown and controlled with strings