0

I am having some problems with implementing Parcelable in a class in my app. This is written in Kotlin. I have a class called CocktailRecipe that has what you'd expect - name, description etc. It also has a two lists and a map. Ingredient and Equipment are also custom classes. I cannot figure out how to parcelise these - all the information I have found relates to Java, such as using parcelreadValue(this.getClass().getClassLoader()) but I cannot find the Kotlin equivalent of this. I also get a bunch of TODO comments generated in the parcel's constructor and I haven't been able to find any information on what should go in their place. Here is the beginning of the class - can anyone point me in the right direction? TIA.

class CocktailRecipe(
        recipeName: String?,
        recipeImage: Int,
        recipeBlurb: String?,
        recipeDesc: String?,
        recipEquipment: MutableList<Equipment>,
        recipeIngredients: MutableMap<Ingredient, Int>,
        recipeInstructions: MutableList<String>
)  {
    var name = recipeName
    var image = recipeImage
    var blurb = recipeBlurb
    var description = recipeDesc
    var equipment = recipEquipment
    var ingredients = recipeIngredients
    var instructions = recipeInstructions
    var isFavourite = false
    var rating = 0

    // methods etc.

}
Donagh
  • 89
  • 2
  • 10
  • As an aside, should Ingredient and Equipment be parcelable in order to be parceled as part of this class? – Donagh Mar 29 '21 at 12:17
  • I suggest declaring your fields in constructor and using parcelize plugin https://developer.android.com/kotlin/parcelize – Pawel Mar 29 '21 at 12:22
  • @Pawel thanks, I'll try this. What do you suggest for isFavourite and rating? These are initialised to default values, so they aren't passed into the constructor, but still need to be parceled. – Donagh Mar 29 '21 at 12:26
  • If you need them parceled they have to be a part of primary constructor. You can declare default values there. If you want to "hide" them from constructor you can make primary constructor protected and create public secondary one that won't modify those values. – Pawel Mar 29 '21 at 12:32

2 Answers2

0

Try this

@Parcelize
data class CocktailRecipe(
        recipeName: String?,
        recipeImage: Int,
        recipeBlurb: String?,
        recipeDesc: String?,
        recipEquipment: MutableList<Equipment>,
        recipeIngredients: MutableMap<Ingredient, Int>,
        recipeInstructions: MutableList<String>
)
0

You can use kotlin-parcelize - It's a plugin that provides a Parcelable implementation generator

First add kotlin-parcelize to your module

plugins {
    ..
    id 'kotlin-parcelize'
}

Now you can use this code that works in your case

@Parcelize
class CocktailRecipe(
    recipeName: String?,
    recipeImage: Int,
    recipeBlurb: String?,
    recipeDesc: String?,
    recipEquipment: MutableList<Equipment>,
    recipeIngredients: MutableMap<Ingredient, Int>,
    recipeInstructions: MutableList<String>
): Parcelable  {
    var name = recipeName
    var image = recipeImage
    var blurb = recipeBlurb
    var description = recipeDesc
    var equipment = recipEquipment
    var ingredients = recipeIngredients
    var instructions = recipeInstructions
    var isFavourite = false
    var rating = 0

    // methods etc.

}

@Parcelize
class Equipment(
    var1: String?,
    var2: String?
    
) : Parcelable

@Parcelize
class Ingredient(
    var1: String?,
    var2: String?
): Parcelable
Hayssam Soussi
  • 903
  • 7
  • 16