+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
MVVM with DataBinding on android
1.
2. MVVM + Data Binding
on Android
Made (with love) by Rodrigo Bresan
3. What is MVVM?
- Architectural pattern (such as MVC, MVP, VIPER, etc)
- Decouple business logic from user interface
- Model (data) - View (UI) - ViewModel (logic and state)
Data binding
Activities/Fragments/XML
Presentation logic
- State + Behaviour
- Interaction with
Busines/External
Business/Data
4. Why use it?
- Decouple business logic from user interface
- Abstract Android APIs from business logic
- Better testability
- Follows SOLID principles
5. Traditional way
- Single activity with many responsibilities:
- Handling state and behaviour of screen (UI widgets);
- Communicating with external world (models);
- Testing becomes harder, due to high being coupled with Android
APIs
6. Layers
- View
- just UI (components) that displays the data to user
- Model
- Well, data :-)
- ViewModel
- Logic that should be applied on data for being presented
(raw data to friendly)
- Usage of Observable pattern
7. View
- Working with Android APIs;
- Initializing/Destroying activities;
- Handling toasts, snackbars, dialogs, menus;
- Handling permissions;
8. View Model
- Handling view states (no connection, empty content, load
content)
- Visibility
- Received extras and arguments
- Input validations
- Fetching data from model
- Uses the application context (since we would use the activity
context only for showing dialogs, starting activities and inflating
layouts)
9. Model
- Anything that can provides information:
- REST APIs (Retrofit)
- Local storage (Realm, DBFlow, SQLite)
- Shared Preferences
- Firebase
- Sensor Manager
- Geolocation
10. Testing
- Model and ViewModel can be tested with simple JUnit Tests (mocking
with Mockito), since they don't rely on Android APIs
- View can be tested using Espresso, doing only UI assertions, since all
the logic is being tested on the ViewModel layer
11. A picture piece of code
is worth a thousand words
https://github.com/bresan/GitHubMVVM