Sunday, June 4, 2017

Highlights of Architecture Components - app and web (from google shared sessions) - Android

Highlights of Architecture Components - app and web (from google shared sessions)

Looking at the convergence of the mobile web and app framework - the idea behind having a consistent framework irrespective of the channel is becoming fundamentally possible via the concept of establishing a consistent architectural pattern and optimising performance on the overall channel - whether an app or web.

Here are some highlights to give an insight on the Android app aspects.

1) Recycler Views and Activity/Fragment life cycles - 
  • Recycler Views served best via APIs.
Optimisation of Activity/Fragment - life cycles 

Life Cycles form a core of the app.
  • Solve the right problems - fundaments are core.
  • Not throw the whole development out but try creating APIs which work well with other libraries or frameworks.
  • Layers on top of existing mechanisms to guide how an app should be built.
  • Focus on the scalability.
  • Easier for everyone to write applications by removing boiler point code.
New Libraries  - Architecture Components
  • Lifecyles - included.
  • Lifecycles - aware observables
  • Lightweight ViewModel
  • Object mapping library for db (for e.g sqlite)
Current Flow: Activity --> LocationListener --> onCreate --> initialise Context, onStart, onStop.

Changes to application might change --> onStart (for eg. user settings)

onStart --> checkUserStatus --> onStop (stop)

listenerStop --> onStop, onStart--> gets called after onStop which causes it to live forever and then drain resources.

Introducing --> LifeCycleOwner & LifeCycle Observer

Inside the activity --

New Flow: onCreate --> Initialise content, getLifeCycle --> location 

Check current state with the lifeCycle - then attach the activity with the action using this listener state.

Use annotations -- for e.g. annotate a method using @OnLifeCycleEvent(ON_START).

So removed all excess code by introducing --> LifeCycle Aware Component.

Share resources across multiple LifeCycles -- 

LiveData<T> - observable data holder, LifeCycle aware -- which helps to manage subscriptions
  • LocationListener extends LiveDatat  --> LocationListener (Context) -- Active Observer (whose lifecycle is started)
  • OnLocationChange(sending data back to the observer).
  • No activities and fragments inside it

Using live data with query - while user rotates the phone (giving back new activity same view model)

webService.fetchUser triggered --> activity rotated --> again fetch called (problem)

Solution: 
  • put data inside the activities which extends viewModel
  • inside getUser method --> first call get from web service else return the value
  • inside activity --> get view model provider of this.
new activity started --> gets the view model --> when finished --> view model not needed.

Never reference views - RxJava.

Persistence 

APIs on android --> Content providers, shared preferences, SQLite

Lot of program for getting data.

Use annotation based queries --> concept of DAO - data access object.

Interface FeedDao --> do load, inserts deleteAll, updateAll.

Supports Rx Java 2.

Compile time validation.

Intensive code migration

Google updates - Guide to App Architecture --> Develop --> Architecture Components - reference 
guide.

UI controllers - activités & fragments
  • observables
  • keep UI up
  • forwards user actions back to the view model
View model
  • prepares data for the UI
  • includes LiveData, Observables
  • survides configuration changes
  • gateway for UI controller
Repository 
  • saves as data store for all of applications
  • provide simple data modifications
  • co-ordinates fetching, syncing, persisting
Data Source 
API data sources --
retrofit, cloud other OS.

User --> LiveData<User> --> UserRepository (What's the benefit -- testing?)

Put the code in the viewModel --> create the mock ViewModel to test the UI

Replace with mockRepository and it will work.

Can use a JUnit test.

Google Resources --> developer.android.com/arch