Skip to content

Instantly share code, notes, and snippets.

@mariusz-blaszczak
Created August 28, 2018 10:32
Show Gist options
  • Save mariusz-blaszczak/4a1942e0f7579ddc3e0cd769252b09b9 to your computer and use it in GitHub Desktop.
Save mariusz-blaszczak/4a1942e0f7579ddc3e0cd769252b09b9 to your computer and use it in GitHub Desktop.
object UUIDProvider {
private const val NAME = "${BuildConfig.APPLICATION_ID}.LOCK_ID"
private const val KEY_ID = "${BuildConfig.APPLICATION_ID}.KEY_ID"
private var cacheId: String? = null
fun provide(context: Context): String {
if (cacheId != null) return cacheId!!
val sharedPreferences = context.getSharedPreferences(NAME, MODE_PRIVATE)
val currentId = sharedPreferences.getString(KEY_ID, null)
if (currentId != null) {
cacheId = currentId
return currentId
}
val newId = UUID.randomUUID().toString()
sharedPreferences.edit().putString(KEY_ID, newId).apply()
cacheId = newId
return newId
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment