Skip to content

Commit

Permalink
replace Chris Banes' PullToRefresh by the native SwipeToRefresh view
Browse files Browse the repository at this point in the history
  • Loading branch information
maxme committed Nov 11, 2014
1 parent a3ef9c1 commit 2bff0f2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 184 deletions.
4 changes: 1 addition & 3 deletions WordPressUtils/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ repositories {
dependencies {
compile 'commons-lang:commons-lang:2.6'
compile 'com.mcxiaoke.volley:library:1.0.+'
compile 'com.github.castorflex.smoothprogressbar:library:0.4.0'
compile 'org.wordpress:pulltorefresh-main:0.9.7@aar' // org.wordpress version includes some fixes
compile 'com.android.support:support-v13:19.1.0'
compile 'com.android.support:support-v13:21.0.+'
}

android {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.TypedArray;
import android.preference.PreferenceManager;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.util.TypedValue;

import org.wordpress.android.util.R;
import org.wordpress.android.util.ToastUtils;
Expand All @@ -20,87 +23,58 @@
import java.util.HashSet;
import java.util.Set;

import uk.co.senab.actionbarpulltorefresh.library.ActionBarPullToRefresh;
import uk.co.senab.actionbarpulltorefresh.library.ActionBarPullToRefresh.SetupWizard;
import uk.co.senab.actionbarpulltorefresh.library.Options;
import uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout;
import uk.co.senab.actionbarpulltorefresh.library.listeners.OnRefreshListener;
import uk.co.senab.actionbarpulltorefresh.library.viewdelegates.ViewDelegate;

public class PullToRefreshHelper implements OnRefreshListener {
public static final String BROADCAST_ACTION_REFRESH_MENU_PRESSED = "REFRESH_MENU_PRESSED";
private static final String REFRESH_BUTTON_HIT_COUNT = "REFRESH_BUTTON_HIT_COUNT";
private static final Set<Integer> TOAST_FREQUENCY = new HashSet<Integer>(Arrays.asList(1, 5, 10, 20, 40, 80, 160,
320, 640));
private PullToRefreshHeaderTransformer mHeaderTransformer;
private PullToRefreshLayout mPullToRefreshLayout;
private SwipeRefreshLayout mSwipeRefreshLayout;
private RefreshListener mRefreshListener;
private WeakReference<Activity> mActivityRef;

public PullToRefreshHelper(Activity activity, PullToRefreshLayout pullToRefreshLayout, RefreshListener listener) {
init(activity, pullToRefreshLayout, listener, null);
public PullToRefreshHelper(Activity activity, SwipeRefreshLayout swipeRefreshLayout, RefreshListener listener) {
init(activity, swipeRefreshLayout, listener, null);
}

public PullToRefreshHelper(Activity activity, PullToRefreshLayout pullToRefreshLayout, RefreshListener listener,
public PullToRefreshHelper(Activity activity, SwipeRefreshLayout swipeRefreshLayout, RefreshListener listener,
java.lang.Class<?> viewClass) {
init(activity, pullToRefreshLayout, listener, viewClass);
init(activity, swipeRefreshLayout, listener, viewClass);
}

public void init(Activity activity, PullToRefreshLayout pullToRefreshLayout, RefreshListener listener,
public void init(Activity activity, SwipeRefreshLayout swipeRefreshLayout, RefreshListener listener,
java.lang.Class<?> viewClass) {
mActivityRef = new WeakReference<Activity>(activity);
mRefreshListener = listener;
mPullToRefreshLayout = pullToRefreshLayout;
mHeaderTransformer = new PullToRefreshHeaderTransformer();
SetupWizard setupWizard = ActionBarPullToRefresh.from(activity).options(Options.create().headerTransformer(
mHeaderTransformer).build()).allChildrenArePullable().listener(this);
if (viewClass != null) {
setupWizard.useViewDelegate(viewClass, new ViewDelegate() {
@Override
public boolean isReadyForPull(View view, float v, float v2) {
return true;
}
}
);
}
setupWizard.setup(mPullToRefreshLayout);
// set network refresh mode as default
setNetworkRefreshMode(true);
mHeaderTransformer.setPullToRefreshAttacher(mPullToRefreshLayout.getAttacher());
}

/**
* 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);
mSwipeRefreshLayout = swipeRefreshLayout;
mSwipeRefreshLayout.setOnRefreshListener(this);
final TypedArray styleAttrs = obtainStyledAttrsFromThemeAttr(activity, R.attr.refreshIndicatorColor,
R.styleable.RefreshIndicator);
int color = styleAttrs.getColor(R.styleable.RefreshIndicator_refreshIndicatorColor,
android.R.color.holo_blue_dark);
mSwipeRefreshLayout.setProgressBackgroundColor(color);
mSwipeRefreshLayout.setColorSchemeResources(android.R.color.white, android.R.color.white, android.R.color.white,
android.R.color.white);
}

public void setRefreshing(boolean refreshing) {
mHeaderTransformer.setShowProgressBarOnly(refreshing);
mPullToRefreshLayout.setRefreshing(refreshing);
mSwipeRefreshLayout.setRefreshing(refreshing);
}

public boolean isRefreshing() {
return mPullToRefreshLayout.isRefreshing();
return mSwipeRefreshLayout.isRefreshing();
}

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

public interface RefreshListener {
public void onRefreshStarted(View view);
public void onRefreshStarted();
}

public void setEnabled(boolean enabled) {
mPullToRefreshLayout.setEnabled(enabled);
mSwipeRefreshLayout.setEnabled(enabled);
}

public void refreshAction() {
Expand All @@ -109,7 +83,7 @@ public void refreshAction() {
return;
}
setRefreshing(true);
mRefreshListener.onRefreshStarted(mPullToRefreshLayout);
mRefreshListener.onRefreshStarted();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
int refreshHits = preferences.getInt(REFRESH_BUTTON_HIT_COUNT, 0);
refreshHits += 1;
Expand Down Expand Up @@ -143,6 +117,13 @@ public void unregisterReceiver(Context context) {
}
}

public static TypedArray obtainStyledAttrsFromThemeAttr(Context context, int themeAttr, int[] styleAttrs) {
TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(themeAttr, outValue, true);
int styleResId = outValue.resourceId;
return context.obtainStyledAttributes(styleResId, styleAttrs);
}

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Expand Down
7 changes: 7 additions & 0 deletions WordPressUtils/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="swipeToRefreshStyle" format="reference"/>
<declare-styleable name="RefreshIndicator">
<attr name="refreshIndicatorColor" format="reference|color"/>
</declare-styleable>
</resources>

0 comments on commit 2bff0f2

Please sign in to comment.