GithubHelp home page GithubHelp logo

giastfader / blade Goto Github PK

View Code? Open in Web Editor NEW

This project forked from frantisekgazo/blade

0.0 2.0 0.0 261 KB

Android library for boilerplate destruction

License: Apache License 2.0

Java 96.54% Groovy 3.46%

blade's Introduction

Bl@De

Android library for boilerplate destruction - "Just code what is worth coding"

  • Generates boilerplate code based on used annotations and lets you focus on what matters.
  • Generated code is fully traceable.
  • Everything is generated during compile time.
  • No reflection used!

Usage

Available annotations:

@Arg

Annotation for generating newInstance() methods for your Fragment classes.

Without using this library you would have to write this:

public class MyFragment extends Fragment {

    private static final String EXTRA_TEXT = "arg_text";
    private static final String EXTRA_DATA = "arg_data";

    public static MyFragment newInstance(String text, MyData data) {
        MyFragment frag = new MyFragment();
        Bundle args = new Bundle();
        args.putString(EXTRA_TEXT, text);
        args.putParcelable(EXTRA_DATA, data);
        frag.setArguments(args);
        return frag;
    }

    private String mText;
    private MyData mData;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        mText = getArguments().getString(EXTRA_TEXT);
        mData = (MyData) getArguments().getParcelable(EXTRA_DATA);
    }
}

But with this library you can write this:

public class MyFragment extends Fragment {

    @Arg 
    String mText;
    @Arg 
    MyData mData;
  
}

Class named F (= Fragment) is automatically generated for you. This class contains 1 method for each Fragmentclass with annotated arguments:

  • X newX(Context c, T1 arg1[, T2 arg2, ...]) - Creates new instance of class X.

e.g. for MyFragment class it contains method named newMyFragment with 2 parameters: String and MyData. So you can easily create new fragment by calling:

F.newMyFragment("some-string", new MyData());

And given values will be set to corresponding attributes annotated with @Arg.

@Extra

Annotation for generating newIntent() methods for your Activity classes.

Without using this library you would have to write this:

public class MyActivity extends Activity {

    private static final String EXTRA_TEXT = "extra_text";
    private static final String EXTRA_DATA = "arg_data";

    public static Intent newIntent(Contaxt context, String text, MyData data) {
        Intent intent = new Intent(context, MyActivity.class);
        intent.putExtra(EXTRA_TEXT, text);
        intent.putExtra(EXTRA_DATA, data);
        return intent;
    }

    private String mText;
    private MyData mData;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        Bundle extras = getIntent().getExtras();
        mText = extras.getString(EXTRA_TEXT);
        mData = (MyData) extras.getParcelable(EXTRA_DATA);
    }
}

But with this library you can write this:

public class MyActivity extends Activity {

    @Extra 
    String mText;
    @Extra
    MyData mData;
  
}

Class named I (= Intent) is automatically generated for you. This class contains 2 methods for each Activity class with annotated arguments:

  • Intent forX(Context c, T1 extra1[, T2 extra2, ...]) - Creates new Intent which can be used for starting new activity. This lets you add additional flags to this intent.
  • void startX(Context c, T1 extra1[, T2 extra2, ...]) - Creates new Intent and starts new activity.

e.g. for MyActivity class it contains methods named forMyActivity and startMyActivity with 2 parameters: String and MyData. So you can easily start new Activity by calling:

I.startMyActivity("some-string", new MyData());

And given values will be set to corresponding attributes annotated with @Extra.

@State

Annotation for simplifying state management.

For each class containing attributes annotated with @State Blade will generate helper class named NameOfClass_Helper with 2 static methods for state management:

  • saveState(Bundle)
  • restoreState(Bundle)

Classes extending from Fragment, Activity or View are managed automatically, so you don't need to call saveState(Bundle) or restoreState(Bundle). But other classes has to call these 2 generated methods when needed.

Without using this library you would have to write this:

public class MyActivity extends Activity {

    private static final String EXTRA_TEXT = "extra_text";
    private static final String EXTRA_DATA = "arg_data";

    private String mText;
    private MyData mData;
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelable(EXTRA_DATA, mData);
        outState.putString(EXTRA_TEXT, mText);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        mData = savedInstanceState.getParcelable(EXTRA_DATA);
        mText = (MyData) savedInstanceState.getString(EXTRA_TEXT);
    }   
    
}

But with this library you can write this:

public class MyActivity extends Activity {

    @State 
    String mText;
    @State
    MyData mData;
  
}

Download

Gradle plugin:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0'
        // Add Blade plugin
        classpath 'eu.f3rog.blade:plugin:1.1.0'
    }
}

apply plugin: 'com.android.application'
// Apply Blade plugin
apply plugin: 'blade'

This library uses Annotation Processor so you have to apply also this Gradle plugin, if not already used:

classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
apply plugin: 'com.neenbedankt.android-apt'

blade's People

Contributors

frantisekgazo avatar

Watchers

 avatar  avatar

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.