Skip to content

Commit

Permalink
Refactor BalloonPersistence
Browse files Browse the repository at this point in the history
  • Loading branch information
skydoves committed Jan 1, 2020
1 parent 747a3c1 commit 1d2cfed
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class ProfileBalloonFactory : Balloon.Factory() {
setBackgroundColorResource(R.color.background900)
setBalloonAnimation(BalloonAnimation.CIRCULAR)
setDismissWhenShowAgain(true)
setPreferenceName("profile balloon")
setShowTime(3)
setLifecycleOwner(lifecycle)
}
}
Expand Down
10 changes: 5 additions & 5 deletions balloon/src/main/java/com/skydoves/balloon/Balloon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class Balloon(
var onBalloonClickListener: OnBalloonClickListener? = null
var onBalloonDismissListener: OnBalloonDismissListener? = null
var onBalloonOutsideTouchListener: OnBalloonOutsideTouchListener? = null
private val balloonPreferenceManager = BalloonPreferenceManager(context).getInstance()
private val balloonPersistence = BalloonPersistence.getInstance(context)

init {
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
Expand Down Expand Up @@ -235,11 +235,11 @@ class Balloon(

@MainThread
private inline fun show(anchor: View, crossinline block: () -> Unit) {
if (!isShowing) {
if (!this.isShowing) {
this.isShowing = true
builder.preferenceName?.let {
if (balloonPreferenceManager.shouldShowUP(it, builder.showTimes)) {
balloonPreferenceManager.putIncrementedTimes(it)
this.builder.preferenceName?.let {
if (balloonPersistence.shouldShowUP(it, builder.showTimes)) {
balloonPersistence.putIncrementedTimes(it)
} else return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,7 @@ import android.content.Context
import android.content.SharedPreferences

/** BalloonPreferenceManager helps to persist showing times. */
@Suppress("PrivatePropertyName", "MemberVisibilityCanBePrivate")
internal class BalloonPreferenceManager(context: Context) {

private val SHOWED_UP = "SHOWED_UP"

init {
balloonPreferenceManager = this
sharedPreferenceManager = context.getSharedPreferences("com.skydoves.balloon", Context.MODE_PRIVATE)
}

/** get a singleton instance of the [BalloonPreferenceManager]. */
fun getInstance(): BalloonPreferenceManager {
return balloonPreferenceManager
}
internal class BalloonPersistence {

/** should show or not the popup. */
fun shouldShowUP(name: String, times: Int): Boolean {
Expand All @@ -42,21 +29,42 @@ internal class BalloonPreferenceManager(context: Context) {

/** gets show-up times from the preference. */
fun getTimes(name: String): Int {
return sharedPreferenceManager.getInt(SHOWED_UP + name, 0)
return sharedPreferenceManager.getInt(getPersistName(name), 0)
}

/** puts show-up times to the preference. */
fun putTimes(name: String, times: Int) {
sharedPreferenceManager.edit().putInt(SHOWED_UP + name, times).apply()
sharedPreferenceManager.edit().putInt(getPersistName(name), times).apply()
}

/** puts a incremented show-up times to the preference. */
fun putIncrementedTimes(name: String) {
putTimes(name, getTimes(name) + 1)
}

/** removes a showed times history from the preference. */
fun removePersistedData(name: String) =
sharedPreferenceManager.edit().remove(SHOWED_UP + name).apply()

/** clears all of [Balloon] showed times history on the application. */
fun clearAllPersistedData() = sharedPreferenceManager.edit().clear().apply()

companion object {
lateinit var balloonPreferenceManager: BalloonPreferenceManager
lateinit var sharedPreferenceManager: SharedPreferences
@Volatile
private var instance: BalloonPersistence? = null
private lateinit var sharedPreferenceManager: SharedPreferences
private const val SHOWED_UP = "SHOWED_UP"

@JvmStatic
fun getInstance(context: Context): BalloonPersistence =
instance ?: synchronized(this) {
instance ?: BalloonPersistence().also {
instance = it
sharedPreferenceManager =
context.getSharedPreferences("com.skydoves.balloon", Context.MODE_PRIVATE)
}
}

fun getPersistName(name: String) = SHOWED_UP + name
}
}

0 comments on commit 1d2cfed

Please sign in to comment.