Skip to content

Commit 8c2d676

Browse files
Daniel NovakDanielX Novak
authored andcommitted
Make getViewModel as NonNull
1 parent de13a99 commit 8c2d676

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

library/src/main/java/eu/inloop/viewmodel/ViewModelHelper.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,18 @@ public void onStart() {
152152
}
153153

154154

155-
@Nullable
155+
/**
156+
* Returns the current ViewModel instance associated with the Fragment or Activity.
157+
* Throws an {@link IllegalStateException} in case the ViewModel is null. This can happen
158+
* if you call this method too soon - before {@link Activity#onCreate(Bundle)} or {@link Fragment#onCreate(Bundle)}
159+
* or this {@link ViewModelHelper} is not properly setup.
160+
* @return {@link R}
161+
*/
162+
@NonNull
156163
public R getViewModel() {
164+
if (null == mViewModel) {
165+
throw new IllegalStateException("ViewModel is not ready. Are you calling this method before Activity/Fragment onCreate?"); //NON-NLS
166+
}
157167
return mViewModel;
158168
}
159169

library/src/main/java/eu/inloop/viewmodel/base/ViewModelBaseActivity.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@ public void onDestroy() {
5353
super.onDestroy();
5454
}
5555

56+
/**
57+
* @see ViewModelHelper#getViewModel()
58+
*/
5659
@SuppressWarnings("unused")
57-
@Nullable
60+
@NonNull
5861
public R getViewModel() {
5962
return mViewModeHelper.getViewModel();
6063
}

library/src/main/java/eu/inloop/viewmodel/base/ViewModelBaseFragment.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ public void onDestroy() {
6161
super.onDestroy();
6262
}
6363

64-
@Nullable
64+
/**
65+
* @see ViewModelHelper#getViewModel()
66+
*/
67+
@NonNull
6568
@SuppressWarnings("unused")
6669
public R getViewModel() {
6770
return mViewModeHelper.getViewModel();

0 commit comments

Comments
 (0)