Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added listener for skipped showcase + minor improvements #87

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
public interface IShowcaseListener {
void onShowcaseDisplayed(MaterialShowcaseView showcaseView);
void onShowcaseDismissed(MaterialShowcaseView showcaseView);
/**
* Notify when singleUse is enabled and showcase has been fired before
*/
void onShowcaseSkipped(MaterialShowcaseView showcaseView);
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public class MaterialShowcaseView extends FrameLayout implements View.OnTouchLis
private Handler mHandler;
private long mDelayInMillis = ShowcaseConfig.DEFAULT_DELAY;
private int mBottomMargin = 0;
private int mRightMargin = 0;
private boolean mSingleUse = false; // should display only once
private PrefsManager mPrefsManager; // used to store state doe single use mode
List<IShowcaseListener> mListeners; // external listeners who want to observe when we show and dismiss
Expand Down Expand Up @@ -226,6 +227,28 @@ private void notifyOnDisplayed() {
}
}

/**
* Notify when {@link #singleUse(String)} is enabled and showcase has been fired before
* @see #singleUse(String)
*/
private void notifyOnSkipped() {

if(mListeners != null){
for (IShowcaseListener listener : mListeners) {
listener.onShowcaseSkipped(this);
}
mListeners.clear();
mListeners = null;
}

/**
* internal listener used by sequence for storing progress within the sequence
*/
if (mDetachedListener != null) {
mDetachedListener.onShowcaseDetached(this, mWasDismissed);
}
}

private void notifyOnDismissed() {
if (mListeners != null) {
for (IShowcaseListener listener : mListeners) {
Expand Down Expand Up @@ -274,10 +297,16 @@ public void setTarget(Target target) {
*/
if (!mRenderOverNav && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBottomMargin = getSoftButtonsBarSizePort((Activity) getContext());
mRightMargin = getSoftButtonsBarHorizontalSizePort((Activity) getContext());

FrameLayout.LayoutParams contentLP = (LayoutParams) getLayoutParams();

if (contentLP != null && contentLP.bottomMargin != mBottomMargin)
contentLP.bottomMargin = mBottomMargin;
if (contentLP != null) {
if (contentLP.bottomMargin != mBottomMargin)
contentLP.bottomMargin = mBottomMargin;
if (contentLP.rightMargin != mRightMargin)
contentLP.rightMargin = mRightMargin;
}
}

// apply the target position
Expand Down Expand Up @@ -682,6 +711,8 @@ public MaterialShowcaseView build() {
throw new IllegalArgumentException("Unsupported shape type: " + shapeType);
}
}
if(showcaseView.mTitleTextView != null && showcaseView.mTitleTextView.getText().equals(""))
showcaseView.mTitleTextView.setVisibility(GONE);

return showcaseView;
}
Expand Down Expand Up @@ -738,6 +769,7 @@ public boolean show(final Activity activity) {
*/
if (mSingleUse) {
if (mPrefsManager.hasFired()) {
notifyOnSkipped();
return false;
} else {
mPrefsManager.setFired();
Expand Down Expand Up @@ -845,6 +877,23 @@ public static int getSoftButtonsBarSizePort(Activity activity) {
}
return 0;
}

public static int getSoftButtonsBarHorizontalSizePort(Activity activity) {
// getRealMetrics is only available with API 17 and +
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {

DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
int usableWidth = metrics.widthPixels;
activity.getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
int realWidth = metrics.widthPixels;
if (realWidth > usableWidth)
return realWidth - usableWidth;
else
return 0;
}
return 0;
}

private void setRenderOverNavigationBar(boolean mRenderOverNav) {
this.mRenderOverNav = mRenderOverNav;
Expand Down