MutablePreferences


public final class MutablePreferences extends Preferences


Mutable version of Preferences. Allows for creating Preferences with different key-value pairs.

Summary

Public methods

@NonNull Map<@NonNull Preferences.Key<@NonNull ?>, @NonNull Object>

Retrieve a map of all key preference pairs.

final void
boolean
<T extends Object> contains(@NonNull Preferences.Key<@NonNull T> key)

Returns true if this Preferences contains the specified key.

boolean
equals(Object other)
T
<T extends Object> get(@NonNull Preferences.Key<@NonNull T> key)

Get a preference with a key.

int
final void

Removes the preference with the given key from this MutablePreferences.

final void

Appends or replaces all pair to this MutablePreferences.

final void

Appends or replaces all pairs from prefs to this MutablePreferences.

final void

Appends or replaces all pairs to this MutablePreferences.

final @NonNull T
<T extends Object> remove(@NonNull Preferences.Key<@NonNull T> key)

Remove a preferences from this MutablePreferences.

final void
<T extends Object> set(@NonNull Preferences.Key<@NonNull T> key, @NonNull T value)

Set a key value pair in MutablePreferences.

@NonNull String

For better debugging.

Inherited methods

From androidx.datastore.preferences.core.Preferences
final @NonNull MutablePreferences

Gets a mutable copy of Preferences which contains all the preferences in this Preferences.

final @NonNull Preferences

Gets a read-only copy of Preferences which contains all the preferences in this Preferences.

Public methods

asMap

Added in 1.1.1
public @NonNull Map<@NonNull Preferences.Key<@NonNull ?>, @NonNull ObjectasMap()

Retrieve a map of all key preference pairs. The returned map is unmodifiable, and attempts to mutate it will throw runtime exceptions.

Returns
@NonNull Map<@NonNull Preferences.Key<@NonNull ?>, @NonNull Object>

a map containing all the preferences in this Preferences

clear

Added in 1.0.0
public final void clear()

contains

Added in 1.1.1
public boolean <T extends Object> contains(@NonNull Preferences.Key<@NonNull T> key)

Returns true if this Preferences contains the specified key.

Parameters
@NonNull Preferences.Key<@NonNull T> key

the key to check for

equals

public boolean equals(Object other)

get

Added in 1.1.1
public T <T extends Object> get(@NonNull Preferences.Key<@NonNull T> key)

Get a preference with a key. If the key is not set, returns null.

If T is Set, this returns an unmodifiable set which will throw a runtime exception when mutated. Do not try to mutate the returned set.

Use MutablePreferences.set to change the value of a preference (inside a DataStore.edit block).

Parameters
<T extends Object>

the type of the preference

@NonNull Preferences.Key<@NonNull T> key

the key for the preference

Throws
kotlin.ClassCastException

if there is something stored with the same name as key but it cannot be cast to T

hashCode

public int hashCode()

minusAssign

Added in 1.0.0
public final void minusAssign(@NonNull Preferences.Key<@NonNull ?> key)

Removes the preference with the given key from this MutablePreferences. If this Preferences does not contain the key, this is a no-op.

Example usage: mutablePrefs -= COUNTER_KEY

Parameters
@NonNull Preferences.Key<@NonNull ?> key

the key to remove from this MutablePreferences

plusAssign

Added in 1.0.0
public final void plusAssign(@NonNull Preferences.Pair<@NonNull ?> pair)

Appends or replaces all pair to this MutablePreferences.

Example usage: mutablePrefs += COUNTER_KEY to 100

Parameters
@NonNull Preferences.Pair<@NonNull ?> pair

the Preference.Pair to add to this MutablePreferences

plusAssign

Added in 1.0.0
public final void plusAssign(@NonNull Preferences prefs)

Appends or replaces all pairs from prefs to this MutablePreferences. Keys in prefs will overwrite keys in this Preferences.

Example usage: mutablePrefs += preferencesOf(COUNTER_KEY to 100, NAME to "abcdef")

Parameters
@NonNull Preferences prefs

Preferences to append to this MutablePreferences

putAll

Added in 1.0.0
public final void putAll(@NonNull Preferences.Pair<@NonNull ?> pairs)

Appends or replaces all pairs to this MutablePreferences.

Parameters
@NonNull Preferences.Pair<@NonNull ?> pairs

the pairs to append to this MutablePreferences

remove

Added in 1.0.0
public final @NonNull T <T extends Object> remove(@NonNull Preferences.Key<@NonNull T> key)

Remove a preferences from this MutablePreferences.

Parameters
@NonNull Preferences.Key<@NonNull T> key

the key to remove this MutablePreferences

Returns
@NonNull T

the original value of this preference key.

set

Added in 1.0.0
public final void <T extends Object> set(@NonNull Preferences.Key<@NonNull T> key, @NonNull T value)

Set a key value pair in MutablePreferences.

Example usage: val COUNTER_KEY = intPreferencesKey("counter")

// Once edit completes successfully, preferenceStore will contain the incremented counter. preferenceStore.edit { prefs: MutablePreferences -> prefs\[COUNTER_KEY\] = prefs\[COUNTER_KEY\] :? 0 + 1 }

Parameters
@NonNull Preferences.Key<@NonNull T> key

the preference to set

@NonNull Preferences.Key<@NonNull T> key

the value to set the preference to

toString

public @NonNull String toString()

For better debugging.