From 2bff0f2bcf056245237386f4f152a81973918988 Mon Sep 17 00:00:00 2001 From: Maxime Biais Date: Tue, 11 Nov 2014 16:36:59 +0100 Subject: [PATCH] replace Chris Banes' PullToRefresh by the native SwipeToRefresh view --- WordPressUtils/build.gradle | 4 +- .../ptr/PullToRefreshHeaderTransformer.java | 129 ------------------ .../android/util/ptr/PullToRefreshHelper.java | 85 +++++------- WordPressUtils/src/main/res/values/attrs.xml | 7 + 4 files changed, 41 insertions(+), 184 deletions(-) delete mode 100644 WordPressUtils/src/main/java/org/wordpress/android/util/ptr/PullToRefreshHeaderTransformer.java create mode 100644 WordPressUtils/src/main/res/values/attrs.xml diff --git a/WordPressUtils/build.gradle b/WordPressUtils/build.gradle index efddf65dbcae..1934595dda00 100644 --- a/WordPressUtils/build.gradle +++ b/WordPressUtils/build.gradle @@ -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 { diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/ptr/PullToRefreshHeaderTransformer.java b/WordPressUtils/src/main/java/org/wordpress/android/util/ptr/PullToRefreshHeaderTransformer.java deleted file mode 100644 index 45bbeaf66d30..000000000000 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/ptr/PullToRefreshHeaderTransformer.java +++ /dev/null @@ -1,129 +0,0 @@ -package org.wordpress.android.util.ptr; - -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 org.wordpress.android.util.NetworkUtils; -import org.wordpress.android.util.R; - -import uk.co.senab.actionbarpulltorefresh.library.DefaultHeaderTransformer; -import uk.co.senab.actionbarpulltorefresh.library.PullToRefreshAttacher; -import uk.co.senab.actionbarpulltorefresh.library.sdk.Compat; - -public class PullToRefreshHeaderTransformer extends DefaultHeaderTransformer { - private View mHeaderView; - private ViewGroup mContentLayout; - private long mAnimationDuration; - private boolean mShowProgressBarOnly; - private OnTopScrollChangedListener mOnTopScrollChangedListener; - private boolean mIsNetworkRefreshMode; - private Context mContext; - private PullToRefreshAttacher mPullToRefreshAttacher; - - public interface OnTopScrollChangedListener { - public void onTopScrollChanged(boolean scrolledOnTop); - } - - 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); - mAnimationDuration = activity.getResources().getInteger(android.R.integer.config_shortAnimTime); - } - - @Override - public boolean hideHeaderView() { - mShowProgressBarOnly = false; - return super.hideHeaderView(); - } - - public void setPullToRefreshAttacher(PullToRefreshAttacher pullToRefreshAttacher) { - mPullToRefreshAttacher = pullToRefreshAttacher; - } - - private void setRefreshDisabled(boolean disabled) { - if (mPullToRefreshAttacher != null) { - mPullToRefreshAttacher.setRefreshDisabled(disabled); - } - } - - @Override - public boolean showHeaderView() { - // Workaround to avoid this bug https://github.com/chrisbanes/ActionBar-PullToRefresh/issues/265 - // Note, that also remove the alpha animation - setRefreshDisabled(false); - resetContentLayoutAlpha(); - - boolean changeVis = mHeaderView.getVisibility() != View.VISIBLE; - mContentLayout.setVisibility(View.VISIBLE); - if (changeVis) { - if (isNetworkAvailableOrNotChecked()) { - setPullText(mContext.getText(R.string.pull_to_refresh_pull_label)); - } else { - // Network mode enabled and network not available: show a different PTR label - setPullText(mContext.getText(R.string.pull_to_refresh_pull_no_network_label)); - setRefreshDisabled(true); - } - mHeaderView.setVisibility(View.VISIBLE); - AnimatorSet animSet = new AnimatorSet(); - ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(mHeaderView, "alpha", 0f, 1f); - ObjectAnimator transAnim = ObjectAnimator.ofFloat(mContentLayout, "translationY", - -mContentLayout.getHeight(), 10f); - animSet.playTogether(transAnim, alphaAnim); - animSet.play(alphaAnim); - animSet.setDuration(mAnimationDuration); - animSet.start(); - if (mShowProgressBarOnly) { - mContentLayout.setVisibility(View.INVISIBLE); - } - } - return changeVis; - } - - private void resetContentLayoutAlpha() { - Compat.setAlpha(mContentLayout, 1f); - } - - @Override - public void onReset() { - super.onReset(); - // Reset the Content Layout - if (mContentLayout != null) { - Compat.setAlpha(mContentLayout, 1f); - mContentLayout.setVisibility(View.VISIBLE); - } - } - - @Override - public void onTopScrollChanged(boolean scrolledOnTop) { - if (mOnTopScrollChangedListener != null) { - mOnTopScrollChangedListener.onTopScrollChanged(scrolledOnTop); - } - } - - public void setOnTopScrollChangedListener(OnTopScrollChangedListener listener) { - mOnTopScrollChangedListener = listener; - } - - public boolean isNetworkAvailableOrNotChecked() { - return !mIsNetworkRefreshMode || NetworkUtils.isNetworkAvailable(mContext); - } -} diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/ptr/PullToRefreshHelper.java b/WordPressUtils/src/main/java/org/wordpress/android/util/ptr/PullToRefreshHelper.java index 893bd8b81215..0f04581274b7 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/ptr/PullToRefreshHelper.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/ptr/PullToRefreshHelper.java @@ -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; @@ -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 TOAST_FREQUENCY = new HashSet(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 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); 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() { @@ -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; @@ -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) { diff --git a/WordPressUtils/src/main/res/values/attrs.xml b/WordPressUtils/src/main/res/values/attrs.xml new file mode 100644 index 000000000000..dd1fa5cbfd48 --- /dev/null +++ b/WordPressUtils/src/main/res/values/attrs.xml @@ -0,0 +1,7 @@ + + + + + + +