storing an retrieving the use case selection by user

- will enable multi account support in the future
This commit is contained in:
Adam Brown 2022-01-21 13:50:51 +00:00
parent c4ac03949c
commit f44ccd739e
4 changed files with 47 additions and 16 deletions

View file

@ -0,0 +1,30 @@
/*
* 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

@ -151,7 +151,7 @@ class MainActivity : VectorBaseActivity<ActivityMainBinding>(), UnlockedActivity
// Just do the local cleanup
Timber.w("Account deactivated, start app")
sessionHolder.clearActiveSession()
doLocalCleanup(clearPreferences = true)
doLocalCleanup(clearPreferences = true, userId = session.myUserId)
startNextActivityAndFinish()
}
}
@ -165,14 +165,14 @@ class MainActivity : VectorBaseActivity<ActivityMainBinding>(), UnlockedActivity
}
Timber.w("SIGN_OUT: success, start app")
sessionHolder.clearActiveSession()
doLocalCleanup(clearPreferences = true)
doLocalCleanup(clearPreferences = true, userId = session.myUserId)
startNextActivityAndFinish()
}
}
args.clearCache -> {
lifecycleScope.launch {
session.clearCache()
doLocalCleanup(clearPreferences = false)
doLocalCleanup(clearPreferences = false, userId = session.myUserId)
session.startSyncing(applicationContext)
startNextActivityAndFinish()
}
@ -185,7 +185,7 @@ class MainActivity : VectorBaseActivity<ActivityMainBinding>(), UnlockedActivity
Timber.w("Ignoring invalid token global error")
}
private suspend fun doLocalCleanup(clearPreferences: Boolean) {
private suspend fun doLocalCleanup(clearPreferences: Boolean, userId: String) {
// On UI Thread
Glide.get(this@MainActivity).clearMemory()
@ -195,7 +195,7 @@ class MainActivity : VectorBaseActivity<ActivityMainBinding>(), UnlockedActivity
pinLocker.unlock()
pinCodeStore.deleteEncodedPin()
vectorAnalytics.onSignOut()
onboardingStore.clear()
onboardingStore.clear(userId)
}
withContext(Dispatchers.IO) {
// On BG thread

View file

@ -753,7 +753,7 @@ class OnboardingViewModel @AssistedInject constructor(
private suspend fun onSessionCreated(session: Session) {
awaitState().useCase?.let { useCase ->
onboardingStore.setUseCase(useCase)
onboardingStore.setUseCase(userId = session.myUserId, useCase)
}
activeSessionHolder.setActiveSession(session)

View file

@ -22,6 +22,7 @@ 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
import javax.inject.Inject
@ -35,25 +36,25 @@ private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(na
class OnboardingStore @Inject constructor(
private val context: Context
) {
private val useCaseKey = stringPreferencesKey("use_case")
suspend fun readUseCase() = context.dataStore.data.first().let { preferences ->
preferences[useCaseKey]?.let { FtueUseCase.from(it) }
suspend fun readUseCase(userId: String) = context.dataStore.data.first().let { preferences ->
preferences[userId.toUseCaseKey()]?.let { FtueUseCase.from(it) }
}
suspend fun setUseCase(useCase: FtueUseCase) {
suspend fun setUseCase(userId: String, useCase: FtueUseCase) {
context.dataStore.edit { settings ->
settings[useCaseKey] = useCase.persistableValue
settings[userId.toUseCaseKey()] = useCase.persistableValue
}
}
suspend fun resetUseCase() {
suspend fun resetUseCase(userId: String) {
context.dataStore.edit { settings ->
settings.remove(useCaseKey)
settings.remove(userId.toUseCaseKey())
}
}
suspend fun clear() {
context.dataStore.edit { it.clear() }
suspend fun clear(userId: String) {
context.dataStore.removeKeysWithPrefix(userId)
}
private fun String.toUseCaseKey() = stringPreferencesKey("$this-use_case")
}