Skip to content

Instantly share code, notes, and snippets.

@webserveis
Last active April 30, 2021 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webserveis/7c3d319171c279c4923611bad4ed57fc to your computer and use it in GitHub Desktop.
Save webserveis/7c3d319171c279c4923611bad4ed57fc to your computer and use it in GitHub Desktop.
Computar clicks para publicidad Admob

ClickManager

Clase para computar los clicks para controlar la publicidad en Admob

Por defecto se lanzará el evento cuando llegue a 5

private val clickManager = ClickManager(
    MyApplication.prefs.getSharedPreferences(), {
        Log.d(TAG, "lanzar anuncio a pantalla completa, clics: $it ")
    })

Para contar un click

clickManager.incCount()

Personalización

Lanzar evento cuando se llegue a 7 clicks y reutilizable con otra instancia mediante su UID

private val clickManager = ClickManager(
    MyApplication.prefs.getSharedPreferences(), {
        Log.d(TAG, "lanzar anuncio a pantalla completa, clics: $it ")
    },
    7,
    "click7"
)
class ClickManager(
private val sp: SharedPreferences,
private val triggerTask: (Int) -> Unit,
private val triggerClick: Int = TRIGGER_CLICK,
private val uid: String? = null
) {
companion object {
private const val KEY_CLICKS_COUNT = "clicks_count"
private val TRIGGER_CLICK = 5
}
val clickCount: Int
get() = sp.getInt(KEY_CLICKS_COUNT + uid, 0)
init {
evaluateTrigger()
}
fun incCount() {
sp.edit { putInt(KEY_CLICKS_COUNT + uid, clickCount + 1) }
evaluateTrigger()
}
fun resetCount() {
sp.edit { putInt(KEY_CLICKS_COUNT + uid, 0) }
}
private fun evaluateTrigger() {
if (clickCount >= triggerClick) {
triggerTask(clickCount)
resetCount()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment