Skip to content

Commit efbdd70

Browse files
committed
storing an retrieving the use case selection by user
- will enable multi account support in the future
1 parent 0d26291 commit efbdd70

File tree

4 files changed

+47
-16
lines changed

4 files changed

+47
-16
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2022 New Vector Ltd
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package im.vector.app.core.extensions
18+
19+
import androidx.datastore.core.DataStore
20+
import androidx.datastore.preferences.core.Preferences
21+
import androidx.datastore.preferences.core.edit
22+
23+
suspend fun DataStore<Preferences>.removeKeysWithPrefix(prefix: String) {
24+
edit { preferences ->
25+
val keysToRemove = preferences.asMap().keys.filter { key -> key.name.startsWith(prefix) }
26+
keysToRemove.forEach {
27+
preferences.remove(it)
28+
}
29+
}
30+
}

vector/src/main/java/im/vector/app/features/MainActivity.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class MainActivity : VectorBaseActivity<ActivityMainBinding>(), UnlockedActivity
151151
// Just do the local cleanup
152152
Timber.w("Account deactivated, start app")
153153
sessionHolder.clearActiveSession()
154-
doLocalCleanup(clearPreferences = true)
154+
doLocalCleanup(clearPreferences = true, userId = session.myUserId)
155155
startNextActivityAndFinish()
156156
}
157157
}
@@ -165,14 +165,14 @@ class MainActivity : VectorBaseActivity<ActivityMainBinding>(), UnlockedActivity
165165
}
166166
Timber.w("SIGN_OUT: success, start app")
167167
sessionHolder.clearActiveSession()
168-
doLocalCleanup(clearPreferences = true)
168+
doLocalCleanup(clearPreferences = true, userId = session.myUserId)
169169
startNextActivityAndFinish()
170170
}
171171
}
172172
args.clearCache -> {
173173
lifecycleScope.launch {
174174
session.clearCache()
175-
doLocalCleanup(clearPreferences = false)
175+
doLocalCleanup(clearPreferences = false, userId = session.myUserId)
176176
session.startSyncing(applicationContext)
177177
startNextActivityAndFinish()
178178
}
@@ -185,7 +185,7 @@ class MainActivity : VectorBaseActivity<ActivityMainBinding>(), UnlockedActivity
185185
Timber.w("Ignoring invalid token global error")
186186
}
187187

188-
private suspend fun doLocalCleanup(clearPreferences: Boolean) {
188+
private suspend fun doLocalCleanup(clearPreferences: Boolean, userId: String) {
189189
// On UI Thread
190190
Glide.get(this@MainActivity).clearMemory()
191191

@@ -195,7 +195,7 @@ class MainActivity : VectorBaseActivity<ActivityMainBinding>(), UnlockedActivity
195195
pinLocker.unlock()
196196
pinCodeStore.deleteEncodedPin()
197197
vectorAnalytics.onSignOut()
198-
onboardingStore.clear()
198+
onboardingStore.clear(userId)
199199
}
200200
withContext(Dispatchers.IO) {
201201
// On BG thread

vector/src/main/java/im/vector/app/features/onboarding/OnboardingViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ class OnboardingViewModel @AssistedInject constructor(
753753

754754
private suspend fun onSessionCreated(session: Session) {
755755
awaitState().useCase?.let { useCase ->
756-
onboardingStore.setUseCase(useCase)
756+
onboardingStore.setUseCase(userId = session.myUserId, useCase)
757757
}
758758
activeSessionHolder.setActiveSession(session)
759759

vector/src/main/java/im/vector/app/features/onboarding/store/OnboardingStore.kt

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import androidx.datastore.preferences.core.Preferences
2222
import androidx.datastore.preferences.core.edit
2323
import androidx.datastore.preferences.core.stringPreferencesKey
2424
import androidx.datastore.preferences.preferencesDataStore
25+
import im.vector.app.core.extensions.removeKeysWithPrefix
2526
import im.vector.app.features.onboarding.FtueUseCase
2627
import kotlinx.coroutines.flow.first
2728
import javax.inject.Inject
@@ -35,25 +36,25 @@ private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(na
3536
class OnboardingStore @Inject constructor(
3637
private val context: Context
3738
) {
38-
private val useCaseKey = stringPreferencesKey("use_case")
39-
40-
suspend fun readUseCase() = context.dataStore.data.first().let { preferences ->
41-
preferences[useCaseKey]?.let { FtueUseCase.from(it) }
39+
suspend fun readUseCase(userId: String) = context.dataStore.data.first().let { preferences ->
40+
preferences[userId.toUseCaseKey()]?.let { FtueUseCase.from(it) }
4241
}
4342

44-
suspend fun setUseCase(useCase: FtueUseCase) {
43+
suspend fun setUseCase(userId: String, useCase: FtueUseCase) {
4544
context.dataStore.edit { settings ->
46-
settings[useCaseKey] = useCase.persistableValue
45+
settings[userId.toUseCaseKey()] = useCase.persistableValue
4746
}
4847
}
4948

50-
suspend fun resetUseCase() {
49+
suspend fun resetUseCase(userId: String) {
5150
context.dataStore.edit { settings ->
52-
settings.remove(useCaseKey)
51+
settings.remove(userId.toUseCaseKey())
5352
}
5453
}
5554

56-
suspend fun clear() {
57-
context.dataStore.edit { it.clear() }
55+
suspend fun clear(userId: String) {
56+
context.dataStore.removeKeysWithPrefix(userId)
5857
}
58+
59+
private fun String.toUseCaseKey() = stringPreferencesKey("$this-use_case")
5960
}

0 commit comments

Comments
 (0)