Git Product home page Git Product logo

auto-bundler's Introduction

Travis CI Codecov Maven Central Nexus Snapshot Android SDK

Auto Bundler

Passing key-value pairs with Bundle is the most common approach of exchanging data across Android components. Unfortunately, much of the process of sending and receiving those key-value pairs (known as extra) requires a lot of boilerplate code. Auto Bundler aims to minify the process with annotation processing.

public class ExampleActivity extends Activity {
    @Extra String username;
    @State int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundler.bindExtras(this);
        Bundler.bindStates(this, savedInstanceState);
        // TODO: Use fields...
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Bundler.saveStates(this, outState);
    }
}

Download

repositories {
    mavenCentral()
}
dependencies {
    implementation "com.hendraanggrian.auto:bundle:$version"
    annotationProcessor "com.hendraanggrian.auto:bundle-compiler:$version" // or kapt for Kotlin
}

Usage

Extra binding

Binding

@BindExtra for binding extra value to field, field cannot be private. When key is not provided, field name will be used as the key.

@BindExtra @JvmField var username: String

Wrapping

Create extras with varargs argument with extrasOf(). This is optional, any Bundle would work just fine.

Intent intent = new Intent(context, ExampleActivity.class);
intent.putExtras(Bundler.extrasOf(ExampleActivity.class, "Hendra Anggrian", 24));
startActivity(intent);

State binding

Restoring

@BindState for binding extra value to field, field cannot be private. When key is not provided, field name will be used as the key.

@BindExtra @JvmField var position: Int

override fun onRestoreInstanceState(savedInstanceState: Bundle) {
    super.onRestoreInstanceState(savedInstanceState)
    restoreStates(savedInstanceState)
}

Saving

Simply call Bundle.saveStates() to save states.

override fun onSaveInstanceState(outState: Bundle) {
   super.onSaveInstanceState(outState)
   saveStates(outState)
}

Supported extra types

  • primitive data types and array of them.
  • CharSequence, CharSequence[], and ArrayList<CharSequence>
  • String, String[], and ArrayList<String>
  • Parcelable, Parcelable[], ArrayList<Parcelable> and SparseArray<Parcelable>
  • Serializable

Parceler

[Parceler][parceler] is supported with this library, it is a library that easily makes any object implements Parcelable with generated code, making it able to be inserted to Bundle as Parcelable.

Bundler.wrap() automatically converts the object to Parcelable. Without Bundler.wrap(), object must be wrapped using Parcels.wrap(). Head to [Parceler doc][parceler] for more information.

User user = new User("Hendra Anggrian", 24);
// with Bundler.wrap()
intent.putExtras(Bundler.wrap(UserActivity.class, user));
// without Bundler.wrap()
intent.putExtra("user", Parcels.wrap(user));

Bundler.bind() automatically converts the Parcelable back to original object.

class UserActivity : Activity {
    @Extra("user") lateinit var user : User

    override fun onCreate(savedInstanceState : Bundle) {
        super.onCreate(savedInstanceState)
        bindExtras()
    }
}

Optional bindings

Extra bindings are required by default, an exception will be thrown if key is not found in Bundle. If this is not a desired behavior, annotate the field with @Nullable from [Android's support design library][support-annotations].

@Nullable @BindExtra String username;

auto-bundler's People

Contributors

hanggrian avatar

Stargazers

 avatar Víctor Manuel Pineda Murcia avatar  avatar

Watchers

James Cloos avatar  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.