StorageDomain.DataStorePreference


class StorageDomain.DataStorePreference : StorageDomain


Targets a preference of an on-device Preferences DataStore.

The DataStore file is datastore/<dataStoreName>.preferences_pb in the files directory of the app, where preferencesDataStore(dataStoreName) keeps it. With isDeviceProtectedStorage, it's that path in the files directory of device-protected storage, which deviceProtectedDataStoreFile("<dataStoreName>.preferences_pb") returns. preferencesDataStoreFile always returns the first path, even for a device-protected context.

The Kotlin type of value sets the type of the preference key, such as 1.5f for floatPreferencesKey and 1.5 for doublePreferencesKey. Use the type of your app's key: with another type, the verification passes, but your app can't read the value.

Summary

Public constructors

DataStorePreference(
    dataStoreName: String,
    key: String,
    value: Any?,
    isDeviceProtectedStorage: Boolean
)

Public functions

open operator Boolean
equals(other: Any?)
open Int
open String

Public properties

String

The name of the Preferences DataStore, as the app passes it to preferencesDataStore.

Boolean

Whether the Preferences DataStore file is in device-protected storage, which is available before the user first unlocks the device, rather than in the default storage of the app.

String

The preference key to seed or verify.

Any?

The value associated with key, or null if asserting missing state.

Public constructors

DataStorePreference

Added in 1.0.0-alpha04
DataStorePreference(
    dataStoreName: String,
    key: String,
    value: Any? = null,
    isDeviceProtectedStorage: Boolean = false
)

Public functions

equals

open operator fun equals(other: Any?): Boolean

hashCode

open fun hashCode(): Int

toString

open fun toString(): String

Public properties

dataStoreName

Added in 1.0.0-alpha04
val dataStoreName: String

The name of the Preferences DataStore, as the app passes it to preferencesDataStore.

isDeviceProtectedStorage

Added in 1.0.0-alpha04
val isDeviceProtectedStorage: Boolean

Whether the Preferences DataStore file is in device-protected storage, which is available before the user first unlocks the device, rather than in the default storage of the app. That is credential-encrypted storage, unless the app sets android:defaultToDeviceProtectedStorage.

key

Added in 1.0.0-alpha04
val key: String

The preference key to seed or verify.

value

Added in 1.0.0-alpha04
val value: Any?

The value associated with key, or null if asserting missing state. Supported types are: String, Int, Long, Float, Double, Boolean, ByteArray, a Set of Strings, or null; other types fail construction with an IllegalArgumentException. The domain keeps a copy of a set or byte array, and returns a new copy of a byte array on each read. A null value is seeded by removing the key and verified by checking that the key is absent. Clearing app data also removes the key, so a null value cannot detect a restore that loses data.