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. 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
  2. INTENTS & EXTRAS NAVIGATION LAYER DART: CONSUMING INTENTS ANNOTATIONS BINDING

    SETUP HENSON: CREATING INTENTS INTENT BUILDERS FLUENT API ANNOTATIONS SETUP CONCLUSION THE TALK
  3. 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
  4. 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
  5. EXTRAS CAN BE USED TO PROVIDE EXTENDED INFORMATION INTENTS &

    EXTRAS 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. CONSUMING & CREATING INTENTS DEAL LIST 
 ACTIVITY DEAL DETAIL

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

    INTENT EXTRA 1 value 1 EXTRA 2 value 2 EXTRA 3 value 3 EXTRAS
  9. 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"); } ... }
  10. 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
  11. 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!!!
  12. 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); ... } }
  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 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. 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); ... } }
  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. 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
  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
  21. 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!!!
  22. 
 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); ... } }
  23. 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); ... } }
  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. 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
  26. 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
  27. 
 HENSON: FLUENT API Intent intent = Henson.with(context) .gotoDealDetailActivity() .dealId(dealId)

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

    public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... } }
  29. 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); ... } }
  30. 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
  31. 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