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

Fundamentals #10 - Architecture & Tying things up

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.

Britt Barak

January 08, 2018
Tweet

More Decks by Britt Barak

Other Decks in Technology

Transcript

  1. Our Goals - Write quicker - Change easily - Easy

    for others to understand - Rely on the code (testable) - Robust - Scalable
  2. class JellyBeanViewModel String flavor; int r; int g; int b;

    boolean isLoading; class JellyBean String flavor; String color;
  3. This are Patterns & Concepts - Many variants - Which

    should you use? - Are there others?
  4. public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { switch

    (seekBar.getId()) { case R.id.sb_r: viewModel.setR(progress); break; //...
  5. 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
  6. public class JellyBeanViewModel extends ViewModel { LiveData<Integer> r; public void

    setR(int newR) { newR = validate(newR); r.setValue(newR); }
  7. class JellyBeanViewModel String flavor; int r; int g; int b;

    class JellyBean String flavor; String color;
  8. - 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
  9. 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) }
  10. 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) }
  11. public class SaveJellyBean extends UseCase { public void execute(JellyBeanViewModel viewModel)

    { JellyBean data = prepareDataModel(viewModel); repo.saveBean(data); }
  12. Exercise 1. Android lifecycle-aware components goo.gl/92wWf3 2. For Sum Up:

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