Created
August 28, 2018 10:32
-
-
Save mariusz-blaszczak/4a1942e0f7579ddc3e0cd769252b09b9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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