SharedPreferencesMigrationKt

Added in 1.0.0

public final class SharedPreferencesMigrationKt


Summary

Public methods

static final @NonNull SharedPreferencesMigration<@NonNull Preferences>
SharedPreferencesMigration(
    @NonNull Function0<@NonNull SharedPreferences> produceSharedPreferences,
    @NonNull Set<@NonNull String> keysToMigrate
)

Creates a SharedPreferencesMigration for DataStore.

static final @NonNull SharedPreferencesMigration<@NonNull Preferences>
SharedPreferencesMigration(
    @NonNull Context context,
    @NonNull String sharedPreferencesName,
    @NonNull Set<@NonNull String> keysToMigrate
)

Creates a SharedPreferencesMigration for DataStore.

Public methods

SharedPreferencesMigration

public static final @NonNull SharedPreferencesMigration<@NonNull PreferencesSharedPreferencesMigration(
    @NonNull Function0<@NonNull SharedPreferences> produceSharedPreferences,
    @NonNull Set<@NonNull String> keysToMigrate
)

Creates a SharedPreferencesMigration for DataStore.

Note: This migration only supports the basic SharedPreferences types: boolean, float, int, long, string and string set. If the result of getAll contains other types, they will be ignored.

Parameters
@NonNull Function0<@NonNull SharedPreferences> produceSharedPreferences

Should return the instance of SharedPreferences to migrate from.

@NonNull Set<@NonNull String> keysToMigrate

The list of keys to migrate. The keys will be mapped to datastore.Preferences with their same values. If the key is already present in the new Preferences, the key will not be migrated again. If the key is not present in the SharedPreferences it will not be migrated. If keysToMigrate is not set, all keys will be migrated from the existing SharedPreferences.

SharedPreferencesMigration

public static final @NonNull SharedPreferencesMigration<@NonNull PreferencesSharedPreferencesMigration(
    @NonNull Context context,
    @NonNull String sharedPreferencesName,
    @NonNull Set<@NonNull String> keysToMigrate
)

Creates a SharedPreferencesMigration for DataStore.

If the SharedPreferences is empty once the migration completes, this migration will attempt to delete it.

Parameters
@NonNull Context context

Context used for getting SharedPreferences.

@NonNull String sharedPreferencesName

The name of the SharedPreferences.

@NonNull Set<@NonNull String> keysToMigrate

The list of keys to migrate. The keys will be mapped to datastore.Preferences with their same values. If the key is already present in the new Preferences, the key will not be migrated again. If the key is not present in the SharedPreferences it will not be migrated. If keysToMigrate is not set, all keys will be migrated from the existing SharedPreferences.