Skip to content

Commit e74f963

Browse files
author
Daniel Novak
committed
Clean up the code
1 parent 807a579 commit e74f963

File tree

7 files changed

+40
-28
lines changed

7 files changed

+40
-28
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@
77
import android.support.annotation.Nullable;
88
import android.support.v4.app.Fragment;
99

10-
import eu.inloop.viewmodel.IView;
11-
1210
public abstract class AbstractViewModel<T extends IView> {
1311

12+
@Nullable
1413
private String mUniqueIdentifier;
1514

1615
@Nullable
1716
private T mView;
1817

19-
void setUniqueIdentifier(String uniqueIdentifier) {
18+
void setUniqueIdentifier(@NonNull final String uniqueIdentifier) {
2019
mUniqueIdentifier = uniqueIdentifier;
2120
}
2221

@@ -25,6 +24,8 @@ void setUniqueIdentifier(String uniqueIdentifier) {
2524
* @return An app unique identifier for the current viewmodel instance (will be kept during orientation
2625
* change). This identifier will be reset in case the corresponding activity is killed.
2726
*/
27+
@SuppressWarnings("unused")
28+
@Nullable
2829
public String getUniqueIdentifier() {
2930
return mUniqueIdentifier;
3031
}
@@ -46,6 +47,7 @@ public void bindView(@NonNull T view) {
4647
mView = view;
4748
}
4849

50+
@Nullable
4951
public T getView() {
5052
return mView;
5153
}
@@ -55,7 +57,7 @@ public void clearView() {
5557
}
5658

5759
@SuppressWarnings("EmptyMethod")
58-
public void saveState(Bundle bundle) {
60+
public void saveState(@NonNull final Bundle bundle) {
5961

6062
}
6163

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public interface IViewModelProvider {
88

99
/**
1010
* See {@link eu.inloop.viewmodel.base.ViewModelBaseActivity} on how to implement.
11-
* @return
11+
* @return the {@link ViewModelProvider}.
1212
*/
13-
public ViewModelProvider getViewModelProvider();
13+
ViewModelProvider getViewModelProvider();
1414
}

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212

1313
public class ViewModelHelper<T extends IView, R extends AbstractViewModel<T>> {
1414

15+
@Nullable
1516
private String mScreenId;
17+
@Nullable
1618
private R mViewModel;
1719
private boolean mModelRemoved;
1820
private boolean mOnSaveInstanceCalled;
@@ -62,9 +64,9 @@ public void onCreate(@NonNull Activity activity,
6264
/**
6365
* Call from {@link android.support.v4.app.Fragment#onViewCreated(android.view.View, android.os.Bundle)}
6466
* or {@link android.app.Activity#onCreate(android.os.Bundle)}
65-
* @param view
67+
* @param view view
6668
*/
67-
public void setView(@NonNull T view) {
69+
public void setView(@NonNull final T view) {
6870
if (mViewModel == null) {
6971
//no viewmodel for this fragment
7072
return;
@@ -76,7 +78,7 @@ public void setView(@NonNull T view) {
7678
* Use in case this model is associated with an {@link android.support.v4.app.Fragment}
7779
* Call from {@link android.support.v4.app.Fragment#onDestroyView()}. Use in case model is associated
7880
* with Fragment
79-
* @param fragment
81+
* @param fragment fragment
8082
*/
8183
public void onDestroyView(@NonNull Fragment fragment) {
8284
if (mViewModel == null) {
@@ -92,9 +94,9 @@ public void onDestroyView(@NonNull Fragment fragment) {
9294
/**
9395
* Use in case this model is associated with an {@link android.support.v4.app.Fragment}
9496
* Call from {@link android.support.v4.app.Fragment#onDestroy()}
95-
* @param fragment
97+
* @param fragment fragment
9698
*/
97-
public void onDestroy(@NonNull Fragment fragment) {
99+
public void onDestroy(@NonNull final Fragment fragment) {
98100
if (mViewModel == null) {
99101
//no viewmodel for this fragment
100102
return;
@@ -105,7 +107,7 @@ public void onDestroy(@NonNull Fragment fragment) {
105107
// The fragment can be still in backstack even if isRemoving() is true.
106108
// We check mOnSaveInstanceCalled - if this was not called then the fragment is totally removed.
107109
if (BuildConfig.DEBUG) {
108-
Log.d("mode", "Removing viewmodel - fragment replaced");
110+
Log.d("mode", "Removing viewmodel - fragment replaced"); //NON-NLS
109111
}
110112
removeViewModel(fragment.getActivity());
111113
}
@@ -114,9 +116,9 @@ public void onDestroy(@NonNull Fragment fragment) {
114116
/**
115117
* Use in case this model is associated with an {@link android.app.Activity}
116118
* Call from {@link android.app.Activity#onDestroy()}
117-
* @param activity
119+
* @param activity activity
118120
*/
119-
public void onDestroy(@NonNull Activity activity) {
121+
public void onDestroy(@NonNull final Activity activity) {
120122
if (mViewModel == null) {
121123
//no viewmodel for this fragment
122124
return;
@@ -159,7 +161,7 @@ public R getViewModel() {
159161
* Call from {@link android.app.Activity#onSaveInstanceState(android.os.Bundle)}
160162
* or {@link android.support.v4.app.Fragment#onSaveInstanceState(android.os.Bundle)}.
161163
* This allows the model to save its state.
162-
* @param bundle
164+
* @param bundle bundle
163165
*/
164166
public void onSaveInstanceState(@NonNull Bundle bundle) {
165167
bundle.putString("identifier", mScreenId);
@@ -170,7 +172,7 @@ public void onSaveInstanceState(@NonNull Bundle bundle) {
170172
}
171173

172174
private void removeViewModel(@NonNull final Activity activity) {
173-
if (!mModelRemoved) {
175+
if (mViewModel != null && !mModelRemoved) {
174176
getViewModelProvider(activity).getViewModelProvider().remove(mScreenId);
175177
mViewModel.onModelRemoved();
176178
mModelRemoved = true;
@@ -179,7 +181,7 @@ private void removeViewModel(@NonNull final Activity activity) {
179181

180182
private IViewModelProvider getViewModelProvider(@NonNull Activity activity) {
181183
if (!(activity instanceof IViewModelProvider)) {
182-
throw new IllegalStateException("Your activity must implement IViewModelProvider");
184+
throw new IllegalStateException("Your activity must implement IViewModelProvider"); //NON-NLS
183185
}
184186
return ((IViewModelProvider) activity);
185187
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import android.app.Activity;
33
import android.support.annotation.NonNull;
44
import android.support.v4.app.FragmentActivity;
5-
import android.util.SparseArray;
65

76
import java.util.HashMap;
87

@@ -24,6 +23,7 @@ public static ViewModelProvider newInstance(@NonNull final FragmentActivity acti
2423
}
2524
}
2625

26+
@SuppressWarnings({"deprecation", "unused"})
2727
@Deprecated
2828
public static ViewModelProvider newInstance(@NonNull final Activity activity) {
2929
if (activity.getLastNonConfigurationInstance() == null) {
@@ -47,7 +47,8 @@ public synchronized void removeAllViewModels() {
4747

4848
@SuppressWarnings("unchecked")
4949
@NonNull
50-
public synchronized <T extends IView> ViewModelWrapper<T> getViewModel(String modelIdentifier, @NonNull Class<? extends AbstractViewModel<T>> viewModelClass) {
50+
public synchronized <T extends IView> ViewModelWrapper<T> getViewModel(final String modelIdentifier,
51+
final @NonNull Class<? extends AbstractViewModel<T>> viewModelClass) {
5152
AbstractViewModel<T> instance = (AbstractViewModel<T>) mViewModelCache.get(modelIdentifier);
5253
if (instance != null) {
5354
return new ViewModelWrapper<>(instance, false);

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.os.Bundle;
44
import android.support.annotation.NonNull;
5+
import android.support.annotation.Nullable;
56
import android.support.v7.app.AppCompatActivity;
67

78
import eu.inloop.viewmodel.AbstractViewModel;
@@ -16,7 +17,7 @@ public abstract class ViewModelBaseActivity<T extends IView, R extends AbstractV
1617
private ViewModelProvider mViewModelProvider;
1718

1819
@Override
19-
protected void onCreate(Bundle savedInstanceState) {
20+
protected void onCreate(@Nullable final Bundle savedInstanceState) {
2021
//This code must be execute prior to super.onCreate()
2122
mViewModelProvider = ViewModelProvider.newInstance(this);
2223
super.onCreate(savedInstanceState);
@@ -25,21 +26,23 @@ protected void onCreate(Bundle savedInstanceState) {
2526

2627
/**
2728
* Call this after your view is ready - usually on the end of {@link android.app.Activity#onCreate(Bundle)}
28-
* @param view
29+
* @param view view
2930
*/
30-
public void setModelView(@NonNull T view) {
31+
@SuppressWarnings("unused")
32+
public void setModelView(@NonNull final T view) {
3133
mViewModeHelper.setView(view);
3234
}
3335

3436
public abstract Class<R> getViewModelClass();
3537

3638
@Override
39+
@Nullable
3740
public Object onRetainCustomNonConfigurationInstance() {
3841
return mViewModelProvider;
3942
}
4043

4144
@Override
42-
public void onSaveInstanceState(Bundle outState) {
45+
public void onSaveInstanceState(@NonNull final Bundle outState) {
4346
super.onSaveInstanceState(outState);
4447
mViewModeHelper.onSaveInstanceState(outState);
4548
}
@@ -66,6 +69,7 @@ public void onDestroy() {
6669
}
6770

6871
@SuppressWarnings("unused")
72+
@Nullable
6973
public R getViewModel() {
7074
return mViewModeHelper.getViewModel();
7175
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.os.Bundle;
44
import android.support.annotation.NonNull;
5+
import android.support.annotation.Nullable;
56
import android.support.v4.app.Fragment;
67
import android.view.View;
78

@@ -14,23 +15,24 @@ public abstract class ViewModelBaseFragment<T extends IView, R extends AbstractV
1415
private final ViewModelHelper<T, R> mViewModeHelper = new ViewModelHelper<>();
1516

1617
@Override
17-
public void onCreate(Bundle savedInstanceState) {
18+
public void onCreate(@Nullable final Bundle savedInstanceState) {
1819
super.onCreate(savedInstanceState);
1920
mViewModeHelper.onCreate(getActivity(), savedInstanceState, getViewModelClass(), getArguments());
2021
}
2122

23+
@Nullable
2224
public abstract Class<R> getViewModelClass();
2325

2426
/**
2527
* Call this after your view is ready - usually on the end of {@link Fragment#onViewCreated(View, Bundle)}
26-
* @param view
28+
* @param view view
2729
*/
28-
protected void setModelView(@NonNull T view) {
30+
protected void setModelView(@NonNull final T view) {
2931
mViewModeHelper.setView(view);
3032
}
3133

3234
@Override
33-
public void onSaveInstanceState(Bundle outState) {
35+
public void onSaveInstanceState(@NonNull final Bundle outState) {
3436
super.onSaveInstanceState(outState);
3537
mViewModeHelper.onSaveInstanceState(outState);
3638
}
@@ -59,6 +61,7 @@ public void onDestroy() {
5961
super.onDestroy();
6062
}
6163

64+
@Nullable
6265
@SuppressWarnings("unused")
6366
public R getViewModel() {
6467
return mViewModeHelper.getViewModel();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ protected void onPostExecute(Void aVoid) {
127127

128128

129129
@Override
130-
public void saveState(Bundle bundle) {
130+
public void saveState(@NonNull final Bundle bundle) {
131131
super.saveState(bundle);
132132
if (mLoadedUsers != null) {
133133
bundle.putStringArrayList("userlist", new ArrayList<>(mLoadedUsers));

0 commit comments

Comments
 (0)