grenade


Source link: https://github.com/kobakei/grenade

Grenade

Grenade is annotation based intent builder for activities and services. By using this library, you can build Intent with extras and retrieve extras by type safe way and less code.

This library is strongly inspired by emilsjolander/IntentBuilder but some advanced features are added.

Download

Project build.gradle

allprojects {

  repositories {

...

maven {
 url "https://jitpack.io" 
}

  
}
 
}

App build.gradle

dependencies {

  ...
  annotationProcessor 'com.github.kobakei.grenade:processor:LATEST_VERSION'
  compile 'com.github.kobakei.grenade:library:LATEST_VERSION' 
}

LATEST_VERSION is

NOTE: if you use Android Gradle Plugin before 2.2.0, you must use android-apt plugin instead of annotationProcessor configuration.

Basic usage

Add @Navigator annotation to activity and @Extra annotation to fields.

@Navigator public class DetailActivity extends AppCompatActivity {

// Required params
  @Extra
  String foo;
  @Extra
  int bar;

// Optional params
  @Extra @Optional
  String hoge;
  @Extra @Optional
  boolean fuga;

@Override
  protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

  
}
 
}

Once you build, DetailActivityNavigator will be generated. Building intent to launch DetailActivity is as below.

// Build intent and start activity startActivity(new DetailActivityNavigator(foo, bar)
  .hoge(hoge)
  .fuga(fuga)
  .flags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
  .build(context);

And handling intent is as below.

@Navigator public class DetailActivity extends AppCompatActivity {

...

@Override
  protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

 // Inject fields

DetailActivityNavigator.inject(this, getIntent());

  
}
 
}

Of course, you can use Grenade to start Service and BroadcastReceiver as same way.

@Navigator public class MyIntentService extends IntentService{

@Extra
  String foo;
  @Extra
  String baz;

@Override
  protected void onHandleIntent(Intent intent) {

 MyIntentServiceNavigator.inject(this, intent);

  
}
 
}
  // Code to start service startService(new MyIntentServiceNavigator("foo", "baz").build(this));

Multiple constructors

By specifying fields in @Navigator annotation, multiple constructors with different set of required params will be generated.

@Navigator({

  "foo,bar1",
  "foo,bar2" 
}
) public class DetailActivity extends AppCompatActivity {

  @Extra
  String foo;
  @Extra
  int bar1;
  @Extra
  long bar2;

... 
}

You can use them as below.

startActivity(new DetailActivityNavigator(foo, bar1)
  .build(context));
 startActivity(new DetailActivityNavigator(foo, bar2)
  .build(context));

Handling onActivityResult

Setting Intent object to onActivityResult has the same problem as above. Grenade offers APIs to build Intent to pass setResult and to handle onActivityResult as type safe way.

As an example, let's think about handling DetailActivity's result in MasterActivity.

At first, in MasterActivity, add method to handle result of DetailActivity with @OnActivityResult annotation. Moreover, call MasterActivityNavigator.onActivityResult in onActivityResult.

@OnActivityResult(requestCode = REQ_CODE_DETAIL1, resultCodes = {
Activity.RESULT_OK
}
) void onDetailOk(String foo, int bar) {

  Toast.makeText(this, "Detail OK", Toast.LENGTH_SHORT).show();
 
}
  @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {

  super.onActivityResult(requestCode, resultCode, data);

  MainActivityNavigator.onActivityResult(this, requestCode, resultCode, data);
 
}

In DetailActivity, setting result is as below:

setResult(RESULT_OK, MainActivityNavigator.resultForOnDetailOk("Hello", 123));
 finish();

Once you build, putting extras and getting extras will be done in MainActivityNavigator.

Built-in support of Parceler

Parceler is a famous library to generate Parcelable reader/writer. Grenade offers build-in support of Parceler. When field type has @Parcel annotation, Grenade will wrap/unwrap an entity with Parceler. To install Parceler to your project, please read Parceler's README.

// Entity class with @Parcel annotation @Parcel public class User {

  ... 
}
@Navigator public class DetailActivity extends AppCompatActivity {

  // Use parcelable entity as field
  @Extra
  User user;
  ... 
}
startActivity(new DetailActivityNavigator(new User())
  .build(context));

ProGuard

If you use Grenade with Parceler, ProGuard setting of Parceler is needed.

License

Copyright 2016 Keisuke Kobayashi  Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 

Resources

RetroFacebook turns Facebook API into a Java interface using RxJava. Easy to add API and model.

TimeRangePicker is a library which can be used to select a time range.

This library provides a lightweight mechanism for adding functionality to Activities, Fragments and Applications without modifying their source code, by using annotations.

Objects extending from AspectActivity, AspectFragmentActivity, AspectFragment, AspectSupportFragment and AspectApplication, can apply the Aspects annotation and provide a list of classes, called aspects, that add functionality at runtime.

A plugin that allows you to control, simply, all your Genymotion devices from your Gradle scripts.

Android calendar view inspired by Sunrise calendar and iOS7 stock calendar.

A customizable view that provisions picking of a date, time & recurrence option, all from a single user-interface.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes