Make AnalyticsStore an interface and create DefaultAnalyticsStore
This commit is contained in:
parent
053ef9b9ab
commit
6acc86641a
1 changed files with 22 additions and 9 deletions
|
|
@ -23,7 +23,9 @@ import androidx.datastore.preferences.core.booleanPreferencesKey
|
||||||
import androidx.datastore.preferences.core.edit
|
import androidx.datastore.preferences.core.edit
|
||||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||||
import androidx.datastore.preferences.preferencesDataStore
|
import androidx.datastore.preferences.preferencesDataStore
|
||||||
|
import com.squareup.anvil.annotations.ContributesBinding
|
||||||
import io.element.android.libraries.core.bool.orFalse
|
import io.element.android.libraries.core.bool.orFalse
|
||||||
|
import io.element.android.libraries.di.AppScope
|
||||||
import io.element.android.libraries.di.ApplicationContext
|
import io.element.android.libraries.di.ApplicationContext
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||||
|
|
@ -41,44 +43,55 @@ private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(na
|
||||||
* - did ask user consent (Boolean);
|
* - did ask user consent (Boolean);
|
||||||
* - analytics Id (String).
|
* - analytics Id (String).
|
||||||
*/
|
*/
|
||||||
class AnalyticsStore @Inject constructor(
|
interface AnalyticsStore {
|
||||||
|
val userConsentFlow: Flow<Boolean>
|
||||||
|
val didAskUserConsentFlow: Flow<Boolean>
|
||||||
|
val analyticsIdFlow: Flow<String>
|
||||||
|
suspend fun setUserConsent(newUserConsent: Boolean)
|
||||||
|
suspend fun setDidAskUserConsent(newValue: Boolean = true)
|
||||||
|
suspend fun setAnalyticsId(newAnalyticsId: String)
|
||||||
|
suspend fun reset()
|
||||||
|
}
|
||||||
|
|
||||||
|
@ContributesBinding(AppScope::class)
|
||||||
|
class DefaultAnalyticsStore @Inject constructor(
|
||||||
@ApplicationContext private val context: Context
|
@ApplicationContext private val context: Context
|
||||||
) {
|
) : AnalyticsStore {
|
||||||
private val userConsent = booleanPreferencesKey("user_consent")
|
private val userConsent = booleanPreferencesKey("user_consent")
|
||||||
private val didAskUserConsent = booleanPreferencesKey("did_ask_user_consent")
|
private val didAskUserConsent = booleanPreferencesKey("did_ask_user_consent")
|
||||||
private val analyticsId = stringPreferencesKey("analytics_id")
|
private val analyticsId = stringPreferencesKey("analytics_id")
|
||||||
|
|
||||||
val userConsentFlow: Flow<Boolean> = context.dataStore.data
|
override val userConsentFlow: Flow<Boolean> = context.dataStore.data
|
||||||
.map { preferences -> preferences[userConsent].orFalse() }
|
.map { preferences -> preferences[userConsent].orFalse() }
|
||||||
.distinctUntilChanged()
|
.distinctUntilChanged()
|
||||||
|
|
||||||
val didAskUserConsentFlow: Flow<Boolean> = context.dataStore.data
|
override val didAskUserConsentFlow: Flow<Boolean> = context.dataStore.data
|
||||||
.map { preferences -> preferences[didAskUserConsent].orFalse() }
|
.map { preferences -> preferences[didAskUserConsent].orFalse() }
|
||||||
.distinctUntilChanged()
|
.distinctUntilChanged()
|
||||||
|
|
||||||
val analyticsIdFlow: Flow<String> = context.dataStore.data
|
override val analyticsIdFlow: Flow<String> = context.dataStore.data
|
||||||
.map { preferences -> preferences[analyticsId].orEmpty() }
|
.map { preferences -> preferences[analyticsId].orEmpty() }
|
||||||
.distinctUntilChanged()
|
.distinctUntilChanged()
|
||||||
|
|
||||||
suspend fun setUserConsent(newUserConsent: Boolean) {
|
override suspend fun setUserConsent(newUserConsent: Boolean) {
|
||||||
context.dataStore.edit { settings ->
|
context.dataStore.edit { settings ->
|
||||||
settings[userConsent] = newUserConsent
|
settings[userConsent] = newUserConsent
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun setDidAskUserConsent(newValue: Boolean = true) {
|
override suspend fun setDidAskUserConsent(newValue: Boolean) {
|
||||||
context.dataStore.edit { settings ->
|
context.dataStore.edit { settings ->
|
||||||
settings[didAskUserConsent] = newValue
|
settings[didAskUserConsent] = newValue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun setAnalyticsId(newAnalyticsId: String) {
|
override suspend fun setAnalyticsId(newAnalyticsId: String) {
|
||||||
context.dataStore.edit { settings ->
|
context.dataStore.edit { settings ->
|
||||||
settings[analyticsId] = newAnalyticsId
|
settings[analyticsId] = newAnalyticsId
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun reset() {
|
override suspend fun reset() {
|
||||||
context.dataStore.edit {
|
context.dataStore.edit {
|
||||||
it.clear()
|
it.clear()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue