Data binding

Preview:

Citation preview

Data Binding

Who we are?

Leonid Olevsky

leonid.olevsky@gmail.com

Yonatan Levin

levinyon@gmail.com

Android Academy

TodayfindViewByIdCoordinates between the model and the views

Create untestable codeAnd more...

What is Data Binding?Model View ViewModel (MVVM)

Ongoing link between an element in the user interface and a value

When a variable’s value is updated, the display on the user’s screen changes automatically

DependenciesIt's a support library - Android 2.1 (API level 7+)

Gradle 1.3.0-beta4 or higher is required

Beta release

DependenciesIn a top-level build.gradle add

In each module

Simple usage - Binding Data

Simple usage - Binding Data

Simple usage - Binding DataThe magic

Other option

Simple usage - Binding Events

More optionsUse imports

Alies

More optionsReferencing static fields and methods in expressions

More options - include

Expression LanguageMathematical + - / * %String concatenation +Logical && ||Binary & | ^Unary + - ! ~Shift >> >>> <<Comparison == > < >=

<=instanceofGrouping ()

Literals - character, String, numeric, null

CastMethod callsField accessArray access []Ternary operator ?:

Expression LanguageNot supported

thissupernewExplicit generic invocation

ObservableObservable Objects

listen for changes of all properties on that object

ObservableFieldslisten for changes of a field of that object

Observable Collectionslisten for changes on dynamic structures to

hold data

Observable Objects

ObservableFields

Observable CollectionsObservableArrayMap ObservableArrayList

Further Readingdeveloper.android.com/tools/data-binding/guide.html

Our app

ArchitectureStarwarsActivi

ty

CrawlLoader OpeningCrawl

mCrawlLoader.startLoading()

new OpeningCrawl

onLoadFinishListener.onLoadFinish(openingCrawl)

Every ~5 seconds

findViewById(R.id.asw_tv_episode);findViewById(R.id.asw_tv_crawl);

mEpisodeTextView.setText(...);mCrawlTextView.setText(...);mCrawlTextView.startAnimation();

Data Binding could be the mess

Downside- Define stuff in XML- Not debuggable- XML not unit tested- Run-time errors instead compile time- Bad IDE support

MVP - The Best Simple Friend

Why MVP?- Decoupled layers

- Easy to debug

- Fully testable business logic, data model

MVP vs DataBindingPresenter react on what happen in the view, retrieves data, formats and display back in the viewData Binding will take over the main responsibilities of the presenter (react and display). Rest - will go to the enhanced Model - ViewModel

MVVM

https://en.wikipedia.org/wiki/Model_View_ViewModel

MVVM ArchitectureStarwarsActivity

CrawlLoader OpeningCrawl

mCrawlLoader.startLoading()

new OpeningCrawl

onLoadFinishListener.onLoadFinish(openingCrawl)

Every ~5 seconds

StarWarsViewModel

Data Binding

Code

https://github.com/parahall/StarWarsDataBinding.git

Layout - ActivityStarWars.xml

Layout - ActivityStarWars.xml

StarWarsActivity

StarWarsActivity - no leak

StarWarsActivity - callbacks

StarWarsViewModel

StarWarsViewModel - react

StarWarsViewModel - retrieve

StarWarsViewModel - decide

* runOnUiThread - might be avoided using OttoBus event

CrawlLoader

So what we got?

Clean Debuggable codeDecoupled layerFully testable UI logic

Fun :)

Q?

Thank you!

Leonid Olevsky

leonid.olevsky@gmail.com

Yonatan Levin

levinyon@gmail.com

Android Academy