Skip to content

Commit

Permalink
add a network mode in PullToRefreshHeaderTransformer to show a differ…
Browse files Browse the repository at this point in the history
…ent message when network is disabled
  • Loading branch information
maxme committed Aug 26, 2014
1 parent 4f788e7 commit cce1b59
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.TextView;

import org.wordpress.android.util.NetworkUtils;
import org.wordpress.android.util.R;

import uk.co.senab.actionbarpulltorefresh.library.DefaultHeaderTransformer;
Expand All @@ -15,10 +18,13 @@
public class PullToRefreshHeaderTransformer extends DefaultHeaderTransformer {
private View mHeaderView;
private ViewGroup mContentLayout;
private TextView mTextView;
private long mAnimationDuration;
private boolean mShowProgressBarOnly;
private Animation mHeaderOutAnimation;
private OnTopScrollChangedListener mOnTopScrollChangedListener;
private boolean mIsNetworkRefreshMode;
private Context mContext;

public interface OnTopScrollChangedListener {
public void onTopScrollChanged(boolean scrolledOnTop);
Expand All @@ -28,11 +34,21 @@ public void setShowProgressBarOnly(boolean progressBarOnly) {
mShowProgressBarOnly = progressBarOnly;
}

public boolean isNetworkRefreshMode() {
return mIsNetworkRefreshMode;
}

public void setNetworkRefreshMode(boolean isNetworkRefresh) {
mIsNetworkRefreshMode = isNetworkRefresh;
}

@Override
public void onViewCreated(Activity activity, View headerView) {
super.onViewCreated(activity, headerView);
mContext = activity.getBaseContext();
mHeaderView = headerView;
mContentLayout = (ViewGroup) headerView.findViewById(R.id.ptr_content);
mTextView = (TextView) headerView.findViewById(R.id.ptr_text);
mAnimationDuration = activity.getResources().getInteger(android.R.integer.config_shortAnimTime);
}

Expand All @@ -51,6 +67,12 @@ public boolean showHeaderView() {
boolean changeVis = mHeaderView.getVisibility() != View.VISIBLE;
mContentLayout.setVisibility(View.VISIBLE);
if (changeVis) {
if (isNetworkAvailableOrNotChecked()) {
mTextView.setText(mContext.getText(R.string.pull_to_refresh_pull_label));
} else {
// Network mode enabled and network not available: show a different PTR label
mTextView.setText(mContext.getText(R.string.pull_to_refresh_pull_no_network_label));
}
mHeaderView.setVisibility(View.VISIBLE);
AnimatorSet animSet = new AnimatorSet();
ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(mHeaderView, "alpha", 0f, 1f);
Expand Down Expand Up @@ -96,4 +118,8 @@ public void onTopScrollChanged(boolean scrolledOnTop) {
public void setOnTopScrollChangedListener(OnTopScrollChangedListener listener) {
mOnTopScrollChangedListener = listener;
}

public boolean isNetworkAvailableOrNotChecked() {
return !mIsNetworkRefreshMode || NetworkUtils.isNetworkAvailable(mContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ public boolean isReadyForPull(View view, float v, float v2) {
setupWizard.setup(mPullToRefreshLayout);
}

/**
* Once set, each PTR action will check for network connectivity. If there is thes network is not available
* (airplane mode for instance), the message will be changed from "Pull to refresh..." to "Can't refresh..."
*/
public void setNetworkRefreshMode(boolean refreshing) {
mHeaderTransformer.setNetworkRefreshMode(refreshing);
}

public void setRefreshing(boolean refreshing) {
mHeaderTransformer.setShowProgressBarOnly(refreshing);
mPullToRefreshLayout.setRefreshing(refreshing);
Expand All @@ -77,7 +85,11 @@ public boolean isRefreshing() {

@Override
public void onRefreshStarted(View view) {
mRefreshListener.onRefreshStarted(view);
if (mHeaderTransformer.isNetworkAvailableOrNotChecked()) {
mRefreshListener.onRefreshStarted(view);
} else {
setRefreshing(false);
}
}

public interface RefreshListener {
Expand All @@ -103,7 +115,7 @@ public void refreshAction() {
}
Editor editor = preferences.edit();
editor.putInt(REFRESH_BUTTON_HIT_COUNT, refreshHits);
editor.commit();
editor.apply();
}

public void registerReceiver(Context context) {
Expand Down
1 change: 1 addition & 0 deletions WordPressUtils/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="ptr_tip_message">Tip: Pull down to refresh</string>
<string name="pull_to_refresh_pull_no_network_label">No network, can\'t refresh</string>
<string name="no_network_message">There is no network available</string>
</resources>

0 comments on commit cce1b59

Please sign in to comment.