Use SharedPreferences.edit helper whenever possible (#636)

This commit is contained in:
Brian Yencho 2024-01-16 10:01:13 -06:00 committed by Álison Fernandes
parent 87ea0da23f
commit c428a57ca8
5 changed files with 106 additions and 103 deletions

View file

@ -1,5 +1,6 @@
package com.x8bit.bitwarden.data.auth.datasource.disk
import androidx.core.content.edit
import app.cash.turbine.test
import com.x8bit.bitwarden.data.auth.datasource.disk.model.AccountJson
import com.x8bit.bitwarden.data.auth.datasource.disk.model.EnvironmentUrlDataJson
@ -56,7 +57,7 @@ class AuthDiskSourceTest {
val testId = "testId"
// Update preferences to hold test value
fakeSharedPreferences.edit().putString(rememberedUniqueAppIdKey, testId).apply()
fakeSharedPreferences.edit { putString(rememberedUniqueAppIdKey, testId) }
assertEquals(testId, authDiskSource.uniqueAppId)
}
@ -77,7 +78,7 @@ class AuthDiskSourceTest {
)
// Update SharedPreferences updates the repository
fakeSharedPreferences.edit().putString(rememberedEmailKey, null).apply()
fakeSharedPreferences.edit { putString(rememberedEmailKey, null) }
assertNull(authDiskSource.rememberedEmailAddress)
}
@ -101,7 +102,7 @@ class AuthDiskSourceTest {
)
// Update SharedPreferences updates the repository
fakeSharedPreferences.edit().putString(userStateKey, null).apply()
fakeSharedPreferences.edit { putString(userStateKey, null) }
assertNull(authDiskSource.userState)
}
@ -157,12 +158,12 @@ class AuthDiskSourceTest {
val mockUserId = "mockUserId"
val mockLastActiveTime = 123456789L
fakeSharedPreferences
.edit()
.putLong(
"${lastActiveTimeBaseKey}_$mockUserId",
mockLastActiveTime,
)
.apply()
.edit {
putLong(
"${lastActiveTimeBaseKey}_$mockUserId",
mockLastActiveTime,
)
}
val actual = authDiskSource.getLastActiveTimeMillis(userId = mockUserId)
assertEquals(
mockLastActiveTime,
@ -197,9 +198,9 @@ class AuthDiskSourceTest {
val mockLastActiveTime = 123456789L
val lastActiveTimeKey = "${lastActiveTimeBaseKey}_$mockUserId"
fakeSharedPreferences
.edit()
.putLong(lastActiveTimeKey, mockLastActiveTime)
.apply()
.edit {
putLong(lastActiveTimeKey, mockLastActiveTime)
}
assertTrue(fakeSharedPreferences.contains(lastActiveTimeKey))
authDiskSource.storeLastActiveTimeMillis(
userId = mockUserId,
@ -214,12 +215,12 @@ class AuthDiskSourceTest {
val mockUserId = "mockUserId"
val mockUserKey = "mockUserKey"
fakeSharedPreferences
.edit()
.putString(
"${userKeyBaseKey}_$mockUserId",
mockUserKey,
)
.apply()
.edit {
putString(
"${userKeyBaseKey}_$mockUserId",
mockUserKey,
)
}
val actual = authDiskSource.getUserKey(userId = mockUserId)
assertEquals(
mockUserKey,
@ -253,12 +254,12 @@ class AuthDiskSourceTest {
val mockUserId = "mockUserId"
val mockPrivateKey = "mockPrivateKey"
fakeSharedPreferences
.edit()
.putString(
"${privateKeyBaseKey}_$mockUserId",
mockPrivateKey,
)
.apply()
.edit {
putString(
"${privateKeyBaseKey}_$mockUserId",
mockPrivateKey,
)
}
val actual = authDiskSource.getPrivateKey(userId = mockUserId)
assertEquals(
mockPrivateKey,
@ -291,12 +292,12 @@ class AuthDiskSourceTest {
val mockUserId = "mockUserId"
val mockUserAutoUnlockKey = "mockUserAutoUnlockKey"
fakeEncryptedSharedPreferences
.edit()
.putString(
"${userAutoUnlockKeyBaseKey}_$mockUserId",
mockUserAutoUnlockKey,
)
.apply()
.edit {
putString(
"${userAutoUnlockKeyBaseKey}_$mockUserId",
mockUserAutoUnlockKey,
)
}
val actual = authDiskSource.getUserAutoUnlockKey(userId = mockUserId)
assertEquals(
mockUserAutoUnlockKey,
@ -330,12 +331,12 @@ class AuthDiskSourceTest {
val mockUserId = "mockUserId"
val mockPinProtectedUserKey = "mockPinProtectedUserKey"
fakeSharedPreferences
.edit()
.putString(
"${pinProtectedUserKeyBaseKey}_$mockUserId",
mockPinProtectedUserKey,
)
.apply()
.edit {
putString(
"${pinProtectedUserKeyBaseKey}_$mockUserId",
mockPinProtectedUserKey,
)
}
val actual = authDiskSource.getPinProtectedUserKey(userId = mockUserId)
assertEquals(
mockPinProtectedUserKey,
@ -369,12 +370,12 @@ class AuthDiskSourceTest {
val mockUserId = "mockUserId"
val mockEncryptedPin = "mockEncryptedPin"
fakeSharedPreferences
.edit()
.putString(
"${encryptedPinBaseKey}_$mockUserId",
mockEncryptedPin,
)
.apply()
.edit {
putString(
"${encryptedPinBaseKey}_$mockUserId",
mockEncryptedPin,
)
}
val actual = authDiskSource.getEncryptedPin(userId = mockUserId)
assertEquals(
mockEncryptedPin,
@ -411,18 +412,18 @@ class AuthDiskSourceTest {
"organizationId2" to "organizationKey2",
)
fakeSharedPreferences
.edit()
.putString(
"${organizationKeysBaseKey}_$mockUserId",
"""
{
"organizationId1": "organizationKey1",
"organizationId2": "organizationKey2"
}
"""
.trimIndent(),
)
.apply()
.edit {
putString(
"${organizationKeysBaseKey}_$mockUserId",
"""
{
"organizationId1": "organizationKey1",
"organizationId2": "organizationKey2"
}
"""
.trimIndent(),
)
}
val actual = authDiskSource.getOrganizationKeys(userId = mockUserId)
assertEquals(
mockOrganizationKeys,
@ -470,12 +471,12 @@ class AuthDiskSourceTest {
)
val mockOrganizationsMap = mockOrganizations.associateBy { it.id }
fakeSharedPreferences
.edit()
.putString(
"${organizationsBaseKey}_$mockUserId",
json.encodeToString(mockOrganizationsMap),
)
.apply()
.edit {
putString(
"${organizationsBaseKey}_$mockUserId",
json.encodeToString(mockOrganizationsMap),
)
}
val actual = authDiskSource.getOrganizations(userId = mockUserId)
assertEquals(
mockOrganizations,

View file

@ -1,5 +1,6 @@
package com.x8bit.bitwarden.data.platform.datasource.disk
import androidx.core.content.edit
import app.cash.turbine.test
import com.x8bit.bitwarden.data.auth.datasource.disk.model.EnvironmentUrlDataJson
import com.x8bit.bitwarden.data.platform.base.FakeSharedPreferences
@ -44,7 +45,7 @@ class EnvironmentDiskSourceTest {
)
// Update SharedPreferences updates the repository
fakeSharedPreferences.edit().putString(environmentKey, null).apply()
fakeSharedPreferences.edit { putString(environmentKey, null) }
assertNull(environmentDiskSource.preAuthEnvironmentUrlData)
}

View file

@ -34,7 +34,7 @@ class PushDiskSourceTest {
)
// Update SharedPreferences updates the repository
fakeSharedPreferences.edit().putString(registeredPushTokenKey, null).apply()
fakeSharedPreferences.edit { putString(registeredPushTokenKey, null) }
assertNull(pushDiskSource.registeredPushToken)
}
@ -62,12 +62,12 @@ class PushDiskSourceTest {
val mockUserId = "mockUserId"
val mockCurrentPushToken = "abcd"
fakeSharedPreferences
.edit()
.putString(
"${currentPushTokenBaseKey}_$mockUserId",
mockCurrentPushToken,
)
.apply()
.edit {
putString(
"${currentPushTokenBaseKey}_$mockUserId",
mockCurrentPushToken,
)
}
val actual = pushDiskSource.getCurrentPushToken(userId = mockUserId)
assertEquals(
mockCurrentPushToken,
@ -101,12 +101,12 @@ class PushDiskSourceTest {
val mockUserId = "mockUserId"
val mockLastPushTokenRegistration = ZonedDateTime.parse("2024-01-06T22:27:45.904314Z")
fakeSharedPreferences
.edit()
.putLong(
"${lastPushTokenBaseKey}_$mockUserId",
getBinaryLongFromZoneDateTime(mockLastPushTokenRegistration),
)
.apply()
.edit {
putLong(
"${lastPushTokenBaseKey}_$mockUserId",
getBinaryLongFromZoneDateTime(mockLastPushTokenRegistration),
)
}
val actual = pushDiskSource.getLastPushTokenRegistrationDate(userId = mockUserId)!!
assertEquals(
mockLastPushTokenRegistration,

View file

@ -30,12 +30,12 @@ class SettingsDiskSourceTest {
// Updating the shared preferences should update disk source.
fakeSharedPreferences
.edit()
.putString(
appLanguageKey,
expected.localeName,
)
.apply()
.edit {
putString(
appLanguageKey,
expected.localeName,
)
}
val actual = settingsDiskSource.appLanguage
assertEquals(
expected,
@ -86,12 +86,12 @@ class SettingsDiskSourceTest {
// Updating the shared preferences should update disk source.
fakeSharedPreferences
.edit()
.putBoolean(
isIconLoadingDisabled,
expected,
)
.apply()
.edit {
putBoolean(
isIconLoadingDisabled,
expected,
)
}
assertEquals(
expected,
settingsDiskSource.isIconLoadingDisabled,
@ -112,12 +112,12 @@ class SettingsDiskSourceTest {
val mockUserId = "mockUserId"
val vaultTimeoutInMinutes = 360
fakeSharedPreferences
.edit()
.putInt(
"${vaultTimeoutBaseKey}_$mockUserId",
vaultTimeoutInMinutes,
)
.apply()
.edit {
putInt(
"${vaultTimeoutBaseKey}_$mockUserId",
vaultTimeoutInMinutes,
)
}
val actual = settingsDiskSource.getVaultTimeoutInMinutes(userId = mockUserId)
assertEquals(
vaultTimeoutInMinutes,
@ -192,12 +192,12 @@ class SettingsDiskSourceTest {
val mockUserId = "mockUserId"
val vaultTimeoutAction = VaultTimeoutAction.LOCK
fakeSharedPreferences
.edit()
.putString(
"${vaultTimeoutActionBaseKey}_$mockUserId",
"0",
)
.apply()
.edit {
putString(
"${vaultTimeoutActionBaseKey}_$mockUserId",
"0",
)
}
val actual = settingsDiskSource.getVaultTimeoutAction(userId = mockUserId)
assertEquals(
vaultTimeoutAction,
@ -269,9 +269,9 @@ class SettingsDiskSourceTest {
val mockUserId = "mockUserId"
val pullToRefreshKey = "${pullToRefreshBaseKey}_$mockUserId"
fakeSharedPreferences
.edit()
.putBoolean(pullToRefreshKey, true)
.apply()
.edit {
putBoolean(pullToRefreshKey, true)
}
assertEquals(true, settingsDiskSource.getPullToRefreshEnabled(userId = mockUserId))
}

View file

@ -1,5 +1,6 @@
package com.x8bit.bitwarden.data.tools.generator.datasource.disk
import androidx.core.content.edit
import com.x8bit.bitwarden.data.platform.base.FakeSharedPreferences
import com.x8bit.bitwarden.data.tools.generator.repository.model.PasscodeGenerationOptions
import com.x8bit.bitwarden.data.tools.generator.repository.model.UsernameGenerationOptions
@ -99,7 +100,7 @@ class GeneratorDiskSourceTest {
)
val key = "bwPreferencesStorage_passwordGenerationOptions_$userId"
fakeSharedPreferences.edit().putString(key, json.encodeToString(options)).apply()
fakeSharedPreferences.edit { putString(key, json.encodeToString(options)) }
val result = generatorDiskSource.getPasscodeGenerationOptions(userId)
@ -166,7 +167,7 @@ class GeneratorDiskSourceTest {
)
val key = "bwPreferencesStorage_usernameGenerationOptions_$userId"
fakeSharedPreferences.edit().putString(key, json.encodeToString(options)).apply()
fakeSharedPreferences.edit { putString(key, json.encodeToString(options)) }
val result = generatorDiskSource.getUsernameGenerationOptions(userId)