- Notifications
You must be signed in to change notification settings - Fork5
🪁 Android Resources Wrapper Library
License
cioccarellia/kite
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Android Resource Wrapper Library.
Fed up with typingContextCompat
,resources
andcontext
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]
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 yourApplication
class) and pass to it the application context.
classApp :Application() {overridefunonCreate() {super.onCreate()Kite.fly(this) }}
You're all set. You can now import theKite
object, select whichever resource category you want to access and fetch it using the resource id and the bracket notation[]
.
Beware: kite can not andwill not save you from the mess that is dealing with AndroidContext
.After all, kite is some cleverly placed syntactic sugar over those same android methods you are used to: kite itself holds a reference toContext
.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 againContextCompat
in your life. That's the precise reason kite was created.
// 🪁 Kitefab.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]// Standardfab.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)
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 anyKiteFetcher
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] )}
Kite delegates resource collection toKiteFetcher
s. Those classes contain a well defined implementation of the actual process of converting the givenid
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 |
- 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 trackerhere. Hope to see it fixed soon, by20202021 Marked as fixed
- A kite usually comes with differentcolors
- A kite is flown and controlled withstrings
About
🪁 Android Resources Wrapper Library