@@ -3,6 +3,8 @@ package com.elconfidencial.bubbleshowcase
33
44import android.app.Activity
55import android.content.Context
6+ import android.content.Context.MODE_PRIVATE
7+ import android.content.SharedPreferences
68import android.graphics.Bitmap
79import android.graphics.RectF
810import android.graphics.drawable.Drawable
@@ -21,6 +23,8 @@ import java.lang.ref.WeakReference
2123 */
2224
2325class 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 */
0 commit comments