Clean, Easy & New Android Arch. : Devoxx-BE 17

3142db3adb711e247e371153b5777e04?s=47 Britt Barak
November 09, 2017

Clean, Easy & New Android Arch. : Devoxx-BE 17

3142db3adb711e247e371153b5777e04?s=128

Britt Barak

November 09, 2017
Tweet

Transcript

  1. 6.

    “As You Like It” / W. Shakespeare ״All the world’s

    a stage, And all the men and women merely players; They have their exits and their entrances, And one man in his time plays many parts:״
  2. 7.

    Players Exits and Entrances Plays a part → Single responsibility

    → Defined interfaces → Separation of concerns
  3. 8.

    Our Goals - Write quicker - Change easily - Rely

    on the code (test) - Easy for others to understand
  4. 11.
  5. 12.
  6. 13.
  7. 14.
  8. 17.
  9. 25.

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

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

    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
  11. 30.
  12. 37.
  13. 44.

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

    class JellyBean String flavor; String color;
  14. 45.

    - 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
  15. 50.

    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) }
  16. 51.

    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) }
  17. 62.

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

    { JellyBean data = prepareDataModel(viewModel); repo.saveBean(data); }
  18. 70.

    Room - Wraps SQLite database - Generates boilerplate code -

    Verifies queries at compile time - Denies calls on the UI thread
  19. 74.

    @Dao public interface JellyBeanDao { @Query("select * from jellyBean") LiveData<List<JellyBean>>

    getAllJellyBeans(); @Query("select * from jellyBean where id = :beanId") LiveData<JellyBean> getJellyBean(String beanId); }
  20. 77.

    @Database(version = 1, entities = {JellyBean.class}) public abstract class AppDatabase

    extends RoomDatabase { private static AppDatabase instance; public abstract JellyBeanDao jellyBeanDao(); //init and destroy code.. }
  21. 78.

    public static AppDatabase getInMemoryDatabase(Context appContext){ if (instance == null) {

    instance = Room.inMemoryDatabaseBuilder( appContext, AppDatabase.class).build(); } return instance; } public static void destroyInstance() { instance = null; }
  22. 82.

    Presentation Layer • View • ViewModel • Presenter Data Layer

    • Repository • DataModel Domain Layer • Interactor • Use case
  23. 83.

    Players Exits and Entrances Plays a part → Single responsibility

    → Defined interfaces → Separation of concerns
  24. 84.

    Our Goals - Write quicker - Change easily - Rely

    on the code (test) - Easy for others to understand
  25. 85.