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

Dart & Henson - Better Android Intents

Dart & Henson - Better Android Intents

Presented at Andevcon SF 2016, DroidKaigi Tokyo 2017 and Andevcon Washington 2017

Daniel Molinero Reguera

December 01, 2016
Tweet

More Decks by Daniel Molinero Reguera

Other Decks in Programming

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, BoundBox, … STEPHANE NICOLAS DANIEL MOLINERO REGUERA Android Dev @ Groupon
 Turbulence Ninja OSS: Dart, TP. +stephane @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. 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
  6. AN INTENT IS AN ABSTRACT DESCRIPTION OF AN OPERATION TO

    BE PERFORMED INTENTS & EXTRAS ACTIVITY from developer.android.com/reference/android/content/Intent.html INTENT
  7. EXTRAS CAN BE USED TO PROVIDE EXTENDED INFORMATION INTENTS &

    EXTRAS ACTIVITY from developer.android.com/reference/android/content/Intent.html INTENT EXTRAS
  8. 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
  9. NAVIGATION LAYER HOW TO ORGANIZE 
 YOUR NAVIGATION LAYER ?

  10. CONSUMING & CREATING INTENTS DEAL LIST 
 ACTIVITY DEAL DETAIL

    
 ACTIVITY CREATING AN INTENT CONSUMING AN INTENT
  11. None
  12. CONSUMING INTENTS

  13. DART CONSUMING INTENTS

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

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

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

    INTENT EXTRA 1 value 1 EXTRA 2 value 2 EXTRA 3 value 3 EXTRAS
  17. 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"); } ... }
  18. 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
  19. 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!!!
  20. DART: ANNOTATIONS public class DealDetailActivity extends Activity {
 @InjectExtra String

    dealId; @InjectExtra @Nullable Boolean shouldShowMap; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Dart.inject(this); ... } }
  21. 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); ... } }
  22. DART: BINDING public class DealDetailActivity extends Activity {
 @InjectExtra String

    dealId; @InjectExtra @Nullable Boolean shouldShowMap; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Dart.inject(this); ... } }
  23. 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); ... } }
  24. ACTIVITY MODEL String field1 int field2 boolean field3 DART: BINDING

    A MODEL
  25. ACTIVITY MODEL String field1 int field2 boolean field3 DART: BINDING

    A MODEL SEPARATE EXTRAS AND ACTIVITIES
  26. 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
  27. 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
  28. None
  29. CREATING INTENTS

  30. HENSON CREATING INTENTS

  31. 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
  32. 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
  33. 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
  34. 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!!!
  35. 
 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); ... } }
  36. 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); ... } }
  37. 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
  38. 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
  39. 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
  40. 
 HENSON: FLUENT API Intent intent = Henson.with(context) .gotoDealDetailActivity() .dealId(dealId)

    .shouldShowMap(true) .build();
  41. 
 HENSON: FLUENT API Intent intent = Henson.with(context) .gotoDealDetailActivity() .dealId(dealId)

    .shouldShowMap(true) .build(); HENSON CLASS PROVIDES A FLUENT API TO ACCESS INTENT BUILDERS
  42. HENSON: ANNOTATIONS @HensonNavigable public class SettingsActivity extends Activity {
 @Override

    public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... } }
  43. 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); ... } }
  44. 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
  45. 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
  46. CONCLUSION QUESTIONS ? COMMENTS ?

  47. CONCLUSION QUESTIONS ? COMMENTS ?

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