I have the following data class, which shows a list of movies in a recyclerView, when the user clicks on a movie it goes to the detail which have a favorite button, and when it clicks on the favorite button it gets saved and shown in a Favorites fragment tab, I want this list to persist app launches. Using Kotlin.
data class MovieItem(
@SerializedName("id")
val id: Int,
@SerializedName("image")
val image: Image,
@SerializedName("name")
val name: String,
@SerializedName("summary")
val summary: String
):Serializable
I saw somewhere else to save it as gson object, but I'm not sure how to retrieve it to show it in the favorites fragment:
in the detailViewModel I converted it to json:
fun convertToJson(movie: MovieItem) {
val json = Gson().toJson(movie)
}
in the favoritesViewModel:
fun getFavorites(json:String) {
val json = Gson().fromJson(json, MovieItem.class)
}