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.

1f9cd9b9bad9aa6115a1ed72433c180d?s=128

stephanenicolas

November 29, 2016
Tweet

Transcript

  1. BETTER ANDROID INTENTS DART & HENSON https://github.com/f2prateek/dart

  2. 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
  3. 5IF"OESPJE5FBNJT)JSJOH
 jobs.groupon.com/careers/

  4. INTENTS & EXTRAS NAVIGATION LAYER DART: CONSUMING INTENTS ANNOTATIONS BINDING

    SETUP HENSON: CREATING INTENTS INTENT BUILDERS FLUENT API ANNOTATIONS SETUP CONCLUSION THE TALK
  5. 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
  6. 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
  7. NAVIGATION LAYER HOW TO ORGANIZE 
 YOUR NAVIGATION LAYER ?

  8. CONSUMING & CREATING INTENTS DEAL LIST 
 ACTIVITY DEAL DETAIL

    
 ACTIVITY CREATING AN INTENT CONSUMING AN INTENT
  9. DART CONSUMING 
 INTENTS

  10. DART: CONSUMING INTENTS ACTIVITY String field1 int field2 boolean field3

    INTENT EXTRA 1 value 1 EXTRA 2 value 2 EXTRA 3 value 3 EXTRAS
  11. 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"); } ... }
  12. 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!!!
  13. 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); ... } }
  14. 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); ... } }
  15. ACTIVITY MODEL String field1 int field2 boolean field3 DART: BINDING

    A MODEL SEPARATE EXTRAS AND ACTIVITIES
  16. 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
  17. 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
  18. HENSON CREATING INTENTS

  19. 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
  20. 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!!!
  21. 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); ... } }
  22. 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
  23. 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
  24. 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
  25. 
 HENSON: FLUENT API Intent intent = Henson.with(context) .gotoDealDetailActivity() .dealId(dealId)

    .shouldShowMap(true) .build(); HENSON CLASS PROVIDES A FLUENT API TO ACCESS INTENT BUILDERS
  26. 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); ... } }
  27. ACTIVITY MODEL String field1 int field2 boolean field3 HENSON: BINDING

    A MODEL SEPARATE EXTRAS AND ACTIVITIES
  28. 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
  29. 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
  30. CONCLUSION, QUESTIONS ?

  31. BETTER ANDROID INTENTS DART & HENSON https://github.com/f2prateek/dart +stephane)nicolas @D_Lemures