Skip to content

Commit 77c5b29

Browse files
author
Daniel Novak
committed
Update README (databinding)
1 parent 0e329cb commit 77c5b29

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

README.md

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ An instance of a ViewModel class is assigned to your Fragment or Activity during
88

99
You can execute asynchronous tasks in this ViewModel instance and this class is not destroyed during orientation change. All data handling and state logic should be placed inside this class. The Fragment or Activity is just a "dumb" view.
1010

11-
![](website/static/viewmodel_architecture.png)
1211

1312
How to implement
1413
--------
@@ -78,36 +77,29 @@ Your Fragment argument Bundle and Activity intent Bundle is forwarded to the Vie
7877
}
7978
```
8079

81-
<b>How does it work?</b>
82-
83-
A unique global ID is generated for the first time your Fragment or Activity is shown. This ID is passed on during orientation changes. Opening another instance of the same Fragment or Activity will result in a different ID. The ID is unique screen identifier. A ViewModel class is created and bound to this ID. The corresponding ViewModel instance is attached to your Fragment or Activity after an orientation change or if you return to the fragment in the back stack.
84-
The ViewModel is discarded once the Fragment/Activity is not reachable anymore (activity is finished or fragment permanently removed).
85-
86-
<b>Why no controller layer?</b>
80+
Data binding support
81+
--------
82+
Data binding is supported - extend [ViewModelBaseBindingFragment.java](library/src/main/java/eu/inloop/viewmodel/binding/ViewModelBaseBindingFragment.java) instead of ViewModelBaseFragment and implement ```getViewModelBindingConfig()``` in your Fragment.
8783

88-
This is not a strict MVC/MVP architecture - simply because we felt that having another layer between the "model" and the view does not bring enough advantages. So to further reduce the code this was simplified, where the Model is talking to the View over an interface. In mobile application most of the code is about interaction with the UI (getting data from API/DB, showing the data, manipulating, saving) so a more direct connection between the layers felt appropriate.
84+
``` java
85+
@Override
86+
public ViewModelBindingConfig getViewModelBindingConfig() {
87+
return new ViewModelBindingConfig(R.layout.fragment_sample_binding, getActivity());
88+
}
89+
```
8990

90-
<b>Sample Workflow</b>:
91+
That's it. You can then directly use ObservableField in your ViewModels. See [example](sample/src/main/java/eu/inloop/viewmodel/sample/viewmodel/SampleBindingViewModel.java).
9192

92-
1. <small>Fragment is shown to user. A ViewModel is assigned.</small>
93-
2. Fragment notifies the View that it's ready.
94-
3. ViewModel starts the async task to load data. Tells the view to show progress.
95-
4. User rotates the display. The ViewModel continues with the loading part.
96-
5. The Fragment is recreated after the orientation change is assigned the same ViewModel instance.
97-
6. Recreated Fragment tells the ViewModel that it's ready. ViewModel tells the UI to show loading, because it's still loading the data.
98-
7. ViewModel finishes the async task and tells the Fragment to show the data.
99-
8. User leaves the Activity, the Fragment is destroyed and the ViewModel is removed.
93+
How does it work?
94+
--------
10095

101-
[![](website/static/lifecycle_thumb.png)](website/static/lifecycle.png)
96+
A unique global ID is generated for the first time your Fragment or Activity is shown. This ID is passed on during orientation changes. Opening another instance of the same Fragment or Activity will result in a different ID. The ID is unique screen identifier. A ViewModel class is created and bound to this ID. The corresponding ViewModel instance is attached to your Fragment or Activity after an orientation change or if you return to the fragment in the back stack.
97+
The ViewModel is discarded once the Fragment/Activity is not reachable anymore (activity is finished or fragment permanently removed).
10298

10399

104100
Download
105101
--------
106102

107-
Grab via Gradle:
108103
```groovy
109104
compile 'eu.inloop:androidviewmodel:1.0.1'
110105
```
111-
112-
Build and study sample application from source code or download from Google Play.<br/>
113-
[![](website/static/google_play.png)](https://play.google.com/store/apps/details?id=eu.inloop.viewmodel.sample)

0 commit comments

Comments
 (0)