[PM-19845] Migrate BaseDiskSource to data module (#4978)

This commit is contained in:
Patrick Honkonen
2025-04-03 15:34:52 -04:00
committed by GitHub
parent ce0aa7adda
commit 5017e935d7
19 changed files with 59 additions and 175 deletions

View File

@@ -214,6 +214,7 @@ dependencies {
implementation(files("libs/authenticatorbridge-1.0.0-release.aar"))
implementation(project(":core"))
implementation(project(":data"))
implementation(project(":network"))
implementation(libs.androidx.activity.compose)

View File

@@ -1,120 +0,0 @@
package com.x8bit.bitwarden.data.platform.datasource.disk
import android.content.SharedPreferences
import androidx.core.content.edit
/**
* Base class for simplifying interactions with [SharedPreferences].
*/
@Suppress("UnnecessaryAbstractClass")
abstract class BaseDiskSource(
private val sharedPreferences: SharedPreferences,
) {
/**
* Gets the [Boolean] for the given [key] from [SharedPreferences], or returns `null` if that
* key is not present.
*/
protected fun getBoolean(key: String): Boolean? =
if (sharedPreferences.contains(key.withBase())) {
sharedPreferences.getBoolean(key.withBase(), false)
} else {
// Make sure we can return a null value as a default if necessary
null
}
/**
* Puts the [value] in [SharedPreferences] for the given [key] (or removes the key when the
* value is `null`).
*/
protected fun putBoolean(
key: String,
value: Boolean?,
): Unit =
sharedPreferences.edit {
if (value != null) {
putBoolean(key.withBase(), value)
} else {
remove(key.withBase())
}
}
/**
* Gets the [Int] for the given [key] from [SharedPreferences], or returns `null` if that key
* is not present.
*/
protected fun getInt(key: String): Int? =
if (sharedPreferences.contains(key.withBase())) {
sharedPreferences.getInt(key.withBase(), 0)
} else {
// Make sure we can return a null value as a default if necessary
null
}
/**
* Puts the [value] in [SharedPreferences] for the given [key] (or removes the key when the
* value is `null`).
*/
protected fun putInt(
key: String,
value: Int?,
): Unit =
sharedPreferences.edit {
if (value != null) {
putInt(key.withBase(), value)
} else {
remove(key.withBase())
}
}
/**
* Gets the [Long] for the given [key] from [SharedPreferences], or returns `null` if that key
* is not present.
*/
protected fun getLong(key: String): Long? =
if (sharedPreferences.contains(key.withBase())) {
sharedPreferences.getLong(key.withBase(), 0)
} else {
// Make sure we can return a null value as a default if necessary
null
}
/**
* Puts the [value] in [SharedPreferences] for the given [key] (or removes the key when the
* value is `null`).
*/
protected fun putLong(
key: String,
value: Long?,
): Unit =
sharedPreferences.edit {
if (value != null) {
putLong(key.withBase(), value)
} else {
remove(key.withBase())
}
}
protected fun getString(
key: String,
): String? = sharedPreferences.getString(key.withBase(), null)
protected fun putString(
key: String,
value: String?,
): Unit = sharedPreferences.edit { putString(key.withBase(), value) }
protected fun removeWithPrefix(prefix: String) {
sharedPreferences
.all
.keys
.filter { it.startsWith(prefix.withBase()) }
.forEach { sharedPreferences.edit { remove(it) } }
}
protected fun String.appendIdentifier(identifier: String): String = "${this}_$identifier"
}
/**
* Helper method for prepending the key with the appropriate base storage key.
*/
private fun String.withBase(): String = "bwPreferencesStorage:$this"

View File

@@ -3,6 +3,7 @@ package com.x8bit.bitwarden.data.platform.datasource.disk
import android.content.SharedPreferences
import androidx.core.content.edit
import androidx.security.crypto.EncryptedSharedPreferences
import com.bitwarden.data.datasource.disk.BaseDiskSource
/**
* Base class for simplifying interactions with [SharedPreferences] and

View File

@@ -3,6 +3,7 @@ package com.x8bit.bitwarden.data.platform.datasource.disk
import android.content.SharedPreferences
import com.bitwarden.core.data.repository.util.bufferedMutableSharedFlow
import com.bitwarden.core.data.util.decodeFromStringOrNull
import com.bitwarden.data.datasource.disk.BaseDiskSource
import com.x8bit.bitwarden.data.platform.datasource.disk.model.ServerConfig
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.onSubscription

View File

@@ -3,6 +3,7 @@ package com.x8bit.bitwarden.data.platform.datasource.disk
import android.content.SharedPreferences
import com.bitwarden.core.data.repository.util.bufferedMutableSharedFlow
import com.bitwarden.core.data.util.decodeFromStringOrNull
import com.bitwarden.data.datasource.disk.BaseDiskSource
import com.x8bit.bitwarden.data.auth.datasource.disk.model.EnvironmentUrlDataJson
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.onSubscription

View File

@@ -1,6 +1,7 @@
package com.x8bit.bitwarden.data.platform.datasource.disk
import android.content.SharedPreferences
import com.bitwarden.data.datasource.disk.BaseDiskSource
import com.x8bit.bitwarden.data.platform.manager.model.FlagKey
/**

View File

@@ -1,6 +1,7 @@
package com.x8bit.bitwarden.data.platform.datasource.disk
import android.content.SharedPreferences
import com.bitwarden.data.datasource.disk.BaseDiskSource
import com.x8bit.bitwarden.data.platform.util.getBinaryLongFromZoneDateTime
import com.x8bit.bitwarden.data.platform.util.getZoneDateTimeFromBinaryLong
import java.time.ZonedDateTime

View File

@@ -3,6 +3,7 @@ package com.x8bit.bitwarden.data.platform.datasource.disk
import android.content.SharedPreferences
import com.bitwarden.core.data.repository.util.bufferedMutableSharedFlow
import com.bitwarden.core.data.util.decodeFromStringOrNull
import com.bitwarden.data.datasource.disk.BaseDiskSource
import com.x8bit.bitwarden.data.platform.manager.model.AppResumeScreenData
import com.x8bit.bitwarden.data.platform.repository.model.UriMatchType
import com.x8bit.bitwarden.data.platform.repository.model.VaultTimeoutAction

View File

@@ -2,7 +2,7 @@ package com.x8bit.bitwarden.data.tools.generator.datasource.disk
import android.content.SharedPreferences
import com.bitwarden.core.data.util.decodeFromStringOrNull
import com.x8bit.bitwarden.data.platform.datasource.disk.BaseDiskSource
import com.bitwarden.data.datasource.disk.BaseDiskSource
import com.x8bit.bitwarden.data.tools.generator.repository.model.PasscodeGenerationOptions
import com.x8bit.bitwarden.data.tools.generator.repository.model.UsernameGenerationOptions
import kotlinx.serialization.json.Json