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

Bundle Injection with Dagger

Bundle Injection with Dagger

See blog post: “Bundle Injection with Dagger” https://medium.com/@oldergod/bundle-injection-with-dagger-fe98b59f1cc6

Benoît Quenaudon

September 18, 2017
Tweet

More Decks by Benoît Quenaudon

Other Decks in Programming

Transcript

  1. Bundle injection
    with Dagger
    Benoît Quenaudon @oldergod

    View full-size slide

  2. public class InputActivity extends Activity {
    @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_input);
    final EditText editText = findViewById(R.id.edit_text);
    }a
    }b

    View full-size slide

  3. public class InputActivity extends Activity {
    @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_input);
    final EditText editText = findViewById(R.id.edit_text);
    findViewById(R.id.button).setOnClickListener(v -> {
    });
    }a
    }b

    View full-size slide

  4. public class InputActivity extends Activity {
    @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_input);
    final EditText editText = findViewById(R.id.edit_text);
    findViewById(R.id.button).setOnClickListener(v -> {
    Intent intent = new Intent(InputActivity.this, OutputActivity.class);
    intent.putExtra(EXTRA_SOME_INPUT, editText.getText().toString());
    });
    }a
    }b

    View full-size slide

  5. public class InputActivity extends Activity {
    @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_input);
    final EditText editText = findViewById(R.id.edit_text);
    findViewById(R.id.button).setOnClickListener(v -> {
    Intent intent = new Intent(InputActivity.this, OutputActivity.class);
    intent.putExtra(EXTRA_SOME_INPUT, editText.getText().toString());
    InputActivity.this.startActivity(intent);
    });
    }a
    }b

    View full-size slide

  6. public class InputActivity extends Activity {
    @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_input);
    final EditText editText = findViewById(R.id.edit_text);
    findViewById(R.id.button).setOnClickListener(v -> {
    Intent intent = new Intent(InputActivity.this, OutputActivity.class);
    intent.putExtra(EXTRA_SOME_INPUT, editText.getText().toString());
    InputActivity.this.startActivity(intent);
    });
    }a
    }b

    View full-size slide

  7. public class OutputActivity extends Activity {
    OutputPresenter presenter;
    @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    }a
    }c

    View full-size slide

  8. public class OutputActivity extends Activity {
    OutputPresenter presenter;
    @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String inputText = getIntent().getStringExtra(EXTRA_SOME_INPUT);
    presenter = new OutputPresenter(inputText);
    }a
    }c

    View full-size slide

  9. public class OutputActivity extends Activity {
    @Inject OutputPresenter presenter;
    @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String inputText = getIntent().getStringExtra(EXTRA_SOME_INPUT);
    }a
    }c

    View full-size slide

  10. public class OutputActivity extends Activity {
    @Inject OutputPresenter presenter;
    @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String inputText = getIntent().getStringExtra(EXTRA_SOME_INPUT);
    presenter.setInputText(inputText);
    }a
    }c

    View full-size slide

  11. public class OutputActivity extends Activity {
    @Inject OutputPresenter presenter;
    @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String inputText = getIntent().getStringExtra(EXTRA_SOME_INPUT);
    presenter.setInputText(inputText);
    }a
    @Override protected void onStart() {
    super.onStart();
    presenter.onStart();
    }
    }c

    View full-size slide

  12. public class BundleService {
    private final Bundle data;
    public BundleService() {
    data = new Bundle();
    }c
    public Object get(String key) {
    return data.get(key);
    }d
    public Bundle getAll() {
    return data;
    }f
    }e

    View full-size slide

  13. public class BundleService {
    private final Bundle data;
    public BundleService(@Nullable Bundle savedState) {
    data = new Bundle();
    if (savedState != null) {
    data.putAll(savedState);
    }a
    }c
    public Object get(String key) {
    return data.get(key);
    }d
    public Bundle getAll() {
    return data;
    }f
    }e

    View full-size slide

  14. public class BundleService {
    private final Bundle data;
    public BundleService(@Nullable Bundle savedState, @Nullable Bundle intentExtras) {
    data = new Bundle();
    if (savedState != null) {
    data.putAll(savedState);
    }a
    if (intentExtras != null) {
    data.putAll(intentExtras);
    }b
    }c
    public Object get(String key) {
    return data.get(key);
    }d
    public Bundle getAll() {
    return data;
    }f
    }e

    View full-size slide

  15. public class BundleService {
    private final Bundle data;
    public BundleService(@Nullable Bundle savedState, @Nullable Bundle intentExtras) {
    data = new Bundle();
    if (savedState != null) {
    data.putAll(savedState);
    }a
    if (intentExtras != null) {
    data.putAll(intentExtras);
    }b
    }c
    public Object get(String key) {
    return data.get(key);
    }d
    public Bundle getAll() {
    return data;
    }f
    }e

    View full-size slide

  16. public abstract class BaseActivity extends Activity {
    private BundleService bundleService;
    @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    bundleService = new BundleService(savedInstanceState, getIntent().getExtras());
    super.onCreate(savedInstanceState);
    }a
    }d

    View full-size slide

  17. public abstract class BaseActivity extends Activity {
    private BundleService bundleService;
    @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    bundleService = new BundleService(savedInstanceState, getIntent().getExtras());
    super.onCreate(savedInstanceState);
    }a
    public BundleService getBundleService() {
    return bundleService;
    }b
    }d

    View full-size slide

  18. public abstract class BaseActivity extends Activity {
    private BundleService bundleService;
    @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    bundleService = new BundleService(savedInstanceState, getIntent().getExtras());
    super.onCreate(savedInstanceState);
    }a
    public BundleService getBundleService() {
    return bundleService;
    }b
    @Override protected void onSaveInstanceState(Bundle outState) {
    outState.putAll(bundleService.getAll());
    super.onSaveInstanceState(outState);
    }c
    }d

    View full-size slide

  19. public abstract class BaseActivity extends Activity {
    private BundleService bundleService;
    @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    bundleService = new BundleService(savedInstanceState, getIntent().getExtras());
    super.onCreate(savedInstanceState);
    }a
    public BundleService getBundleService() {
    return bundleService;
    }b
    @Override protected void onSaveInstanceState(Bundle outState) {
    outState.putAll(bundleService.getAll());
    super.onSaveInstanceState(outState);
    }c
    }d

    View full-size slide

  20. @Module public abstract class BundleModule {
    @Provides static BundleService provideBundleService(BaseActivity activity) {
    return activity.getBundleService();
    }
    }

    View full-size slide

  21. @Qualifier @Retention(RUNTIME)
    public @interface SomeInput {}a

    View full-size slide

  22. @Qualifier @Retention(RUNTIME)
    public @interface SomeInput {}a
    @Module public abstract class BundleModule {
    public static final String EXTRA_SOME_INPUT = "SOME_INPUT";
    @Provides static BundleService provideBundleService(BaseActivity activity) {
    return activity.getBundleService();
    }
    @Provides @SomeInput static String provideSomeInput(BundleService bundleService) {
    String style = (String) bundleService.get(EXTRA_SOME_INPUT);
    return style == null ? "Null" : style;
    }
    }

    View full-size slide

  23. class OutputPresenter {
    private final String inputText;
    OutputPresenter(String inputText) {
    this.inputText = inputText;
    }a
    void onStart() {
    repository.doSomeWork(inputText);
    }b
    }c

    View full-size slide

  24. class OutputPresenter {
    private final String inputText;
    @Inject OutputPresenter(String inputText) {
    this.inputText = inputText;
    }a
    void onStart() {
    repository.doSomeWork(inputText);
    }b
    }c

    View full-size slide

  25. class OutputPresenter {
    private final String inputText;
    @Inject OutputPresenter(@SomeInput String inputText) {
    this.inputText = inputText;
    }a
    void onStart() {
    repository.doSomeWork(inputText);
    }b
    }c

    View full-size slide

  26. class OutputPresenter {
    private final String inputText;
    @Inject OutputPresenter(@SomeInput String inputText) {
    this.inputText = inputText;
    }a
    void onStart() {
    repository.doSomeWork(inputText);
    }b
    }c

    View full-size slide

  27. public class OutputActivity extends BaseActivity {
    OutputPresenter presenter;
    @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_output);
    String inputText = getIntent().getStringExtra(EXTRA_SOME_INPUT);
    presenter = new OutputPresenter(inputText);
    }a
    @Override protected void onStart() {
    super.onStart();
    presenter.onStart();
    }b
    }c

    View full-size slide

  28. public class OutputActivity extends BaseActivity {
    OutputPresenter presenter;
    @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_output);
    String inputText = getIntent().getStringExtra(EXTRA_SOME_INPUT);
    presenter = new OutputPresenter(inputText);
    }a
    @Override protected void onStart() {
    super.onStart();
    presenter.onStart();
    }b
    }c

    View full-size slide

  29. public class OutputActivity extends BaseActivity {
    @Inject OutputPresenter presenter;
    @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_output);
    String inputText = getIntent().getStringExtra(EXTRA_SOME_INPUT);
    presenter = new OutputPresenter(inputText);
    }a
    @Override protected void onStart() {
    super.onStart();
    presenter.onStart();
    }b
    }c

    View full-size slide

  30. public class OutputActivity extends BaseActivity {
    @Inject OutputPresenter presenter;
    @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_output);
    }a
    @Override protected void onStart() {
    super.onStart();
    presenter.onStart();
    }b
    }c

    View full-size slide

  31. public class OutputActivity extends BaseActivity {
    @Inject OutputPresenter presenter;
    @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_output);
    }a
    @Override protected void onStart() {
    super.onStart();
    presenter.onStart();
    }b
    }c

    View full-size slide

  32. Fin
    Benoît Quenaudon @oldergod

    View full-size slide