From afb0d01afa139ea9e7128d5ad1524efff397157a Mon Sep 17 00:00:00 2001 From: Louis Legrand Date: Tue, 10 Dec 2024 15:09:08 +0100 Subject: [PATCH] =?UTF-8?q?feat(CON-168)=20:=20[SDK]=20-=20G=C3=A9n=C3=A9r?= =?UTF-8?q?=C3=A9=20et=20manipuler=20l'id=20unique=20d'installation=20de?= =?UTF-8?q?=20l'application.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 3 ++ consentium/build.gradle.kts | 10 +++- .../data/local/PreferenceDataStore.kt | 48 +++++++++++++++++++ gradle/libs.versions.toml | 11 ++++- 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 consentium/src/main/java/fr/openium/consentium/data/local/PreferenceDataStore.kt diff --git a/build.gradle.kts b/build.gradle.kts index 0917e96..409a872 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,4 +14,7 @@ plugins { // Agp alias(libs.plugins.android.library) apply false + + // Hilt + alias(libs.plugins.hilt) apply false } \ No newline at end of file diff --git a/consentium/build.gradle.kts b/consentium/build.gradle.kts index 6505ed7..d4d5e77 100644 --- a/consentium/build.gradle.kts +++ b/consentium/build.gradle.kts @@ -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) diff --git a/consentium/src/main/java/fr/openium/consentium/data/local/PreferenceDataStore.kt b/consentium/src/main/java/fr/openium/consentium/data/local/PreferenceDataStore.kt new file mode 100644 index 0000000..2d0c4ac --- /dev/null +++ b/consentium/src/main/java/fr/openium/consentium/data/local/PreferenceDataStore.kt @@ -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 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 + } + } +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 448ed8f..e32bb5c 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -14,7 +14,7 @@ appcompat = "1.7.0" lifecycle = "2.8.7" # Hilt -hilt = "2.50" +hilt = "2.51.1" hiltNavigation = "1.2.0" # Compose @@ -31,6 +31,9 @@ junit = "4.13.2" espressoCore = "3.6.1" junitExtVersion = "1.2.1" +# Preferences DataStore +preferencesDataStore = "1.1.1" + # Plugins agp = "8.7.3" kotlin = "2.0.0" @@ -51,7 +54,7 @@ androidx-lifecycle-compose = { group = "androidx.lifecycle", name = "lifecycle-r # Hilt hilt-android = { module = "com.google.dagger:hilt-android", version.ref = "hilt" } -hilt-compiler = { module = "com.google.dagger:hilt-compiler", version.ref = "hilt" } +hilt-compiler = { module = "com.google.dagger:hilt-android-compiler", version.ref = "hilt" } hilt-navigation-compose = { module = "androidx.hilt:hilt-navigation-compose", version.ref = "hiltNavigation" } # Timber @@ -67,6 +70,9 @@ compose-material3 = { group = "androidx.compose.material3", name = "material3" } # Material material = { group = "com.google.android.material", name = "material", version.ref = "material" } +# Preferences DataStore +preferencesDataStore = { group = "androidx.datastore", name = "datastore-preferences", version.ref = "preferencesDataStore" } + # Test test-junit = { group = "junit", name = "junit", version.ref = "junit" } test-androidx-junit = { group = "androidx.test.ext", name = "junit-ktx", version.ref = "junitExtVersion" } @@ -79,6 +85,7 @@ kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" } kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" } ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" } android-library = { id = "com.android.library", version.ref = "agp" } +hilt = { id = "com.google.dagger.hilt.android", version.ref = "hilt" } [bundles] androidx = ["androidx-core-ktx", "androidx-activity-compose"]