Skip to content

Commit

Permalink
Added a contentLoadingSmoothProgressBar
Browse files Browse the repository at this point in the history
  • Loading branch information
castorflex committed Jan 25, 2014
1 parent e784e6e commit e6ce517
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
##0.3.3

- Added a ContentLoadingSmoothProgressBar (see also [ContentLoadingProgressBar](https://android.googlesource.com/platform/frameworks/support/+/refs/heads/master/v4/java/android/support/v4/widget/ContentLoadingProgressBar.java))

##0.3.2

- targetSdkVersion is now 14. We just need holo style.
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
VERSION_NAME=0.3.2
VERSION_CODE=9
VERSION_NAME=0.3.3
VERSION_CODE=10
GROUP=com.github.castorflex.smoothprogressbar

#storeFile=nice try
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package fr.castorflex.android.smoothprogressbar;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

/**
* This is a copy of the ContentLoadingProgressBar from the support library, but extends
* SmoothProgressBar.
*/
public class ContentLoadingSmoothProgressBar extends SmoothProgressBar {

private static final int MIN_SHOW_TIME = 500; // ms
private static final int MIN_DELAY = 500; // ms

private long mStartTime = -1;

private boolean mPostedHide = false;

private boolean mPostedShow = false;

private boolean mDismissed = false;

private final Runnable mDelayedHide = new Runnable() {

@Override
public void run() {
mPostedHide = false;
mStartTime = -1;
setVisibility(View.GONE);
}
};

private final Runnable mDelayedShow = new Runnable() {

@Override
public void run() {
mPostedShow = false;
if (!mDismissed) {
mStartTime = System.currentTimeMillis();
setVisibility(View.VISIBLE);
}
}
};

public ContentLoadingSmoothProgressBar(Context context) {
this(context, null);
}

public ContentLoadingSmoothProgressBar(Context context, AttributeSet attrs) {
super(context, attrs, 0);
}

@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
removeCallbacks();
}

@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
removeCallbacks();
}

private void removeCallbacks() {
removeCallbacks(mDelayedHide);
removeCallbacks(mDelayedShow);
}

/**
* Hide the progress view if it is visible. The progress view will not be
* hidden until it has been shown for at least a minimum show time. If the
* progress view was not yet visible, cancels showing the progress view.
*/
public void hide() {
mDismissed = true;
removeCallbacks(mDelayedShow);
long diff = System.currentTimeMillis() - mStartTime;
if (diff >= MIN_SHOW_TIME || mStartTime == -1) {
// The progress spinner has been shown long enough
// OR was not shown yet. If it wasn't shown yet,
// it will just never be shown.
setVisibility(View.GONE);
} else {
// The progress spinner is shown, but not long enough,
// so put a delayed message in to hide it when its been
// shown long enough.
if (!mPostedHide) {
postDelayed(mDelayedHide, MIN_SHOW_TIME - diff);
mPostedHide = true;
}
}
}

/**
* Show the progress view after waiting for a minimum delay. If
* during that time, hide() is called, the view is never made visible.
*/
public void show() {
// Reset the start time.
mStartTime = -1;
mDismissed = false;
removeCallbacks(mDelayedHide);
if (!mPostedShow) {
postDelayed(mDelayedShow, MIN_DELAY);
mPostedShow = true;
}
}
}

0 comments on commit e6ce517

Please sign in to comment.