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

View File

@ -753,7 +753,7 @@ class OnboardingViewModel @AssistedInject constructor(
private suspend fun onSessionCreated(session: Session) { private suspend fun onSessionCreated(session: Session) {
awaitState().useCase?.let { useCase -> awaitState().useCase?.let { useCase ->
onboardingStore.setUseCase(useCase) onboardingStore.setUseCase(userId = session.myUserId, useCase)
} }
activeSessionHolder.setActiveSession(session) 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.edit
import androidx.datastore.preferences.core.stringPreferencesKey import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore import androidx.datastore.preferences.preferencesDataStore
import im.vector.app.core.extensions.removeKeysWithPrefix
import im.vector.app.features.onboarding.FtueUseCase import im.vector.app.features.onboarding.FtueUseCase
import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.first
import javax.inject.Inject import javax.inject.Inject
@ -35,25 +36,25 @@ private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(na
class OnboardingStore @Inject constructor( class OnboardingStore @Inject constructor(
private val context: Context private val context: Context
) { ) {
private val useCaseKey = stringPreferencesKey("use_case") 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[useCaseKey]?.let { FtueUseCase.from(it) }
} }
suspend fun setUseCase(useCase: FtueUseCase) { suspend fun setUseCase(userId: String, useCase: FtueUseCase) {
context.dataStore.edit { settings -> 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 -> context.dataStore.edit { settings ->
settings.remove(useCaseKey) settings.remove(userId.toUseCaseKey())
} }
} }
suspend fun clear() { suspend fun clear(userId: String) {
context.dataStore.edit { it.clear() } context.dataStore.removeKeysWithPrefix(userId)
} }
private fun String.toUseCaseKey() = stringPreferencesKey("$this-use_case")
} }