Skip to content

Commit be3a2a6

Browse files
author
Daniel Novak
committed
Add onCreate method to ViewModel
1 parent 57fbf58 commit be3a2a6

File tree

9 files changed

+104
-49
lines changed

9 files changed

+104
-49
lines changed

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package eu.inloop.viewmodel;
22

3+
import android.app.Activity;
4+
import android.content.Intent;
35
import android.os.Bundle;
46
import android.support.annotation.NonNull;
57
import android.support.annotation.Nullable;
8+
import android.support.v4.app.Fragment;
69

710
import eu.inloop.viewmodel.IView;
811

@@ -26,6 +29,18 @@ public int getUniqueIdentifier() {
2629
return mUniqueIdentifier;
2730
}
2831

32+
/**
33+
* @param arguments initial ViewModel arguments passed from {@link Fragment#getArguments()} or
34+
* {@link Activity#getIntent()}.{@link Intent#getExtras()}
35+
* @param savedInstanceState bundle with saved state, will be not null
36+
* only in case the system is killed due to low memory
37+
* and restored (and {@link #saveState(Bundle)} returned a non-null bundle.
38+
*/
39+
@SuppressWarnings("EmptyMethod")
40+
public void onCreate(@Nullable Bundle arguments, @Nullable Bundle savedInstanceState) {
41+
42+
}
43+
2944
public void initWithView(@NonNull T view) {
3045
mView = view;
3146
}
@@ -43,15 +58,6 @@ public void saveState(Bundle bundle) {
4358

4459
}
4560

