Skip to content

Commit

Permalink
Merge pull request #1 from wordpress-mobile/issue/move-ptr-to-utils
Browse files Browse the repository at this point in the history
Move ptr to utils
  • Loading branch information
daniloercoli committed Jul 23, 2014
2 parents a0e973c + c6b0ff9 commit 90f9f22
Show file tree
Hide file tree
Showing 5 changed files with 286 additions and 0 deletions.
4 changes: 4 additions & 0 deletions WordPressUtils/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ apply plugin: 'maven'

repositories {
mavenCentral()
maven { url 'http://wordpress-mobile.github.io/WordPress-Android' }
}

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:+@aar' // org.wordpress version includes some fixes
compile 'com.android.support:support-v13:19.0.+'
}

android {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.wordpress.android.util;

import android.content.Context;
import android.view.Gravity;
import android.widget.Toast;

/**
* Provides a simplified way to show toast messages without having to create the toast, set the
* desired gravity, etc.
*/
public class ToastUtils {
public enum Duration {SHORT, LONG}

private ToastUtils() {
throw new AssertionError();
}

public static Toast showToast(Context context, int stringResId) {
return showToast(context, stringResId, Duration.SHORT);
}

public static Toast showToast(Context context, int stringResId, Duration duration) {
return showToast(context, context.getString(stringResId), duration);
}

public static Toast showToast(Context context, String text) {
return showToast(context, text, Duration.SHORT);
}

public static Toast showToast(Context context, String text, Duration duration) {
Toast toast = Toast.makeText(context, text,
(duration == Duration.SHORT ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG));
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return toast;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package org.wordpress.android.util.ptr;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;

import org.wordpress.android.util.R;

import uk.co.senab.actionbarpulltorefresh.library.DefaultHeaderTransformer;
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 Animation mHeaderOutAnimation;
private OnTopScrollChangedListener mOnTopScrollChangedListener;

public interface OnTopScrollChangedListener {
public void onTopScrollChanged(boolean scrolledOnTop);
}

public void setShowProgressBarOnly(boolean progressBarOnly) {
mShowProgressBarOnly = progressBarOnly;
}

@Override
public void onViewCreated(Activity activity, View headerView) {
super.onViewCreated(activity, headerView);
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();
}

@Override
public boolean showHeaderView() {
// Workaround to avoid this bug https://github.com/chrisbanes/ActionBar-PullToRefresh/issues/265
// Note, that also remove the alpha animation
resetContentLayoutAlpha();

boolean changeVis = mHeaderView.getVisibility() != View.VISIBLE;
mContentLayout.setVisibility(View.VISIBLE);
if (changeVis) {
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;
}

@Override
public void onPulled(float percentagePulled) {
super.onPulled(percentagePulled);
}

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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package org.wordpress.android.util.ptr;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;

import org.wordpress.android.util.R;
import org.wordpress.android.util.ToastUtils;
import org.wordpress.android.util.ToastUtils.Duration;

import java.lang.ref.WeakReference;
import java.util.Arrays;
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 RefreshListener mRefreshListener;
private WeakReference<Activity> mActivityRef;

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

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

public void init(Activity activity, PullToRefreshLayout pullToRefreshLayout, 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);
}

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

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

@Override
public void onRefreshStarted(View view) {
mRefreshListener.onRefreshStarted(view);
}

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

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

public void refreshAction() {
Activity activity = mActivityRef.get();
if (activity == null) {
return;
}
setRefreshing(true);
mRefreshListener.onRefreshStarted(mPullToRefreshLayout);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
int refreshHits = preferences.getInt(REFRESH_BUTTON_HIT_COUNT, 0);
refreshHits += 1;
if (TOAST_FREQUENCY.contains(refreshHits)) {
ToastUtils.showToast(activity, R.string.ptr_tip_message, Duration.LONG);
}
Editor editor = preferences.edit();
editor.putInt(REFRESH_BUTTON_HIT_COUNT, refreshHits);
editor.commit();
}

public void registerReceiver(Context context) {
if (context == null) {
return;
}
IntentFilter filter = new IntentFilter();
filter.addAction(BROADCAST_ACTION_REFRESH_MENU_PRESSED);
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
lbm.registerReceiver(mReceiver, filter);
}

public void unregisterReceiver(Context context) {
if (context == null) {
return;
}
try {
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
lbm.unregisterReceiver(mReceiver);
} catch (IllegalArgumentException e) {
// exception occurs if receiver already unregistered (safe to ignore)
}
}

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null || intent.getAction() == null) {
return;
}
if (intent.getAction().equals(BROADCAST_ACTION_REFRESH_MENU_PRESSED)) {
refreshAction();
}
}
};
}
4 changes: 4 additions & 0 deletions WordPressUtils/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="ptr_tip_message">Tip: Pull down to refresh</string>
</resources>

0 comments on commit 90f9f22

Please sign in to comment.