Analytics: setup the first classes

This commit is contained in:
Benoit Marty 2021-11-23 13:42:46 +01:00 committed by Benoit Marty
parent 7c4460b812
commit b68e9e1f7f
4 changed files with 191 additions and 0 deletions

View File

@ -31,6 +31,8 @@ import im.vector.app.core.error.DefaultErrorFormatter
import im.vector.app.core.error.ErrorFormatter
import im.vector.app.core.time.Clock
import im.vector.app.core.time.DefaultClock
import im.vector.app.features.analytics.VectorAnalytics
import im.vector.app.features.analytics.impl.DefaultVectorAnalytics
import im.vector.app.features.invite.AutoAcceptInvites
import im.vector.app.features.invite.CompileTimeAutoAcceptInvites
import im.vector.app.features.navigation.DefaultNavigator
@ -57,6 +59,9 @@ abstract class VectorBindModule {
@Binds
abstract fun bindNavigator(navigator: DefaultNavigator): Navigator
@Binds
abstract fun bindVectorAnalytics(analytics: DefaultVectorAnalytics): VectorAnalytics
@Binds
abstract fun bindErrorFormatter(formatter: DefaultErrorFormatter): ErrorFormatter

View File

@ -0,0 +1,56 @@
/*
* Copyright (c) 2021 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.features.analytics
import kotlinx.coroutines.flow.Flow
interface VectorAnalytics {
/**
* Return a Flow of Boolean, true if the user has given their consent
*/
fun getUserConsent(): Flow<Boolean>
/**
* Update the user consent value
*/
suspend fun setUserConsent(userConsent: Boolean)
/**
* Return a Flow of Boolean, true if the user has been asked for their consent
*/
fun didAskUserConsent(): Flow<Boolean>
/**
* Store the fact that the user has been asked for their consent
*/
suspend fun setDidAskUserConsent(didAskUserConsent: Boolean)
/**
* Return a Flow of String, used for analytics Id
*/
fun getAnalyticsId(): Flow<String>
/**
* Update analyticsId from the AccountData
*/
suspend fun setAnalyticsId(analyticsId: String)
/**
* To be called when a session is destroyed
*/
suspend fun onSignOut()
}

View File

@ -0,0 +1,55 @@
/*
* Copyright (c) 2021 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.features.analytics.impl
import im.vector.app.features.analytics.VectorAnalytics
import im.vector.app.features.analytics.store.AnalyticsStore
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject
class DefaultVectorAnalytics @Inject constructor(
private val analyticsStore: AnalyticsStore
) : VectorAnalytics {
override fun getUserConsent(): Flow<Boolean> {
return analyticsStore.userConsentFlow
}
override suspend fun setUserConsent(userConsent: Boolean) {
analyticsStore.setUserConsent(userConsent)
}
override fun didAskUserConsent(): Flow<Boolean> {
return analyticsStore.didAskUserConsentFlow
}
override suspend fun setDidAskUserConsent(didAskUserConsent: Boolean) {
analyticsStore.setDidAskUserConsent(didAskUserConsent)
}
override fun getAnalyticsId(): Flow<String> {
return analyticsStore.analyticsIdFlow
}
override suspend fun setAnalyticsId(analyticsId: String) {
analyticsStore.setAnalyticsId(analyticsId)
}
override suspend fun onSignOut() {
// reset the analyticsId
setAnalyticsId("")
}
}

View File

@ -0,0 +1,75 @@
/*
* Copyright (c) 2021 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.features.analytics.store
import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import org.matrix.android.sdk.api.extensions.orFalse
import javax.inject.Inject
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "vector_analytics")
/**
* Local storage for:
* - user consent (Boolean)
* - did ask user consent (Boolean)
* - analytics Id (String)
*/
class AnalyticsStore @Inject constructor(
private val context: Context
) {
private val userConsent = booleanPreferencesKey("user_consent")
private val didAskUserConsent = booleanPreferencesKey("did_ask_user_consent")
private val analyticsId = stringPreferencesKey("analytics_id")
val userConsentFlow: Flow<Boolean> = context.dataStore.data.map { preferences ->
preferences[userConsent].orFalse()
}
val didAskUserConsentFlow: Flow<Boolean> = context.dataStore.data.map { preferences ->
preferences[didAskUserConsent].orFalse()
}
val analyticsIdFlow: Flow<String> = context.dataStore.data.map { preferences ->
preferences[analyticsId].orEmpty()
}
suspend fun setUserConsent(newUserConsent: Boolean) {
context.dataStore.edit { settings ->
settings[userConsent] = newUserConsent
}
}
suspend fun setDidAskUserConsent(newDidAskUserConsent: Boolean) {
context.dataStore.edit { settings ->
settings[didAskUserConsent] = newDidAskUserConsent
}
}
suspend fun setAnalyticsId(newAnalyticsId: String) {
context.dataStore.edit { settings ->
settings[analyticsId] = newAnalyticsId
}
}
}