Solution 1 :
Not quite sure if this is the right answer to your question, but I think that a sealed class may help you.
Basically you could define a MutableUser
(User or string) and each subclass of MutableUser should implement Parcelable
.
sealed class MutableUser : Parcelable {
@Parcelize
class UserAttribute(val user: User) : MutableUser()
@Parcelize
class SimpleAttribute(val userId: String) : MutableUser()
}
Then, you could use this MutableUser
in your car data model like below:
@Parcelize
data class Car(
val createdAt: String? = "",
val updatedAt: String? = "",
val id: String? = "",
val name: String? = "",
val user: MutableUser
) : Parcelable
Problem :
I have the following data class that implements Parcelable
:
data class Car(
val createdAt: String? = "",
val updatedAt: String? = "",
val id: String? = "",
val name: String? = "",
val user: User? = User()
)
In this data class, sometimes the user
attributes can be the id of the user (String) and sometimes it could be the User object itself.
Is there any way to implement Parcelable and indicates that the value can be either a String or an Object ? (Another data model)
Comments
Comment posted by Blackbelt
User should be parcelable as well. When you unparcel Car you might have both or just one of the two information.