46-
/**
47-
* Will be called only in case the system is killed due to low memory and restored
48-
* @param bundle - bundle with saved state
49-
*/
50-
@SuppressWarnings("EmptyMethod")
51-
public void restoreState(Bundle bundle) {
52-
53-
}
54-
5561
@SuppressWarnings("EmptyMethod")
5662
public void onStop() {
5763

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package eu.inloop.viewmodel;
22

33
import android.app.Activity;
4+
import android.content.Intent;
45
import android.os.Bundle;
56
import android.support.annotation.NonNull;
67
import android.support.annotation.Nullable;
@@ -20,11 +21,15 @@ public class ViewModelHelper<T extends IView, R extends AbstractViewModel<T>> {
2021
/**
2122
* Call from {@link android.app.Activity#onCreate(android.os.Bundle)} or
2223
* {@link android.support.v4.app.Fragment#onCreate(android.os.Bundle)}
23-
* @param savedInstanceState
24-
* @param viewModelClass
24+
* @param savedInstanceState savedInstance state from {@link Activity#onCreate(Bundle)} or
25+
* {@link Fragment#onCreate(Bundle)}
26+
* @param viewModelClass the {@link Class} of your ViewModel
27+
* @param arguments pass {@link Fragment#getArguments()} or
28+
* {@link Activity#getIntent()}.{@link Intent#getExtras() getExtras()}
2529
*/
2630
public void onCreate(@Nullable Bundle savedInstanceState,
27-
@Nullable Class<? extends AbstractViewModel<T>> viewModelClass) {
31+
@Nullable Class<? extends AbstractViewModel<T>> viewModelClass,
32+
@Nullable Bundle arguments) {
2833
// no viewmodel for this fragment
2934
if (viewModelClass == null) {
3035
mViewModel = null;
@@ -44,12 +49,12 @@ public void onCreate(@Nullable Bundle savedInstanceState,
4449
//noinspection unchecked
4550
mViewModel = (R) viewModelWrapper.viewModel;
4651

47-
// detect that the system has killed the app - saved instance is not null, but the model was recreated
48-
if (savedInstanceState != null && viewModelWrapper.wasCreated) {
49-
if (BuildConfig.DEBUG) {
52+
if (viewModelWrapper.wasCreated) {
53+
// detect that the system has killed the app - saved instance is not null, but the model was recreated
54+
if (BuildConfig.DEBUG && savedInstanceState != null) {
5055
Log.d("model", "Fragment recreated by system - restoring viewmodel");
5156
}
52-
mViewModel.restoreState(savedInstanceState);
57+
mViewModel.onCreate(arguments, savedInstanceState);
5358
}
5459
}
5560

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public abstract class ViewModelBaseActivity<T extends IView, R extends AbstractV
1414
@Override
1515
protected void onCreate(Bundle savedInstanceState) {
1616
super.onCreate(savedInstanceState);
17-
mViewModeHelper.onCreate(savedInstanceState, getViewModelClass());
17+
mViewModeHelper.onCreate(savedInstanceState, getViewModelClass(), getIntent().getExtras());
1818
//noinspection unchecked
1919
mViewModeHelper.initWithView((T) this);
2020
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
import eu.inloop.viewmodel.IView;
1010
import eu.inloop.viewmodel.ViewModelHelper;
1111

12-
public abstract class ViewModelBaseFragment<T extends IView, R extends AbstractViewModel<T>> extends Fragment {
12+
public abstract class ViewModelBaseFragment<T extends IView, R extends AbstractViewModel<T>> extends Fragment implements IView {
1313

1414
private final ViewModelHelper<T, R> mViewModeHelper = new ViewModelHelper<>();
1515

1616
@Override
1717
public void onCreate(Bundle savedInstanceState) {
1818
super.onCreate(savedInstanceState);
19-
mViewModeHelper.onCreate(savedInstanceState, getViewModelClass());
19+
mViewModeHelper.onCreate(savedInstanceState, getViewModelClass(), getArguments());
2020
}
2121

2222
public abstract Class<R> getViewModelClass();
@@ -62,4 +62,5 @@ public void onDestroy() {
6262
public R getViewModel() {
6363
return mViewModeHelper.getViewModel();
6464
}
65+
6566
}

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

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package eu.inloop.viewmodel.sample.fragment;
2+
3+
import android.os.Bundle;
4+
import android.support.annotation.Nullable;
5+
import android.support.v4.app.Fragment;
6+
import android.view.LayoutInflater;
7+
import android.view.View;
8+
import android.view.ViewGroup;
9+
10+
import eu.inloop.viewmodel.IView;
11+
import eu.inloop.viewmodel.base.ViewModelBaseFragment;
12+
import eu.inloop.viewmodel.sample.R;
13+
import eu.inloop.viewmodel.sample.viewmodel.SampleArgumentViewModel;
14+
15+
16+
public class SampleBundleFragment extends ViewModelBaseFragment<IView, SampleArgumentViewModel> {
17+
18+
public static SampleBundleFragment newInstance(int userId) {
19+
final Bundle bundle = new Bundle();
20+
bundle.putInt(SampleArgumentViewModel.ARG_INT_USER_ID, userId);
21+
22+
final SampleBundleFragment fragment = new SampleBundleFragment();
23+
fragment.setArguments(bundle);
24+
return fragment;
25+
}
26+
27+
@Override
28+
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
29+
return inflater.inflate(R.layout.fragment_empty, container, false);
30+
}
31+
32+
@Override
33+
public Class<SampleArgumentViewModel> getViewModelClass() {
34+
return SampleArgumentViewModel.class;
35+
}
36+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
5555
headerView.findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
5656
@Override
5757
public void onClick(View view) {
58-
getFragmentManager().beginTransaction().replace(R.id.root_content, new EmptyFragment(), "empty-fragment").addToBackStack(null).commit();
58+
getFragmentManager().beginTransaction().replace(R.id.root_content, SampleBundleFragment.newInstance(1234), "empty-fragment").addToBackStack(null).commit();
5959
}
6060
});
6161
headerView.findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@@ -93,7 +93,7 @@ public void showUsers(List<String> users) {
9393
@Override
9494
public void showLoading(float progress) {
9595
mProgressView.setVisibility(View.VISIBLE);
96-
mProgressText.setText((int)(progress * 100) + "%");
96+
mProgressText.setText((int) (progress * 100) + "%");
9797
}
9898

9999
@Override
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package eu.inloop.viewmodel.sample.viewmodel;
2+
3+
import android.os.Bundle;
4+
import android.support.annotation.Nullable;
5+
import android.util.Log;
6+
7+
import eu.inloop.viewmodel.AbstractViewModel;
8+
import eu.inloop.viewmodel.IView;
9+
10+
public class SampleArgumentViewModel extends AbstractViewModel<IView> {
11+
12+
public static final String ARG_INT_USER_ID = "ARG_INT_USER_ID";
13+
14+
@Override
15+
public void onCreate(@Nullable Bundle arguments, @Nullable Bundle savedInstanceState) {
16+
super.onCreate(arguments, savedInstanceState);
17+
18+
Log.d("SampleArgumentViewModel", "ViewModel created with argument - " + arguments.getInt(ARG_INT_USER_ID));
19+
if (savedInstanceState != null) {
20+
Log.d("SampleArgumentViewModel", "Application killed by system, viewmodel is restored");
21+
}
22+
}
23+
}

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.os.AsyncTask;
44
import android.os.Bundle;
55
import android.support.annotation.NonNull;
6+
import android.support.annotation.Nullable;
67

78
import java.util.ArrayList;
89
import java.util.List;
@@ -18,9 +19,18 @@ public class UserListViewModel extends AbstractViewModel<IUserListView> {
1819
//Don't persist state variables
1920
private boolean mLoadingUsers;
2021

21-
private int mNumberOfRunningDeletes = 0;
2222
private float mCurrentLoadingProgress = 0;
2323

24+
@Override
25+
public void onCreate(@Nullable Bundle arguments, @Nullable Bundle savedInstanceState) {
26+
super.onCreate(arguments, savedInstanceState);
27+
28+
//this will be only not null in case the application was killed due to low memory
29+
if (savedInstanceState != null) {
30+
mLoadedUsers = savedInstanceState.getStringArrayList("userlist");
31+
}
32+
}
33+
2434
@Override
2535
public void initWithView(@NonNull IUserListView view) {
2636
super.initWithView(view);
@@ -90,7 +100,7 @@ public void deleteUser(final int position) {
90100
if (getView() != null) {
91101
getView().showUsers(mLoadedUsers);
92102
}
93-
mNumberOfRunningDeletes++;
103+
94104
final String itemToDelete = mLoadedUsers.get(position);
95105
new AsyncTask<Void, Void, Void>() {
96106

@@ -102,7 +112,6 @@ protected Void doInBackground(Void... voids) {
102112
//
103113
}
104114
mLoadedUsers.remove(itemToDelete);
105-
mNumberOfRunningDeletes--;
106115
return null;
107116
}
108117

@@ -117,12 +126,6 @@ protected void onPostExecute(Void aVoid) {
117126
}
118127

119128

120-
@Override
121-
public void restoreState(Bundle bundle) {
122-
super.restoreState(bundle);
123-
mLoadedUsers = bundle.getStringArrayList("userlist");
124-
}
125-
126129
@Override
127130
public void saveState(Bundle bundle) {
128131
super.saveState(bundle);

0 commit comments

Comments
 (0)