From 73b80b1c7d7fb901ff91f87faafc11ab18b96515 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Tue, 25 Jan 2022 17:03:13 +0000 Subject: [PATCH] providing the onboarding store via session extension - avoids needing to know about the user id for each read/write --- .../im/vector/app/core/extensions/Session.kt | 3 +++ .../im/vector/app/features/MainActivity.kt | 14 ++++++----- .../onboarding/OnboardingViewModel.kt | 5 ++-- .../onboarding/store/OnboardingStore.kt | 23 ++++++++++--------- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/vector/src/main/java/im/vector/app/core/extensions/Session.kt b/vector/src/main/java/im/vector/app/core/extensions/Session.kt index 90b08ef92b..b350e0041c 100644 --- a/vector/src/main/java/im/vector/app/core/extensions/Session.kt +++ b/vector/src/main/java/im/vector/app/core/extensions/Session.kt @@ -21,6 +21,7 @@ import androidx.core.content.ContextCompat import androidx.lifecycle.Lifecycle import androidx.lifecycle.ProcessLifecycleOwner import im.vector.app.core.services.VectorSyncService +import im.vector.app.features.onboarding.store.OnboardingStore import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.crypto.keysbackup.KeysBackupState import org.matrix.android.sdk.api.session.sync.FilterService @@ -76,3 +77,5 @@ fun Session.cannotLogoutSafely(): Boolean { // That are not backed up !sharedSecretStorageService.isRecoverySetup()) } + +fun Session.onboardingStore(context: Context) = OnboardingStore(context, myUserId) diff --git a/vector/src/main/java/im/vector/app/features/MainActivity.kt b/vector/src/main/java/im/vector/app/features/MainActivity.kt index 98a967e925..25d0323577 100644 --- a/vector/src/main/java/im/vector/app/features/MainActivity.kt +++ b/vector/src/main/java/im/vector/app/features/MainActivity.kt @@ -28,6 +28,7 @@ import dagger.hilt.android.AndroidEntryPoint import im.vector.app.R import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.error.ErrorFormatter +import im.vector.app.core.extensions.onboardingStore import im.vector.app.core.extensions.startSyncing import im.vector.app.core.platform.VectorBaseActivity import im.vector.app.core.utils.deleteAllFiles @@ -99,7 +100,6 @@ class MainActivity : VectorBaseActivity(), UnlockedActivity @Inject lateinit var pinLocker: PinLocker @Inject lateinit var popupAlertManager: PopupAlertManager @Inject lateinit var vectorAnalytics: VectorAnalytics - @Inject lateinit var onboardingStore: OnboardingStore override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -145,13 +145,15 @@ class MainActivity : VectorBaseActivity(), UnlockedActivity startNextActivityAndFinish() return } + + val onboardingStore = session.onboardingStore(this) when { args.isAccountDeactivated -> { lifecycleScope.launch { // Just do the local cleanup Timber.w("Account deactivated, start app") sessionHolder.clearActiveSession() - doLocalCleanup(clearPreferences = true, userId = session.myUserId) + doLocalCleanup(clearPreferences = true, onboardingStore) startNextActivityAndFinish() } } @@ -165,14 +167,14 @@ class MainActivity : VectorBaseActivity(), UnlockedActivity } Timber.w("SIGN_OUT: success, start app") sessionHolder.clearActiveSession() - doLocalCleanup(clearPreferences = true, userId = session.myUserId) + doLocalCleanup(clearPreferences = true, onboardingStore) startNextActivityAndFinish() } } args.clearCache -> { lifecycleScope.launch { session.clearCache() - doLocalCleanup(clearPreferences = false, userId = session.myUserId) + doLocalCleanup(clearPreferences = false, onboardingStore) session.startSyncing(applicationContext) startNextActivityAndFinish() } @@ -185,7 +187,7 @@ class MainActivity : VectorBaseActivity(), UnlockedActivity Timber.w("Ignoring invalid token global error") } - private suspend fun doLocalCleanup(clearPreferences: Boolean, userId: String) { + private suspend fun doLocalCleanup(clearPreferences: Boolean, onboardingStore: OnboardingStore) { // On UI Thread Glide.get(this@MainActivity).clearMemory() @@ -195,7 +197,7 @@ class MainActivity : VectorBaseActivity(), UnlockedActivity pinLocker.unlock() pinCodeStore.deleteEncodedPin() vectorAnalytics.onSignOut() - onboardingStore.clear(userId) + onboardingStore.clear() } withContext(Dispatchers.IO) { // On BG thread diff --git a/vector/src/main/java/im/vector/app/features/onboarding/OnboardingViewModel.kt b/vector/src/main/java/im/vector/app/features/onboarding/OnboardingViewModel.kt index 9c3743f372..72379f17ea 100644 --- a/vector/src/main/java/im/vector/app/features/onboarding/OnboardingViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/onboarding/OnboardingViewModel.kt @@ -32,6 +32,7 @@ import im.vector.app.core.di.MavericksAssistedViewModelFactory import im.vector.app.core.di.hiltMavericksViewModelFactory import im.vector.app.core.extensions.configureAndStart import im.vector.app.core.extensions.exhaustive +import im.vector.app.core.extensions.onboardingStore import im.vector.app.core.platform.VectorViewModel import im.vector.app.core.resources.StringProvider import im.vector.app.core.utils.ensureTrailingSlash @@ -45,7 +46,6 @@ import im.vector.app.features.login.LoginMode import im.vector.app.features.login.ReAuthHelper import im.vector.app.features.login.ServerType import im.vector.app.features.login.SignMode -import im.vector.app.features.onboarding.store.OnboardingStore import kotlinx.coroutines.Job import kotlinx.coroutines.launch import org.matrix.android.sdk.api.MatrixPatterns.getDomain @@ -78,7 +78,6 @@ class OnboardingViewModel @AssistedInject constructor( private val stringProvider: StringProvider, private val homeServerHistoryService: HomeServerHistoryService, private val vectorFeatures: VectorFeatures, - private val onboardingStore: OnboardingStore, private val analyticsTracker: AnalyticsTracker ) : VectorViewModel(initialState) { @@ -754,7 +753,7 @@ class OnboardingViewModel @AssistedInject constructor( private suspend fun onSessionCreated(session: Session) { awaitState().useCase?.let { useCase -> - onboardingStore.setUseCase(userId = session.myUserId, useCase) + session.onboardingStore(applicationContext).setUseCase(useCase) } activeSessionHolder.setActiveSession(session) diff --git a/vector/src/main/java/im/vector/app/features/onboarding/store/OnboardingStore.kt b/vector/src/main/java/im/vector/app/features/onboarding/store/OnboardingStore.kt index 96ad6ec050..3193c68f98 100644 --- a/vector/src/main/java/im/vector/app/features/onboarding/store/OnboardingStore.kt +++ b/vector/src/main/java/im/vector/app/features/onboarding/store/OnboardingStore.kt @@ -25,7 +25,6 @@ import androidx.datastore.preferences.preferencesDataStore import im.vector.app.core.extensions.removeKeysWithPrefix import im.vector.app.features.onboarding.FtueUseCase import kotlinx.coroutines.flow.first -import javax.inject.Inject private val Context.dataStore: DataStore by preferencesDataStore(name = "vector_onboarding") @@ -33,27 +32,29 @@ private val Context.dataStore: DataStore by preferencesDataStore(na * Local storage for: * - messaging use case (Enum/String) */ -class OnboardingStore @Inject constructor( - private val context: Context +class OnboardingStore constructor( + private val context: Context, + private val myUserId: String ) { - suspend fun readUseCase(userId: String) = context.dataStore.data.first().let { preferences -> - preferences[userId.toUseCaseKey()]?.let { FtueUseCase.from(it) } + + suspend fun readUseCase() = context.dataStore.data.first().let { preferences -> + preferences[myUserId.toUseCaseKey()]?.let { FtueUseCase.from(it) } } - suspend fun setUseCase(userId: String, useCase: FtueUseCase) { + suspend fun setUseCase(useCase: FtueUseCase) { context.dataStore.edit { settings -> - settings[userId.toUseCaseKey()] = useCase.persistableValue + settings[myUserId.toUseCaseKey()] = useCase.persistableValue } } - suspend fun resetUseCase(userId: String) { + suspend fun resetUseCase() { context.dataStore.edit { settings -> - settings.remove(userId.toUseCaseKey()) + settings.remove(myUserId.toUseCaseKey()) } } - suspend fun clear(userId: String) { - context.dataStore.removeKeysWithPrefix(userId) + suspend fun clear() { + context.dataStore.removeKeysWithPrefix(myUserId) } private fun String.toUseCaseKey() = stringPreferencesKey("$this-use_case")