Skip to content

Instantly share code, notes, and snippets.

@adrianhall
Last active September 4, 2018 20:24
Show Gist options
  • Save adrianhall/f5acd51b27dc66daae681675c6f1b5ca to your computer and use it in GitHub Desktop.
Save adrianhall/f5acd51b27dc66daae681675c6f1b5ca to your computer and use it in GitHub Desktop.
/**
* Where the preferences are stored on the system
*/
private val preferencesFile: String
get() = "${this::class.java.simpleName}.prefs"
/**
* The name of the custom attribute
*/
private val categoriesAttributeName = "categories"
/**
* The shared preferences area object
*/
private var prefs: SharedPreferences = context.getSharedPreferences(preferencesFile, 0)
init {
// Read the current categories list, if present
val categories = prefs.getStringSet(categoriesAttributeName, emptySet())
if (categories.size > 0) {
val endpoint = pinpoint.targetingClient.currentEndpoint()
endpoint.addAttribute(categoriesAttributeName, categories.toList())
pinpoint.targetingClient.updateEndpointProfile(endpoint)
}
pinpoint.sessionClient.startSession()
pinpoint.analyticsClient.submitEvents()
}
/**
* Update the categories within the endpoint profile
*/
override fun updateCategories(categories: List<String>) {
prefs.edit().putStringSet(categoriesAttributeName, categories.toSet()).apply()
val endpoint = pinpoint.targetingClient.currentEndpoint()
endpoint.addAttribute(categoriesAttributeName, categories)
pinpoint.targetingClient.updateEndpointProfile(endpoint)
recordEvent("_endpoint.updateCategories")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment