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

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

Britt Barak
November 09, 2017

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

Britt Barak

November 09, 2017
Tweet

More Decks by Britt Barak

Other Decks in Technology

Transcript

  1. “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. Players Exits and Entrances Plays a part → Single responsibility

    → Defined interfaces → Separation of concerns
  3. Our Goals - Write quicker - Change easily - Rely

    on the code (test) - Easy for others to understand
  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. class JellyBeanViewModel String flavor; int r; int g; int b;

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

    { JellyBean data = prepareDataModel(viewModel); repo.saveBean(data); }
  11. Room - Wraps SQLite database - Generates boilerplate code -

    Verifies queries at compile time - Denies calls on the UI thread
  12. @Dao public interface JellyBeanDao { @Query("select * from jellyBean") LiveData<List<JellyBean>>

    getAllJellyBeans(); @Query("select * from jellyBean where id = :beanId") LiveData<JellyBean> getJellyBean(String beanId); }
  13. @Database(version = 1, entities = {JellyBean.class}) public abstract class AppDatabase

    extends RoomDatabase { private static AppDatabase instance; public abstract JellyBeanDao jellyBeanDao(); //init and destroy code.. }
  14. public static AppDatabase getInMemoryDatabase(Context appContext){ if (instance == null) {

    instance = Room.inMemoryDatabaseBuilder( appContext, AppDatabase.class).build(); } return instance; } public static void destroyInstance() { instance = null; }
  15. Presentation Layer • View • ViewModel • Presenter Data Layer

    • Repository • DataModel Domain Layer • Interactor • Use case
  16. Players Exits and Entrances Plays a part → Single responsibility

    → Defined interfaces → Separation of concerns
  17. Our Goals - Write quicker - Change easily - Rely

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