From bc373917b53dfebd0fde905dcd7916fd3e19ac53 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Wed, 26 Jan 2022 09:15:44 +0000 Subject: [PATCH] making the onboarding store a userId scoped generic store instead --- .../vector/app/core/extensions/DataStore.kt | 30 ------------------- .../im/vector/app/core/extensions/Session.kt | 4 +-- .../im/vector/app/features/MainActivity.kt | 10 +++---- .../onboarding/OnboardingViewModel.kt | 4 +-- .../VectorSessionStore.kt} | 22 +++++++------- 5 files changed, 19 insertions(+), 51 deletions(-) delete mode 100644 vector/src/main/java/im/vector/app/core/extensions/DataStore.kt rename vector/src/main/java/im/vector/app/features/{onboarding/store/OnboardingStore.kt => session/VectorSessionStore.kt} (69%) diff --git a/vector/src/main/java/im/vector/app/core/extensions/DataStore.kt b/vector/src/main/java/im/vector/app/core/extensions/DataStore.kt deleted file mode 100644 index 72e2408591..0000000000 --- a/vector/src/main/java/im/vector/app/core/extensions/DataStore.kt +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2022 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.core.extensions - -import androidx.datastore.core.DataStore -import androidx.datastore.preferences.core.Preferences -import androidx.datastore.preferences.core.edit - -suspend fun DataStore.removeKeysWithPrefix(prefix: String) { - edit { preferences -> - val keysToRemove = preferences.asMap().keys.filter { key -> key.name.startsWith(prefix) } - keysToRemove.forEach { - preferences.remove(it) - } - } -} 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 b350e0041c..87ed51522f 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,7 +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 im.vector.app.features.session.VectorSessionStore 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 @@ -78,4 +78,4 @@ fun Session.cannotLogoutSafely(): Boolean { !sharedSecretStorageService.isRecoverySetup()) } -fun Session.onboardingStore(context: Context) = OnboardingStore(context, myUserId) +fun Session.vectorStore(context: Context) = VectorSessionStore(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 25d0323577..33b735551c 100644 --- a/vector/src/main/java/im/vector/app/features/MainActivity.kt +++ b/vector/src/main/java/im/vector/app/features/MainActivity.kt @@ -28,8 +28,8 @@ 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.extensions.vectorStore import im.vector.app.core.platform.VectorBaseActivity import im.vector.app.core.utils.deleteAllFiles import im.vector.app.databinding.ActivityMainBinding @@ -37,11 +37,11 @@ import im.vector.app.features.analytics.VectorAnalytics import im.vector.app.features.home.HomeActivity import im.vector.app.features.home.ShortcutsHandler import im.vector.app.features.notifications.NotificationDrawerManager -import im.vector.app.features.onboarding.store.OnboardingStore import im.vector.app.features.pin.PinCodeStore import im.vector.app.features.pin.PinLocker import im.vector.app.features.pin.UnlockedActivity import im.vector.app.features.popup.PopupAlertManager +import im.vector.app.features.session.VectorSessionStore import im.vector.app.features.settings.VectorPreferences import im.vector.app.features.signout.hard.SignedOutActivity import im.vector.app.features.themes.ActivityOtherThemes @@ -146,7 +146,7 @@ class MainActivity : VectorBaseActivity(), UnlockedActivity return } - val onboardingStore = session.onboardingStore(this) + val onboardingStore = session.vectorStore(this) when { args.isAccountDeactivated -> { lifecycleScope.launch { @@ -187,7 +187,7 @@ class MainActivity : VectorBaseActivity(), UnlockedActivity Timber.w("Ignoring invalid token global error") } - private suspend fun doLocalCleanup(clearPreferences: Boolean, onboardingStore: OnboardingStore) { + private suspend fun doLocalCleanup(clearPreferences: Boolean, vectorSessionStore: VectorSessionStore) { // On UI Thread Glide.get(this@MainActivity).clearMemory() @@ -197,7 +197,7 @@ class MainActivity : VectorBaseActivity(), UnlockedActivity pinLocker.unlock() pinCodeStore.deleteEncodedPin() vectorAnalytics.onSignOut() - onboardingStore.clear() + vectorSessionStore.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 72379f17ea..8097e90206 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,7 +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.extensions.vectorStore import im.vector.app.core.platform.VectorViewModel import im.vector.app.core.resources.StringProvider import im.vector.app.core.utils.ensureTrailingSlash @@ -753,7 +753,7 @@ class OnboardingViewModel @AssistedInject constructor( private suspend fun onSessionCreated(session: Session) { awaitState().useCase?.let { useCase -> - session.onboardingStore(applicationContext).setUseCase(useCase) + session.vectorStore(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/session/VectorSessionStore.kt similarity index 69% rename from vector/src/main/java/im/vector/app/features/onboarding/store/OnboardingStore.kt rename to vector/src/main/java/im/vector/app/features/session/VectorSessionStore.kt index 3193c68f98..bd0b7e2cca 100644 --- a/vector/src/main/java/im/vector/app/features/onboarding/store/OnboardingStore.kt +++ b/vector/src/main/java/im/vector/app/features/session/VectorSessionStore.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package im.vector.app.features.onboarding.store +package im.vector.app.features.session import android.content.Context import androidx.datastore.core.DataStore @@ -22,40 +22,38 @@ import androidx.datastore.preferences.core.Preferences import androidx.datastore.preferences.core.edit import androidx.datastore.preferences.core.stringPreferencesKey import androidx.datastore.preferences.preferencesDataStore -import im.vector.app.core.extensions.removeKeysWithPrefix import im.vector.app.features.onboarding.FtueUseCase import kotlinx.coroutines.flow.first -private val Context.dataStore: DataStore by preferencesDataStore(name = "vector_onboarding") - /** * Local storage for: * - messaging use case (Enum/String) */ -class OnboardingStore constructor( +class VectorSessionStore constructor( private val context: Context, - private val myUserId: String + myUserId: String ) { + private val Context.dataStore: DataStore by preferencesDataStore(name = "vector_session_store_$myUserId") + private val useCaseKey = stringPreferencesKey("use_case") + suspend fun readUseCase() = context.dataStore.data.first().let { preferences -> - preferences[myUserId.toUseCaseKey()]?.let { FtueUseCase.from(it) } + preferences[useCaseKey]?.let { FtueUseCase.from(it) } } suspend fun setUseCase(useCase: FtueUseCase) { context.dataStore.edit { settings -> - settings[myUserId.toUseCaseKey()] = useCase.persistableValue + settings[useCaseKey] = useCase.persistableValue } } suspend fun resetUseCase() { context.dataStore.edit { settings -> - settings.remove(myUserId.toUseCaseKey()) + settings.remove(useCaseKey) } } suspend fun clear() { - context.dataStore.removeKeysWithPrefix(myUserId) + context.dataStore.edit { settings -> settings.clear() } } - - private fun String.toUseCaseKey() = stringPreferencesKey("$this-use_case") }