Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving view states #138

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,30 @@
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import com.fernandocejas.android10.sample.presentation.R;
import com.fernandocejas.android10.sample.presentation.internal.di.components.UserComponent;
import com.fernandocejas.android10.sample.presentation.model.UserModel;
import com.fernandocejas.android10.sample.presentation.presenter.UserDetailsPresenter;
import com.fernandocejas.android10.sample.presentation.view.UserDetailsView;
import com.fernandocejas.android10.sample.presentation.view.component.AutoLoadImageView;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
* Fragment that shows details of a certain user.
*/
public class UserDetailsFragment extends BaseFragment implements UserDetailsView {

private static final String INSTANCE_STATE_PARAM_SHOW_LOADING
= "org.android10.STATE_PARAM_SHOW_LOADING";
private static final String INSTANCE_STATE_PARAM_SHOW_RETRY
= "org.android10.STATE_PARAM_SHOW_RETRY";

@Inject UserDetailsPresenter userDetailsPresenter;

@Bind(R.id.iv_cover) AutoLoadImageView iv_cover;
Expand Down Expand Up @@ -63,6 +71,22 @@ public UserDetailsFragment() {
}
}

@Override
public void onViewStateRestored(Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
if (savedInstanceState == null) {
return;
}
boolean loadingVisible = savedInstanceState.getBoolean(INSTANCE_STATE_PARAM_SHOW_LOADING);
boolean retryVisible = savedInstanceState.getBoolean(INSTANCE_STATE_PARAM_SHOW_RETRY);
if (loadingVisible) {
showLoading();
}
if (retryVisible) {
showRetry();
}
}

@Override public void onResume() {
super.onResume();
this.userDetailsPresenter.resume();
Expand All @@ -73,6 +97,15 @@ public UserDetailsFragment() {
this.userDetailsPresenter.pause();
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
boolean loadingVisible = rl_progress.getVisibility() == View.VISIBLE;
boolean retryVisible = rl_retry.getVisibility() == View.VISIBLE;
outState.putBoolean(INSTANCE_STATE_PARAM_SHOW_LOADING, loadingVisible);
outState.putBoolean(INSTANCE_STATE_PARAM_SHOW_RETRY, retryVisible);
}

@Override public void onDestroyView() {
super.onDestroyView();
ButterKnife.unbind(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,33 @@
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import com.fernandocejas.android10.sample.presentation.R;
import com.fernandocejas.android10.sample.presentation.internal.di.components.UserComponent;
import com.fernandocejas.android10.sample.presentation.model.UserModel;
import com.fernandocejas.android10.sample.presentation.presenter.UserListPresenter;
import com.fernandocejas.android10.sample.presentation.view.UserListView;
import com.fernandocejas.android10.sample.presentation.view.adapter.UsersAdapter;
import com.fernandocejas.android10.sample.presentation.view.adapter.UsersLayoutManager;

import java.util.Collection;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
* Fragment that shows a list of Users.
*/
public class UserListFragment extends BaseFragment implements UserListView {

private static final String INSTANCE_STATE_PARAM_SHOW_LOADING
= "org.android10.STATE_PARAM_SHOW_LOADING";
private static final String INSTANCE_STATE_PARAM_SHOW_RETRY
= "org.android10.STATE_PARAM_SHOW_RETRY";

/**
* Interface for listening user list events.
*/
Expand Down Expand Up @@ -81,6 +90,22 @@ public UserListFragment() {
}
}

@Override
public void onViewStateRestored(Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
if (savedInstanceState == null) {
return;
}
boolean loadingVisible = savedInstanceState.getBoolean(INSTANCE_STATE_PARAM_SHOW_LOADING);
boolean retryVisible = savedInstanceState.getBoolean(INSTANCE_STATE_PARAM_SHOW_RETRY);
if (loadingVisible) {
showLoading();
}
if (retryVisible) {
showRetry();
}
}

@Override public void onResume() {
super.onResume();
this.userListPresenter.resume();
Expand All @@ -91,6 +116,15 @@ public UserListFragment() {
this.userListPresenter.pause();
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
boolean loadingVisible = rl_progress.getVisibility() == View.VISIBLE;
boolean retryVisible = rl_retry.getVisibility() == View.VISIBLE;
outState.putBoolean(INSTANCE_STATE_PARAM_SHOW_LOADING, loadingVisible);
outState.putBoolean(INSTANCE_STATE_PARAM_SHOW_RETRY, retryVisible);
}

@Override public void onDestroyView() {
super.onDestroyView();
rv_users.setAdapter(null);
Expand Down