Fundamentals #10 - Architecture & Tying things up

3142db3adb711e247e371153b5777e04?s=47 Britt Barak
January 08, 2018

Fundamentals #10 - Architecture & Tying things up

Closing talk on Android fundamentals course: architecture design patterns and suggested implementation, with new Android Architecture Components.

3142db3adb711e247e371153b5777e04?s=128

Britt Barak

January 08, 2018
Tweet

Transcript

  1. 4.

    Our Goals - Write quicker - Change easily - Easy

    for others to understand - Rely on the code (testable) - Robust - Scalable
  2. 6.
  3. 7.
  4. 8.
  5. 9.
  6. 11.
  7. 17.

    class JellyBeanViewModel String flavor; int r; int g; int b;

    boolean isLoading; class JellyBean String flavor; String color;
  8. 23.
  9. 42.

    This are Patterns & Concepts - Many variants - Which

    should you use? - Are there others?
  10. 44.
  11. 46.
  12. 53.

    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { switch

    (seekBar.getId()) { case R.id.sb_r: viewModel.setR(progress); break; //...
  13. 56.

    LiveData - Observable data holder class - Lifecycle-aware - Updates

    only on started / resumed state - Removes itself on destroyed state - No memory leaks https://developer.android.com/topic/libraries/architecture/livedata.html
  14. 58.

    public class JellyBeanViewModel extends ViewModel { LiveData<Integer> r; public void

    setR(int newR) { newR = validate(newR); r.setValue(newR); }
  15. 66.
  16. 71.

    class JellyBeanViewModel String flavor; int r; int g; int b;

    class JellyBean String flavor; String color;
  17. 72.

    - One per data type (e.g jelly bean, recipe, user….).

    - Encapsulates the logic of getting/setting the data. - CRUD operations (Create, Read, Update, Delete) Repository
  18. 77.

    LiveData<JellyBean> getJellyBean(String id) { if (cache.hasJellyBean(id)){ return cache.getJellyBean(id); } else

    if (appDatabase.hasJellyBean(id)) { return appDatabase.jellyBeanDao().getJellyBean(id); } else{ return myFirebaseClient.getJellyBean(id) }
  19. 78.

    LiveData<JellyBean> getJellyBean(String id) { if (cache.hasJellyBean(id)){ return cache.getJellyBean(id); } else

    if (appDatabase.hasJellyBean(id)) { return appDatabase.jellyBeanDao().getJellyBean(id); } else{ return myApiClient.getJellyBean(id) }
  20. 88.

    public class SaveJellyBean extends UseCase { public void execute(JellyBeanViewModel viewModel)

    { JellyBean data = prepareDataModel(viewModel); repo.saveBean(data); }
  21. 97.
  22. 98.

    Exercise 1. Android lifecycle-aware components goo.gl/92wWf3 2. For Sum Up:

    Build an App with Architecture Components goo.gl/5og51s