Git Product home page Git Product logo

cakecutter's Introduction

๐Ÿฐ CakeCutter

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.

๐Ÿฐ

cakecutter's People

Contributors

bryanx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

cakecutter's Issues

Add an option to cast a styleable

It would be nice if, using an annotation argument, it is possible to decide which type the styleable should be cast to. For example load a reference type specifically as a drawable res id, load an integer as a string or load a float simply as a float instead of a dimension.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.