feat(CON-168) : [SDK] - Généré et manipuler l'id unique d'installation de l'application.
This commit is contained in:
@ -1,6 +1,8 @@
|
||||
plugins {
|
||||
alias(libs.plugins.android.library)
|
||||
alias(libs.plugins.kotlin.android)
|
||||
alias(libs.plugins.hilt)
|
||||
alias(libs.plugins.ksp)
|
||||
}
|
||||
|
||||
android {
|
||||
@ -30,11 +32,17 @@ android {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
||||
// AndroidX
|
||||
implementation(libs.androidx.core.ktx)
|
||||
implementation(libs.androidx.appcompat)
|
||||
|
||||
// Preferences data store
|
||||
implementation(libs.preferencesDataStore)
|
||||
|
||||
// Hilt
|
||||
implementation(libs.hilt.android)
|
||||
ksp(libs.hilt.compiler)
|
||||
|
||||
// Test
|
||||
testImplementation(libs.test.junit)
|
||||
androidTestImplementation(libs.androidx.junit)
|
||||
|
@ -0,0 +1,48 @@
|
||||
package fr.openium.consentium.data.local
|
||||
|
||||
import android.content.Context
|
||||
import androidx.datastore.core.DataStore
|
||||
import androidx.datastore.preferences.core.Preferences
|
||||
import androidx.datastore.preferences.core.edit
|
||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||
import androidx.datastore.preferences.preferencesDataStore
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import kotlinx.coroutines.flow.firstOrNull
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import java.util.UUID
|
||||
import javax.inject.Inject
|
||||
|
||||
private val DATASTORE_NAME = "consentium_datastore"
|
||||
|
||||
val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = DATASTORE_NAME)
|
||||
|
||||
internal class ConsentiumDataStore @Inject constructor(@ApplicationContext context: Context) {
|
||||
|
||||
companion object {
|
||||
// Installation unique id
|
||||
private val INSTALLATION_UNIQUE_ID = stringPreferencesKey("INSTALLATION_UNIQUE_ID")
|
||||
}
|
||||
|
||||
private val _dataStore = context.dataStore
|
||||
|
||||
fun getInstallationUniqueId(): String =
|
||||
runBlocking {
|
||||
val uuid : String ? = _dataStore.data.map { prefs ->
|
||||
prefs[INSTALLATION_UNIQUE_ID]
|
||||
}.firstOrNull()
|
||||
if (uuid == null) {
|
||||
val newUuid = UUID.randomUUID().toString()
|
||||
setInstallationUniqueId(newUuid)
|
||||
newUuid
|
||||
} else {
|
||||
uuid
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun setInstallationUniqueId(installationUniqueId: String) {
|
||||
_dataStore.edit { prefs ->
|
||||
prefs[INSTALLATION_UNIQUE_ID] = installationUniqueId
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user