Skip to content

Commit 900cccf

Browse files
author
Daniel
committed
Update samples and Readme
1 parent 62cb8d4 commit 900cccf

File tree

6 files changed

+15
-45
lines changed

6 files changed

+15
-45
lines changed

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,12 @@ How to implement
2727
....
2828
}
2929
```
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)/[ViewModelBaseFragment](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/>
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)/[ViewModelBaseFragment](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). For example: <br/>
3131

3232
```java
3333
public class UserListFragment extends ViewModelBaseFragment<IUserListView, UserListViewModel>
3434
implements IUserListView {
3535

36-
@Override
37-
public Class<UserListViewModel> getViewModelClass() {
38-
return UserListViewModel.class;
39-
}
40-
4136
}
4237
```
4338

@@ -63,6 +58,13 @@ You can forward user interaction from the View into the ViewModel simply by call
6358

6459
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:
6560

61+
```java
62+
getViewOptional().showUsers(userList);
63+
```
64+
65+
The ```getViewOptional()``` method will never return null. It will return a dummy implementation in case the View is null at the moment (e.g. Fragment already destroyed, or between orientation change).
66+
You can also check if the View is not null in case you need to:
67+
6668
```java
6769
if (getView() != null) {
6870
getView().showUsers(userList);
@@ -76,6 +78,7 @@ Your Fragment argument Bundle and Activity intent Bundle is forwarded to the Vie
7678
long userId = arguments.getInt("user_id", -1);
7779
}
7880
```
81+
7982

8083
Data binding support
8184
--------

sample/src/main/java/eu/inloop/viewmodel/sample/fragment/PagerFragment.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
3939
setModelView(this);
4040
}
4141

42-
@Override
43-
public Class<PageModel> getViewModelClass() {
44-
return PageModel.class;
45-
}
46-
4742
@Override
4843
public void onDestroy() {
4944
super.onDestroy();

sample/src/main/java/eu/inloop/viewmodel/sample/fragment/SampleBindingFragment.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,4 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
3333
public ViewModelBindingConfig getViewModelBindingConfig() {
3434
return new ViewModelBindingConfig(R.layout.fragment_sample_binding, getActivity());
3535
}
36-
37-
@Nullable
38-
@Override
39-
public Class<SampleBindingViewModel> getViewModelClass() {
40-
return SampleBindingViewModel.class;
41-
}
42-
4336
}

sample/src/main/java/eu/inloop/viewmodel/sample/fragment/SampleBundleFragment.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,4 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
3636
ButterKnife.inject(this, view);
3737
setModelView(this);
3838
}
39-
40-
@Override
41-
public Class<SampleArgumentViewModel> getViewModelClass() {
42-
return SampleArgumentViewModel.class;
43-
}
44-
4539
}

sample/src/main/java/eu/inloop/viewmodel/sample/fragment/UserListFragment.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@ public void onCreate(Bundle savedInstanceState) {
4747
mAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, new ArrayList<String>());
4848
}
4949

50-
@Override
51-
public Class<UserListViewModel> getViewModelClass() {
52-
return UserListViewModel.class;
53-
}
54-
5550
@Override
5651
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
5752
final View view = inflater.inflate(R.layout.fragment_userlist, container, false);

sample/src/main/java/eu/inloop/viewmodel/sample/viewmodel/UserListViewModel.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ public void onBindView(@NonNull IUserListView view) {
4848
private void loadUsers() {
4949
mLoadingUsers = true;
5050
mCurrentLoadingProgress = 0;
51-
if (getView() != null) {
52-
getView().showLoading(mCurrentLoadingProgress);
53-
}
51+
getViewOptional().showLoading(mCurrentLoadingProgress);
5452
new AsyncTask<Void, Float, List<String>>() {
5553

5654
@Override
@@ -74,20 +72,16 @@ protected List<String> doInBackground(Void... voids) {
7472
protected void onProgressUpdate(Float... values) {
7573
super.onProgressUpdate(values);
7674
mCurrentLoadingProgress = values[0];
77-
if (getView() != null) {
78-
getView().showLoading(mCurrentLoadingProgress);
79-
}
75+
getViewOptional().showLoading(mCurrentLoadingProgress);
8076
}
8177

8278
@Override
8379
protected void onPostExecute(List<String> s) {
8480
super.onPostExecute(s);
8581
mLoadedUsers = s;
8682
mLoadingUsers = false;
87-
if (getView() != null) {
88-
getView().showUsers(s);
89-
getView().hideProgress();
90-
}
83+
getViewOptional().showUsers(s);
84+
getViewOptional().hideProgress();
9185
}
9286
}.execute();
9387
}
@@ -97,9 +91,7 @@ public void deleteUser(final int position) {
9791
return;
9892
}
9993
mLoadedUsers.set(position, "Deleting in 5 seconds...");
100-
if (getView() != null) {
101-
getView().showUsers(mLoadedUsers);
102-
}
94+
getViewOptional().showUsers(mLoadedUsers);
10395

10496
final String itemToDelete = mLoadedUsers.get(position);
10597
new AsyncTask<Void, Void, Void>() {
@@ -118,9 +110,7 @@ protected Void doInBackground(Void... voids) {
118110
@Override
119111
protected void onPostExecute(Void aVoid) {
120112
super.onPostExecute(aVoid);
121-
if (getView() != null) {
122-
getView().showUsers(mLoadedUsers);
123-
}
113+
getViewOptional().showUsers(mLoadedUsers);
124114
}
125115
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
126116
}

0 commit comments

Comments
 (0)