Code Monkey home page Code Monkey logo

wagon's Introduction

Wagon

Makes passing extras and saving/loading preferences clean and easy via annotations.

How To Use:

Put the wagon_x.xx.jar in the Android 'libs' folder of your project. If 'libs' doesn't exist, create it at the same level as 'src'.

Examples: ([See wiki] (https://github.com/beplaya/Wagon/wiki))

Include

Gradle

compile 'james.a.grant:wagon:1.1.0'

Use

Annotate which fields you'd like to put in the wagon:

  	public class MainActivity extends Activity {
	// All fields in crates will be copied to
	// another instance in the next activity
	@Crate(key = "theCrate")
	public CrateExample crateExample = new CrateExample();
	
	@WoodBox(key = "theString")
	public String sTRING = "I'm a string";
	@WoodBox(key = "theList")
	public ArrayList<String> lIST;
	//...
}

Pack your wagon and start the next activity:

	//...
	private void startNextAcitivity() {
		Wagon<MainActivity> wagon = new Wagon<MainActivity>(this.getClass(), this);//this==MainActivity
		Intent intent = new Intent(getApplicationContext(), OtherActivity.class);
		wagon.pack(intent);
		startActivity(intent);
	}
	//...

Unpack your wagon:

public class OtherActivity extends Activity {

	//The crate from the last activity
	//will be copied into this crate instance
	//with the same field values
	@Crate(key = "theCrate")
	public CrateExample crateExample = new CrateExample();

	@WoodBox(key = "theList")
	public ArrayList<String> lIST = new ArrayList<String>();
	@WoodBox(key = "theString")
	public String sTRING = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		final Wagon<OtherActivity> wagon = new Wagon<OtherActivity>(this.getClass(), this);
		wagon.unpack(getIntent());
	}
	//...
}

CrateExample class used above

public class CrateExample {

	public ArrayList<String> theListInCrate;

	public String theStringInCrate;
	public int numberInt;

	public CrateExample() {
		this(new ArrayList<String>(), "", 0);
	}

	public CrateExample(ArrayList<String> l, String s, int number) {
		this.theListInCrate = l;
		this.theStringInCrate = s;
		this.numberInt = number;
	}
	//...

}

Nesting crates

You may nest crates within other crates without limit. Simply annotate each Object field in the class as @Crate.
There is no reason to use a WoodBox within a Crate. Any field within a Crate is already considered a WoodBox.
E.G.
	public class MainActivity extends Activity {
	@WoodBox(key = "aBox")
	public void float value = 42;

	@Crate(key = "aCrate")
	public CrateExample crateExample;
	//...
public class CrateExample {

	//No need to annotate as WoodBox.  Already considered one.
	public void int number = 77;
	//No need to annotate as WoodBox.  Already considered one.
	public ArrayList<String> theListInCrate;

	@Crate(key = "aNestedCrate")
	public NestedCrateExample nestedCrate;
	//...
}
public class NestedCrateExample {

	//No need to annotate as WoodBox.  Already considered one.
	public float theFloat;
	//No need to annotate as WoodBox.  Already considered one.
	public long theLong;

	public NestedCrateExample() {
	}

	public NestedCrateExample(float f, long l) {
		this.theFloat = f;
		this.theLong = l;
	}
	//...
}

SharedPreferences

Just like extras, Wagon can pass annotated fields to SharedPreferences:
	...
	@Crate(key = "myPreferenceCrate", preference = true)
	public CrateExample crate;
	@WoodBox(key = "myPreferenceString", preference = true)
	public String value;
	...
	wagon.pack(getSharedPreferences(PREFERENCES_NAME, MODE_PRIVATE));
	...
	wagon.unpack(getSharedPreferences(PREFERENCES_NAME, MODE_PRIVATE));
	...

wagon's People

Contributors

beplaya avatar bryant1410 avatar cah-grantjames 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.