Skip to content

Commit 2d781e4

Browse files
author
Daniel Novak
committed
Update README.md
1 parent a5bd061 commit 2d781e4

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

README.md

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,66 @@ AndroidViewModel
44
Separating data and state handling from Fragments or Activities without lots of boilerplate-code. Reducing them to simple <i>dumb views</i>.
55

66
<b>Basic idea behind this library</b>.
7-
An instance of a Model class is assigned to your Fragment or Activity during the first creation and is kept during it's life cycle, even between display orientation changes. The Model instance is removed after the Fragment or Activity is completely gone (finished, popped from backstack, replaced without keeping it in backstack).
7+
An instance of a ViewModel class is assigned to your Fragment or Activity during the first creation and is kept during it's life cycle, even between display orientation changes. The ViewModel instance is removed after the Fragment or Activity is completely gone (finished, popped from backstack, replaced without keeping it in backstack).
88

9-
You can execute asynchronous tasks in this Model 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.
9+
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.
10+
11+
![](website/static/viewmodel_architecture.png)
12+
13+
<b>How to implement</b>:
14+
15+
1. Create an interface for your <b>View</b> by extending [IView](library/src/main/java/eu/inloop/viewmodel/IView.java). We will call it IUserListView for this example.
16+
17+
```java
18+
19+
public interface IUserListView extends IView {
20+
public void showUsers(List<User> users);
21+
}
22+
```
23+
2. Create your <b>ViewModel</b> class by extending [AbstractViewModel](library/src/main/java/eu/inloop/viewmodel/AbstractViewModel.java). For example: <br/>
24+
25+
```java
26+
public class UserListViewModel extends AbstractViewModel<IUserListView> {
27+
....
28+
}
29+
```
30+
3. Each <b>Fragment</b> or <b>Activity</b> that you would like to associate with a ViewModel will need either to extend [ViewModelActivityBase](library/src/main/java/eu/inloop/viewmodel/base/ViewModelBaseActivity.java)/[ViewModelActivityBase](library/src/main/java/eu/inloop/viewmodel/base/ViewModelBaseFragment.java) or copy the implementation from these classes to your base activity/fragment class (in case you can't inherit directly). Override ```getViewModelClass()``` to return the corresponding ViewModel class. For example: <br/>
31+
32+
```java
33+
public class UserListFragment extends ViewModelBaseFragment<IUserListView, UserListViewModel>
34+
implements IUserListView {
35+
36+
@Override
37+
public Class<UserListViewModel> getViewModelClass() {
38+
return UserListViewModel.class;
39+
}
40+
41+
}
42+
```
43+
44+
<b>How to use</b>:
45+
46+
You can forward user interaction from the View into the ViewModel simply by calling:
47+
48+
```java
49+
getViewModel().onDeleteUserClicked(userId);
50+
```
51+
52+
The same goes for the opposite direction, when your asynchronous operation in the ViewModel finished and you would like to forward data to the View to show a list for example:
53+
54+
```java
55+
if (getView() != null) {
56+
getView().showUsers(userList);
57+
}
58+
```
1059

1160
<b>How does it work?</b>
61+
1262
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.
1363
The ViewModel is discarded once the Fragment/Activity is not reachable anymore (activity is finished or fragment permanently removed).
1464

1565
<b>Why no controller layer?</b>
66+
1667
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.
1768

1869
<b>Sample Workflow</b>:
@@ -26,6 +77,9 @@ This is not a strict MVC/MVP architecture - simply because we felt that having a
2677
7. ViewModel finishes the async task and tells the Fragment to show the data.
2778
8. User leaves the Activity, the Fragment is destroyed and the ViewModel is removed.
2879

80+
[![](website/static/lifecycle_thumb.png)](website/static/lifecycle.png)
81+
82+
2983
Download
3084
--------
3185

website/static/lifecycle.png

59.7 KB
Loading

website/static/lifecycle_thumb.png

30.2 KB
Loading
25.1 KB
Loading

0 commit comments

Comments
 (0)