Coder Social home page Coder Social logo

bundleargs's Introduction

#BundleArgs Release Android Arsenal Type safe bundle/intent builder for any class (activities, fragments, ...)

BundleBuilder is a type safe and fast way of creating intents/bundles and populating them with extras.

Gradle (via JitPack.io)

  1. add jitpack to your project's build.gradle:
repositories {
    maven { url "https://jitpack.io" }
}
  1. add the compile statement to your module's build.gradle and apply the apt plugin:
apply plugin: 'com.neenbedankt.android-apt'

dependencies {
    compile 'com.github.MFlisar:BundleBuilder:0.5'
    apt 'com.github.MFlisar:BundleBuilder:0.5'
}

Usage - Definitions

Here's a simple example that demonstrates the use in ANY class that needs a Bundle argument:

@BundleBuilder
public class Test
{
    @Arg
    Long id;
    @Arg
    String value;
    @Arg @Nullable
    String optionalValue;
    
    // define a constructor with Bundle args and the processor will create a build method that directly creates an object of this class
    public Test(Bundle args)
    {
        TestBundleBuilder.inject(args, this);
    }
}

And this is how you define it in an activity:

@BundleBuilder
public class MyActivity extends Activity
{
	@Arg
	String stringArg;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		MyActivityBundleBuilder.inject(getIntent().getExtras(), this);
	}
}

Usage - Builder

  1. If you have defined the default costructor with a bundle as args in it, you can directly create a class like this:
Test test = new TestBundleBuilder()
                .id(1L)
                .value("Test")
                .optionalValue("optionalValue")
                .create();
  1. You can always just create a Bundle with the builder like following:
Bundle bundle = new TestBundleBuilder()
                .id(1L)
                .value("Test")
                .optionalValue("optionalValue")
                .build();
  1. You can always just create an Intent with the builder like following (if the annotated class is an Activity or if the boolean flag alwaysAddIntentBuilder of the BundleBuilder is set to true:
Intent intent = new TestBundleBuilder()
                .id(1L)
                .value("Test")
                .optionalValue("optionalValue")
                .buildIntent();
  1. If the annotated class extends Activity, following method will be added to start the activity directly;
new MyActivityBundleBuilder()
        .stringArg("Test")
        .startActivity(context);

###Customisation

@BundleBuilder

You can define some setup variables like following (each one is optional):

@BundleBuilder(useConstructorForMandatoryArgs = true, setterPrefix = "with", alwaysAddIntentBuilder = false)
  • boolean useConstructorForMandatoryArgs(): default: false... if true, all mandatory fields will be part of the constructor, otherwise all mandatory fields need to be set with the builder style
  • String setterPrefix(): default ""... if not empty, all setters for the builder will be prefixed with this String. I.e. the field customField will be settable via a function builder.withCustomField(...) if the setterPrefix == "with"...
  • boolean alwaysAddIntentBuilder(): default: false... defines, if the buildIntent method is generated for non activity classes as well

@Arg

 @Arg(value = "", optional = false)
  • String value(): default: ""... if set, the builder setter will get the custom value instead of the one derived from the field name
  • boolean optional(): default: false... if true, fields are optional, if not, they must be set via constructor or via setter

Additional, fields can be annotated with @Nullable to define, if the field is allowed to be null or not, the builder will make the corresponding checks if necessary

###Demo

For an example with activities, check out the demo: Demo

Credits

This project is generally based on https://github.com/emilsjolander/IntentBuilder but was improved and restructured heavily in the meantime.

TODO

  • conductor/fragment/class demo?
  • ???

bundleargs's People

Contributors

mflisar avatar

Watchers

 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.