Skip to content

Commit

Permalink
Added new feature
Browse files Browse the repository at this point in the history
Users can choose, with a check box, whether they want to be 'warned' again

To do so, just call the 'showUntil' method with the showcaseID for that action
  • Loading branch information
leocabral committed Sep 15, 2015
1 parent 29a51dc commit 5577fbd
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.TextView;

Expand All @@ -46,6 +47,7 @@ public class MaterialShowcaseView extends FrameLayout implements View.OnTouchLis
private boolean mUseAutoRadius = true;
private View mContentBox;
private TextView mContentTextView;
private CheckBox mContentCheckBox;
private TextView mDismissButton;
private int mGravity;
private int mContentBottomMargin;
Expand All @@ -60,10 +62,13 @@ public class MaterialShowcaseView extends FrameLayout implements View.OnTouchLis
private long mDelayInMillis = ShowcaseConfig.DEFAULT_DELAY;
private int mBottomMargin = 0;
private boolean mSingleUse = false; // should display only once
private boolean mShowAgain = true; // should display once again
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
private UpdateOnGlobalLayout mLayoutListener;
private IDetachedListener mDetachedListener;
private String singleUseShowcaseID;
private String showAgainShowcaseID;

public MaterialShowcaseView(Context context) {
super(context);
Expand Down Expand Up @@ -109,6 +114,7 @@ private void init(Context context) {
View contentView = LayoutInflater.from(getContext()).inflate(R.layout.showcase_content, this, true);
mContentBox = contentView.findViewById(R.id.content_box);
mContentTextView = (TextView) contentView.findViewById(R.id.tv_content);
mContentCheckBox = (CheckBox) contentView.findViewById(R.id.tv_never_show);
mDismissButton = (TextView) contentView.findViewById(R.id.tv_dismiss);
mDismissButton.setOnClickListener(this);
}
Expand Down Expand Up @@ -176,7 +182,7 @@ protected void onDetachedFromWindow() {
* Ensure we reset the flag so the showcase display again.
*/
if (!mWasDismissed && mSingleUse && mPrefsManager != null) {
mPrefsManager.resetShowcase();
mPrefsManager.resetShowcase(singleUseShowcaseID);
}


Expand Down Expand Up @@ -246,7 +252,7 @@ public void setTarget(Target target) {
mBottomMargin = getSoftButtonsBarSizePort((Activity) getContext());
FrameLayout.LayoutParams contentLP = (LayoutParams) getLayoutParams();

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

Expand Down Expand Up @@ -329,6 +335,12 @@ private void setContentText(CharSequence contentText) {
}
}

private void setCheckBoxText(CharSequence text, String showcaseID) {
if (mContentCheckBox != null) {
mContentCheckBox.setText(text);
}
}

private void setDismissText(CharSequence dismissText) {
if (mDismissButton != null) {
mDismissButton.setText(dismissText);
Expand Down Expand Up @@ -466,6 +478,22 @@ public Builder setContentText(CharSequence text) {
return this;
}

/**
* This feature is intend to be used as a flag
* so the user can tell if he wants to be warned every time.
*/
public Builder showUntil(String showAgainShowcaseID) {
showcaseView.showUntil(showAgainShowcaseID);
return this;
}

/**
* Set the text shown on the check box.
*/
public Builder setCheckBoxText(String showAgainShowcaseID) {
showcaseView.showUntil(showAgainShowcaseID);
return this;
}

/**
* Use auto radius, if true then the showcase circle will auto size based on the target view
Expand Down Expand Up @@ -533,12 +561,22 @@ public MaterialShowcaseView show() {
showcaseView.show(activity);
return showcaseView;
}
}

private void showUntil(String showcaseID) {
mShowAgain = true;
mContentCheckBox.setVisibility(VISIBLE);
showAgainShowcaseID = showcaseID;

if (mPrefsManager == null) {
mPrefsManager = new PrefsManager(getContext());
}
}

private void singleUse(String showcaseID) {
mSingleUse = true;
mPrefsManager = new PrefsManager(getContext(), showcaseID);
singleUseShowcaseID = showcaseID;
mPrefsManager = new PrefsManager(getContext());
}

public void removeFromWindow() {
Expand Down Expand Up @@ -580,13 +618,17 @@ public boolean show(final Activity activity) {
* if we're in single use mode and have already shot our bolt then do nothing
*/
if (mSingleUse) {
if (mPrefsManager.hasFired()) {
if (mPrefsManager.hasFired(singleUseShowcaseID)) {
return false;
} else {
mPrefsManager.setFired();
mPrefsManager.setFired(singleUseShowcaseID);
}
}

if (mPrefsManager.hasFired(showAgainShowcaseID)) {
return false;
}

((ViewGroup) activity.getWindow().getDecorView()).addView(this);

setShouldRender(true);
Expand Down Expand Up @@ -621,6 +663,10 @@ public void hide() {
} else {
removeFromWindow();
}

if (mContentCheckBox.isChecked()) {
mPrefsManager.setFired(showAgainShowcaseID);
}
}

public void fadeIn() {
Expand Down Expand Up @@ -649,7 +695,7 @@ public void onAnimationEnd() {
}

public void resetSingleUse() {
if (mSingleUse && mPrefsManager != null) mPrefsManager.resetShowcase();
if (mSingleUse && mPrefsManager != null) mPrefsManager.resetShowcase(singleUseShowcaseID);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,90 @@ public class PrefsManager {
private String showcaseID = null;
private Context context;

public PrefsManager(Context context) {
this.context = context;
}

/***
* @deprecated Pass the showcaseID when needed
*/
@Deprecated
public PrefsManager(Context context, String showcaseID) {
this.context = context;
this.showcaseID = showcaseID;
}


/***
* METHODS FOR INDIVIDUAL SHOWCASE VIEWS
* @deprecated send the showcaseID via param
*/
@Deprecated
boolean hasFired() {
int status = getSequenceStatus();
return (status == SEQUENCE_FINISHED);
}

boolean hasFired(String showcaseID) {
int status = getSequenceStatus(showcaseID);
return (status == SEQUENCE_FINISHED);
}

/***
* @deprecated send the showcaseID via param
*/
@Deprecated
void setFired() {
setSequenceStatus(SEQUENCE_FINISHED);
}

void setFired(String showcaseID) {
setSequenceStatus(SEQUENCE_FINISHED, showcaseID);
}

/***
* METHODS FOR SHOWCASE SEQUENCES
* @deprecated send the showcaseID via param
*/
@Deprecated
int getSequenceStatus() {
return context
.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
.getInt(STATUS + showcaseID, SEQUENCE_NEVER_STARTED);

}

int getSequenceStatus(String showcaseID) {
return context
.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
.getInt(STATUS + showcaseID, SEQUENCE_NEVER_STARTED);

}

/***
* @deprecated send the showcaseID via param
*/
@Deprecated
void setSequenceStatus(int status) {
SharedPreferences internal = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
internal.edit().putInt(STATUS + showcaseID, status).apply();
}

void setSequenceStatus(int status, String showcaseID) {
SharedPreferences internal = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
internal.edit().putInt(STATUS + showcaseID, status).apply();
}

/***
* @deprecated send the showcaseID via param
*/
@Deprecated
public void resetShowcase() {
resetShowcase(context, showcaseID);
}

public void resetShowcase(String showcaseID) {
resetShowcase(context, showcaseID);
}

static void resetShowcase(Context context, String showcaseID) {
SharedPreferences internal = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
internal.edit().putInt(STATUS + showcaseID, SEQUENCE_NEVER_STARTED).apply();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ public class ShowcaseConfig {
private long mDelay = DEFAULT_DELAY;
private int mMaskColour;
private int mContentTextColor;
private int mCheckBoxTextColor;
private int mDismissTextColor;
private long mFadeDuration = DEFAULT_FADE_TIME;

public ShowcaseConfig() {
mMaskColour = Color.parseColor(ShowcaseConfig.DEFAULT_MASK_COLOUR);
mContentTextColor = Color.parseColor("#ffffff");
mDismissTextColor = Color.parseColor("#ffffff");
mCheckBoxTextColor = Color.parseColor("#ffffff");
}

public long getDelay() {
Expand Down Expand Up @@ -63,4 +65,12 @@ public long getFadeDuration() {
public void setFadeDuration(long fadeDuration) {
this.mFadeDuration = fadeDuration;
}

public int getCheckBoxTextColor() {
return mCheckBoxTextColor;
}

public void setCheckBoxTextColor(int mCheckBoxTextColor) {
this.mCheckBoxTextColor = mCheckBoxTextColor;
}
}
16 changes: 12 additions & 4 deletions library/src/main/res/layout/showcase_content.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content_box"
android:layout_width="match_parent"
Expand All @@ -14,21 +13,30 @@
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:paddingLeft="5dp"
android:textColor="@android:color/white"
android:textSize="20dp" />

<CheckBox
android:id="@+id/tv_never_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/dont_show_again"
android:textColor="@android:color/white"
android:visibility="gone" />

<TextView
android:id="@+id/tv_dismiss"
android:background="?android:attr/selectableItemBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="10dp"
android:textColor="@android:color/white"
android:textSize="22dp" />
</LinearLayout>
4 changes: 4 additions & 0 deletions library/src/main/res/values-pt/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="dont_show_again">Não mostrar novamente</string>
</resources>
4 changes: 4 additions & 0 deletions library/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="dont_show_again">Don\'t show this again</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private void presentShowcaseView(int withDelay) {
.setDismissText("GOT IT")
.setContentText("This is some amazing feature you should know about")
.setDelay(withDelay) // optional but starting animations immediately in onCreate can make them choppy
.singleUse(SHOWCASE_ID) // provide a unique ID used to ensure it is only shown once
.showUntil(SHOWCASE_ID)
.show();
}

Expand Down

0 comments on commit 5577fbd

Please sign in to comment.