Android MVVM

14

Click here to load reader

Transcript of Android MVVM

Page 1: Android MVVM

Android MVVM

Vinícius T. Sossella

Page 2: Android MVVM

MVVM

Model-View-View Model (MVVM) is a design pattern for building user interfaces

Was created in 2005 by John Gossman, the Microsoft WPF and Silverlight architect

Similar to MVC and MVP, we can say that MVVM is an MVP specialization.

https://blogs.msdn.microsoft.com/johngossman/2005/10/08/introduction-to-modelviewviewmodel-pattern-for-building-wpf-apps/

Page 3: Android MVVM

MVVM

View - The view is responsible for defining the structure, layout, and appearance of what the user sees on the screen.

Model - Encapsulates the application's business logic and data.

View Model - The view model acts as an intermediary between the view and the model, and is responsible for handling the view logic. Typically, the view model interacts with the model by invoking methods in the model classes. The view model retrieves data from the model and then makes the data available to the view, and may reformat the data in some way that makes it simpler for the view to handle.

Page 4: Android MVVM

Advantages

- Helps you to cleanly separate the business and presentation logic of your application from its user interface (UI)- Unlike the Presenter in MVP, a ViewModel does not need a reference to a view. The view binds to properties on a ViewModel, which,

in turn, exposes data contained in model objects and other state specific to the view. - Easier to test- Improve code re-use opportunities

'you can test it without awkward UI automation and interaction. If you’ve ever tried to unit test UI code, you know how hard that can be.' JohnGossmanMarch 4, 2006

Disadvantages

- Bindings (but wait android did it well)- In bigger cases, it can be hard to design the ViewModel

MVVM

Page 5: Android MVVM

Do we need all this stupid code every time?

Page 6: Android MVVM

Did I study for this??!!

Page 7: Android MVVM

Android Data Binding

https://developer.android.com/topic/libraries/data-binding/index.html

Page 8: Android MVVM

Android Data Binding

- Announced at Google I/O 2015

- Allows you to write declarative layouts and minimize the glue code necessary to bind your data and layouts

- Allows you to bind values of an object directly to views in your layout, and if the object changes, the view is updated automatically

- By binding properties of a view to a ViewModel, you get loose coupling between the two and entirely remove the need for writing code in a ViewModel that directly updates a view.

Page 9: Android MVVM

Disavantages

- Bindings (but wait android did it well)

The binding objects are created at compile time. All other databinding needed refletion, but not the new one (Android Data Binding)

MVVM

Page 10: Android MVVM

Android Data Binding

- How to use?

Add to Gradle file

Page 11: Android MVVM

Android Data Binding

Page 12: Android MVVM

Android Data Binding

Page 13: Android MVVM

Android Data Binding

Page 14: Android MVVM

ENDVinicius Tonial Sossella

[email protected]