Skip to content

Commit f0e0333

Browse files
committed
ShowOnce paramater added
1 parent cb5cadb commit f0e0333

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ BubbleShowCaseBuilder(this) //Activity instance
4141
.descriptionTextSize(15) //Subtitle text size in SP (default value 14sp)
4242
.image(imageDrawable) //Bubble main image
4343
.closeActionImage(CloseImageDrawable) //Custom close action image
44+
.showOnce("BUBBLE_SHOW_CASE_ID") //Id to show only once the BubbleShowCase
4445
.listener(listener(object : BubbleShowCaseListener{ //Listener for user actions
4546
override fun onTargetClick(bubbleShowCase: BubbleShowCase) {
4647
//Called when the user clicks the target

bubbleshowcase/src/main/java/com/elconfidencial/bubbleshowcase/BubbleShowCase.kt

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package com.elconfidencial.bubbleshowcase
33

44
import android.app.Activity
55
import android.content.Context
6+
import android.content.Context.MODE_PRIVATE
7+
import android.content.SharedPreferences
68
import android.graphics.Bitmap
79
import android.graphics.RectF
810
import android.graphics.drawable.Drawable
@@ -21,6 +23,8 @@ import java.lang.ref.WeakReference
2123
*/
2224

2325
class BubbleShowCase(builder: BubbleShowCaseBuilder){
26+
private val SHARED_PREFS_NAME = "BubbleShowCasePrefs"
27+
2428
private val FOREGROUND_LAYOUT_ID = 731
2529

2630
private val DURATION_SHOW_CASE_ANIMATION = 200 //ms
@@ -47,6 +51,7 @@ class BubbleShowCase(builder: BubbleShowCaseBuilder){
4751
private val mTextColor: Int? = builder.mTextColor
4852
private val mTitleTextSize: Int? = builder.mTitleTextSize
4953
private val mSubtitleTextSize: Int? = builder.mSubtitleTextSize
54+
private val mShowOnce: String? = builder.mShowOnce
5055
private val mDisableTargetClick: Boolean = builder.mDisableTargetClick
5156
private val mArrowPositionList: MutableList<ArrowPosition> = builder.mArrowPositionList
5257
private val mTargetView: WeakReference<View>? = builder.mTargetView
@@ -62,6 +67,15 @@ class BubbleShowCase(builder: BubbleShowCaseBuilder){
6267
private var bubbleMessageViewBuilder: BubbleMessageView.Builder? = null
6368

6469
fun show(){
70+
if(mShowOnce != null){
71+
if(isBubbleShowCaseHasBeenShowedPreviously(mShowOnce)){
72+
notifyDismissToSequenceListener()
73+
return
74+
} else{
75+
registerBubbleShowCaseInPreferences(mShowOnce)
76+
}
77+
}
78+
6579
val rootView = getViewRoot(mActivity.get()!!)
6680
foregroundLayoutWithBlur = getForegroundLayoutWithBlur()
6781
bubbleMessageViewBuilder = getBubbleMessageViewBuilder()
@@ -95,7 +109,7 @@ class BubbleShowCase(builder: BubbleShowCaseBuilder){
95109
}
96110

97111
fun dismiss() {
98-
mSequenceListener?.let { mSequenceListener.onDismiss() }
112+
notifyDismissToSequenceListener()
99113
if (foregroundLayoutWithBlur != null && isLastOfSequence) {
100114
//Remove foreground layout if the BubbleShowCase is the last of the sequence
101115
val rootView = getViewRoot(mActivity.get()!!)
@@ -107,6 +121,10 @@ class BubbleShowCase(builder: BubbleShowCaseBuilder){
107121
}
108122
}
109123

124+
private fun notifyDismissToSequenceListener(){
125+
mSequenceListener?.let { mSequenceListener.onDismiss() }
126+
}
127+
110128
private fun getViewRoot(activity: Activity): ViewGroup {
111129
val androidContent = activity.findViewById<ViewGroup>(android.R.id.content)
112130
return androidContent.parent.parent as ViewGroup
@@ -143,6 +161,27 @@ class BubbleShowCase(builder: BubbleShowCaseBuilder){
143161
})
144162
}
145163

164+
private fun isBubbleShowCaseHasBeenShowedPreviously(id: String): Boolean{
165+
val mPrefs = mActivity.get()!!.getSharedPreferences(SHARED_PREFS_NAME, MODE_PRIVATE)
166+
return getString(mPrefs, id)!=null
167+
}
168+
169+
private fun registerBubbleShowCaseInPreferences(id: String){
170+
val mPrefs = mActivity.get()!!.getSharedPreferences(SHARED_PREFS_NAME, MODE_PRIVATE)
171+
setString(mPrefs, id, id)
172+
}
173+
174+
fun getString(mPrefs: SharedPreferences, key: String): String? {
175+
return mPrefs.getString(key, null)
176+
}
177+
178+
fun setString(mPrefs: SharedPreferences, key: String, value: String) {
179+
val editor = mPrefs.edit()
180+
editor.putString(key, value)
181+
editor.apply()
182+
}
183+
184+
146185
/**
147186
* This function takes a screenshot of the targetView, creating an ImageView from it. This new ImageView is also set on the layout passed by param
148187
*/

bubbleshowcase/src/main/java/com/elconfidencial/bubbleshowcase/BubbleShowCaseBuilder.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class BubbleShowCaseBuilder{
2222
internal var mTitleTextSize: Int? = null
2323
internal var mSubtitleTextSize: Int? = null
2424
internal var mDisableTargetClick: Boolean = false
25+
internal var mShowOnce: String? = null
2526
internal var mIsFirstOfSequence: Boolean? = null
2627
internal var mIsLastOfSequence: Boolean? = null
2728
internal val mArrowPositionList = ArrayList<BubbleShowCase.ArrowPosition>()
@@ -108,6 +109,15 @@ class BubbleShowCaseBuilder{
108109
return this
109110
}
110111

112+
/**
113+
* If an unique id is passed in this function, this BubbleShowCase will only be showed once
114+
* - ID to identify the BubbleShowCase
115+
*/
116+
fun showOnce(id: String): BubbleShowCaseBuilder {
117+
mShowOnce = id
118+
return this
119+
}
120+
111121
/**
112122
* Target view to be highlighted. Set a TargetView is essential to figure out BubbleShowCase position
113123
* - If a target view is not defined, the BubbleShowCase final position will be the center of the screen

0 commit comments

Comments
 (0)