Skip to content

Instantly share code, notes, and snippets.

@ArnyminerZ
Last active February 3, 2023 06:10
Show Gist options
  • Save ArnyminerZ/580ca9e9c404a172e583b922d453ca78 to your computer and use it in GitHub Desktop.
Save ArnyminerZ/580ca9e9c404a172e583b922d453ca78 to your computer and use it in GitHub Desktop.
Kotlin extension functions for allowing Parcelable write and read from Android's shared preferences.
// Being Location a Parcelable class, and location a valid initialized instance of this class:
// For writing
sharedPreferences.edit {
putParcelable("location", location)
}
// For reading
val loadedLocation = sharedPreferences.getParcelable<Location?>("location", null)
// FOR LISTS
// Writing
sharedPreferences.edit {
putParcelableList("location", locations)
}
// Reading
val loadedLocations = sharedPreferences.getParcelableList<Location>("location", listOf())
import android.content.SharedPreferences
import android.os.Parcelable
import com.google.gson.Gson
import com.google.gson.JsonSyntaxException
/**
* Set a [Parcelable] value in the preferences editor, to be written back once
* [SharedPreferences.Editor.commit] or [SharedPreferences.Editor.apply] are called.
* @author Arnau Mora
* @param key The name of the preference to modify.
* @param parcelable The new value for the preference. Passing null for this argument is equivalent
* to calling [SharedPreferences.Editor.remove] with this key.
*/
fun SharedPreferences.Editor.putParcelable(key: String, parcelable: Parcelable?) {
val json = Gson().toJson(parcelable)
putString(key, json)
}
/**
* Retrieve a [T] value from the preferences.
* @author Arnau Mora
* @param key The name of the preference to retrieve.
* @param default The value to return if the preference doesn't exist, or is not valid.
* @return Returns the preference value if it exists, or [default] if not.
*/
inline fun <reified T : Parcelable?> SharedPreferences.getParcelable(key: String, default: T): T {
val json = getString(key, null)
return try {
if (json != null)
Gson().fromJson(json, T::class.java)
else default
} catch (_: JsonSyntaxException) {
default
}
}
/**
* Set a [Parcelable] [Collection] value in the preferences editor, to be written back once
* [SharedPreferences.Editor.commit] or [SharedPreferences.Editor.apply] are called.
* @author Arnau Mora
* @param key The name of the preference to modify.
* @param collection The new value for the preference. Passing null for this argument is equivalent
* to calling [SharedPreferences.Editor.remove] with this key.
*/
fun SharedPreferences.Editor.putParcelableList(key: String, collection: Collection<Parcelable>) {
val json = Gson().toJson(collection)
putString(key, json)
}
/**
* Retrieve a [T] [Collection] value from the preferences.
* @author Arnau Mora
* @param key The name of the preference to retrieve.
* @param default The value to return if the preference doesn't exist, or is not valid.
* @return Returns the preference value if it exists, or [default] if not.
*/
inline fun <reified T : Parcelable?> SharedPreferences.getParcelableList(
key: String,
default: List<T>
): List<T> {
val json = getString(key, null)
return try {
if (json != null) {
val result = arrayListOf<T>()
for (e in Gson().fromJson(json, Collection::class.java))
if (e is T)
result.add(e)
result
} else default
} catch (_: JsonSyntaxException) {
default
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment