making the onboarding store a userId scoped generic store instead

This commit is contained in:
Adam Brown 2022-01-26 09:15:44 +00:00
parent 73b80b1c7d
commit bc373917b5
5 changed files with 19 additions and 51 deletions

View file

@ -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<Preferences>.removeKeysWithPrefix(prefix: String) {
edit { preferences ->
val keysToRemove = preferences.asMap().keys.filter { key -> key.name.startsWith(prefix) }
keysToRemove.forEach {
preferences.remove(it)
}
}
}

View file

@ -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)

View file

@ -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<ActivityMainBinding>(), UnlockedActivity
return
}
val onboardingStore = session.onboardingStore(this)
val onboardingStore = session.vectorStore(this)
when {
args.isAccountDeactivated -> {
lifecycleScope.launch {
@ -187,7 +187,7 @@ class MainActivity : VectorBaseActivity<ActivityMainBinding>(), 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<ActivityMainBinding>(), UnlockedActivity
pinLocker.unlock()
pinCodeStore.deleteEncodedPin()
vectorAnalytics.onSignOut()
onboardingStore.clear()
vectorSessionStore.clear()
}
withContext(Dispatchers.IO) {
// On BG thread

View file

@ -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)

View file

@ -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<Preferences> 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<Preferences> 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")
}