cakecutter

Project Url: Bryanx/cakecutter
Introduction: 🍰 A tiny android annotation library for injecting styled attributes into custom views.
More: Author   ReportBugs   
Tags:

A tiny annotation library for injecting styled attributes into custom views.

Example

Traditional way of loading styled attributes:

class CustomView(ctx: Context, attrs: AttributeSet) : FrameLayout(ctx, attrs) {
    var customText: String = ""
    var customNumber: Int = 0
    var customSize: Float = 0F

    init {
        val styledAttrs = ctx.obtainStyledAttributes(attrs, R.styleable.CustomView)
        try {
            customText = styledAttrs.getString(R.styleable.CustomView_customText) ?: customText
            customNumber = styledAttrs.getInt(R.styleable.CustomView_customNumber, customNumber)
            customSize = styledAttrs.getDimension(R.styleable.CustomView_customSize, customSize)
        } finally {
            styledAttrs.recycle()
        }
    }
}

With cakecutter:

class CustomView(ctx: Context, internal val attrs: AttributeSet) : FrameLayout(ctx, attrs) {
    @Styleable var customText: String = ""
    @Styleable var customNumber: Float = 0F
    @Styleable var customSize: Int = 0

    init {
        CakeCutter.bind(this)
    }
}

The styleables are bound by property name.

Some advantages:

  • Default values are assigned once instead of twice.
  • Layout/programmatic setters are combined.
  • Less boilerplate.

Alternative annotation:

@BindStyleable("customText") var otherTextName: String = ""

With this annotation the props can have different names than the styleables.

Generated code

It works similarly to Dagger and ButterKnife, here is the generated code for above example:

fun bind(view: CustomView) {
  view.context.obtainStyledAttributes(view.attrs, R.styleable.CustomView)
    .apply {
      try {
        view.customText = getBoolean(6, view.customText)
        view.customNumber = getString(R.styleable.CustomView_customNumber) ?: view.customNumber
        view.customSize = getDimension(R.styleable.CustomView_customSize, view.customSize)
      } finally {
        recycle()
      }
  }
}

Install

Add these dependencies in your app's build.gradle file:

implementation 'nl.bryanderidder.cakecutter:annotations:0.2.1'
kapt 'nl.bryanderidder.cakecutter:compiler:0.2.1'

Use annotationProcessor instead of kapt for Java projects. \ For kotlin projects add this to the top of your build.gradle file if it's not added yet:

apply plugin: "kotlin-kapt"

After adding the annotations you have to rebuild the project to load the generated code.

Note

This project is more of an expirement/study on annotation libraries and ButterKnife.

At the moment this library does not support these custom attribute types:

  • Fraction types
  • Multi types



The cake is now ready to be served.

🍰

Apps
About Me
GitHub: Trinea
Facebook: Dev Tools