Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dart & Henson: Better Android Intents

Dart & Henson: Better Android Intents

Intents are an essential component of the Android ecosystem. They are used to express an action to be performed and can be classified into implicit and explicit intents. In an abstract way, all intents together define a navigation layer inside an application. Dart & Henson can generate all the code you need to create a clean navigation layer in your Android apps.

stephanenicolas

November 29, 2016
Tweet

More Decks by stephanenicolas

Other Decks in Programming

Transcript

  1. ABOUT US Senior Android Dev @ Groupon ~20 years of

    Java coding OSS: Dart, TP STEPHANE NICOLAS DANIEL MOLINERO REGUERA Android Dev @ Groupon Best level 3 ever OSS: Dart, +stephane)nicolas @D_Lemures
  2. INTENTS & EXTRAS NAVIGATION LAYER DART: CONSUMING INTENTS ANNOTATIONS BINDING

    SETUP HENSON: CREATING INTENTS INTENT BUILDERS FLUENT API ANNOTATIONS SETUP CONCLUSION THE TALK
  3. EXTRAS CAN BE USED TO PROVIDE EXTENDED INFORMATION AN INTENT

    IS AN ABSTRACT DESCRIPTION OF AN OPERATION TO BE PERFORMED INTENTS & EXTRAS BROADCAST
 RECEIVER SERVICE ACTIVITY from developer.android.com/reference/android/content/Intent.html INTENT EXTRAS
  4. NAVIGATION LAYER ACTIVITY 1 ACTIVITY 4 ACTIVITY 2 ALL INTENTS

    TOGETHER FORM THE NAVIGATION LAYER OF AN ANDROID APP ACTIVITY 3 ACTIVITY 5 ACTIVITY 6
  5. CONSUMING & CREATING INTENTS DEAL LIST 
 ACTIVITY DEAL DETAIL

    
 ACTIVITY CREATING AN INTENT CONSUMING AN INTENT
  6. DART: CONSUMING INTENTS ACTIVITY String field1 int field2 boolean field3

    INTENT EXTRA 1 value 1 EXTRA 2 value 2 EXTRA 3 value 3 EXTRAS
  7. DART: CONSUMING INTENTS public class DealDetailActivity extends Activity { public

    static final String EXTRA_DEAL_ID = "EXTRA_DEAL_ID"; public static final String EXTRA_SHOW_MAP = "EXTRA_SHOW_MAP"; 
 private String dealId; private boolean shouldShowMap; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); dealId = getIntent().getStringExtra(EXTRA_DEAL_ID); shouldShowMap = getIntent().getBooleanExtra(EXTRA_SHOW_MAP, false); if (dealId == null) { throw new IllegalArgumentException("Deal Id is required"); } ... }
  8. public class DealDetailActivity extends Activity { public static final String

    EXTRA_DEAL_ID = "EXTRA_DEAL_ID"; public static final String EXTRA_SHOW_MAP = "EXTRA_SHOW_MAP"; 
 private String dealId; private boolean shouldShowMap; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); dealId = getIntent().getStringExtra(EXTRA_DEAL_ID); shouldShowMap = getIntent().getBooleanExtra(EXTRA_SHOW_MAP, false); if (dealId == null) { throw new IllegalArgumentException("Deal Id is required"); } ... } DART: CONSUMING INTENTS BOILERPLATE!!!
  9. DART: ANNOTATIONS DECLARE REQUIRED AND OPTIONAL FIELDS public class DealDetailActivity

    extends Activity {
 @InjectExtra String dealId; @InjectExtra @Nullable Boolean shouldShowMap; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Dart.inject(this); ... } }
  10. DART: BINDING BIND INTENT EXTRAS TO ANNOTATED FIELDS public class

    DealDetailActivity extends Activity {
 @InjectExtra String dealId; @InjectExtra @Nullable Boolean shouldShowMap; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Dart.inject(this); ... } }
  11. public class DealDetailModel {
 @InjectExtra String dealId; @InjectExtra @Nullable Boolean

    shouldShowMap; } MODEL: public class DealDetailActivity extends Activity {
 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Dart.inject(detailModelInstance, this); ... } ACTIVITY: DART: BINDING A MODEL
  12. compile ‘com.f2prateek.dart:dart:2.0.1 provided ‘com.f2prateek.dart:dart-processor:2.0.1 GRADLE: <dependency> <groupId>com.f2prateek.dart</groupId> <artifactId>dart</artifactId> <version>2.0.1</version> </dependency>

    <dependency> <groupId>com.f2prateek.dart</groupId> <artifactId>dart-processor</artifactId> <version>2.0.1</version> <scope>provided</scope> </dependency> MAVEN: DART: SETUP
  13. public class CentralIntentFactory { public Intent newDealDetailActivityIntent(Context context, String dealId)

    {
 return new Intent(context, DealDetailActivity.class) .putExtra(EXTRA_DEAL_ID, dealId);
 }
 
 public Intent newDealDetailActivityIntent(Context context, 
 String dealId, 
 boolean showMap) {
 return new Intent(context, DealDetailActivity.class)
 .putExtra(EXTRA_DEAL_ID, dealId)
 .putExtra(EXTRA_SHOW_MAP, showMap);
 }
 ...
 } 
 HENSON: CREATING INTENTS - MEYER’S OPEN/CLOSED PRINCIPLE - BIG BALL OF MUD
  14. public class DealDetailActivity extends Activity { ... public static Intent

    getIntent(Context context, 
 String dealId) { 
 return new Intent(context, DealDetailActivity.class)
 .putExtra(EXTRA_DEAL_ID, dealId);
 } 
 
 public static Intent getIntent(Context context, 
 String dealId, 
 boolean showMap) { 
 return new Intent(context, DealDetailActivity.class)
 .putExtra(EXTRA_DEAL_ID, itemId)
 .putExtra(EXTRA_SHOW_MAP, showMap);
 } ...
 } 
 HENSON: CREATING INTENTS BOILERPLATE!!!
  15. DART & HENSON GENERATE THEM FOR YOU ! 
 HENSON:

    INTENT BUILDERS public class DealDetailActivity extends Activity {
 @InjectExtra String dealId; @InjectExtra @Nullable Boolean shouldShowMap; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Dart.inject(this); ... } }
  16. public class DealDetailActivity$$IntentBuilder {
 private Intent intent;
 private Bundler bundler

    = Bundler.create();
 
 public DealDetailActivity$$IntentBuilder(Context context) {
 intent = new Intent(context, DealDetailActivity.class);
 }
 
 public DealDetailActivity$$IntentBuilder dealId(String dealId) {
 bundler.put("dealId", dealId);
 return this;
 }
 
 public DealDetailActivity$$IntentBuilder shouldShowMap(Boolean shouldShowMap) {
 bundler.put(“shouldShowMap", shouldShowMap);
 return this;
 }
 
 public Intent build() {
 intent.putExtras(bundler.get());
 return intent;
 } } 
 HENSON: INTENT BUILDERS
  17. public class DealDetailActivity$$IntentBuilder {
 private Intent intent;
 private Bundler bundler

    = Bundler.create();
 
 public DealDetailActivity$$IntentBuilder(Context context) {
 intent = new Intent(context, DealDetailActivity.class);
 }
 
 public DealDetailActivity$$IntentBuilder dealId(String dealId) {
 bundler.put("dealId", dealId);
 return this;
 }
 
 public DealDetailActivity$$IntentBuilder shouldShowMap(Boolean shouldShowMap) {
 bundler.put(“shouldShowMap", shouldShowMap);
 return this;
 }
 
 public Intent build() {
 intent.putExtras(bundler.get());
 return intent;
 } } 
 HENSON: INTENT BUILDERS
  18. public class DealDetailActivity$$IntentBuilder {
 private Intent intent;
 private Bundler bundler

    = Bundler.create();
 
 public DealDetailActivity$$IntentBuilder(Context context) {
 intent = new Intent(context, DealDetailActivity.class);
 }
 
 public DealDetailActivity$$IntentBuilder dealId(String dealId) {
 bundler.put("dealId", dealId);
 return this;
 }
 
 public DealDetailActivity$$IntentBuilder shouldShowMap(Boolean shouldShowMap) {
 bundler.put(“shouldShowMap", shouldShowMap);
 return this;
 }
 
 public Intent build() {
 intent.putExtras(bundler.get());
 return intent;
 } } 
 HENSON: INTENT BUILDERS
  19. 
 HENSON: FLUENT API Intent intent = Henson.with(context) .gotoDealDetailActivity() .dealId(dealId)

    .shouldShowMap(true) .build(); HENSON CLASS PROVIDES A FLUENT API TO ACCESS INTENT BUILDERS
  20. HENSON: ANNOTATIONS MAKE YOUR ACTIVITIES VISIBLE TO HENSON EVEN IF

    THEY DON'T USE DART ANNOTATIONS @HensonNavigable public class SettingsActivity extends Activity {
 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... } }
  21. public class DealDetailModel {
 @InjectExtra String dealId; @InjectExtra @Nullable Boolean

    shouldShowMap; } MODEL: @HensonNavigable(model = DealDetailModel.class) public class DealDetailActivity extends Activity {
 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Dart.inject(detailModelInstance, this); ... } ACTIVITY: HENSON: ANNOTATIONS
  22. compile ‘com.f2prateek.dart:henson:2.0.1 provided ‘com.f2prateek.dart:henson-processor:2.0.1 GRADLE: <dependency> <groupId>com.f2prateek.dart</groupId> <artifactId>henson</artifactId> <version>2.0.1</version> </dependency>

    <dependency> <groupId>com.f2prateek.dart</groupId> <artifactId>henson-processor</artifactId> <version>2.0.1</version> <scope>provided</scope> </dependency> MAVEN: HENSON: SETUP