Hi Im getting the next trouble declaring my models on android
I have this api response
{
"isWeekTopic": false,
"_id": "62d739f65a4f2db1b81b5192",
"topicNumber": "1",
"title": "Números naturales",
"thumbnail": "https://www.example.com",
"relatedSkillsId": [
"62c12752d9de3e63d7cd0a78"
],
"relatedSubSkillsId": [
"62d73a8c5a4f2db1b81b5194"
],
"createdAt": "2018-01-01T00:00:00.000Z",
"updatedAt": "2018-01-01T00:00:00.000Z",
"testing": {
"mainColor": "#f2f2f2",
"secondColor": "#f2f2f2",
"image": "https://www.example.com"
},
"assetsStreak": {
"VIDEO": 2,
"STORY": 0,
"VIDEOGAME": 2,
"INFOGRAPHIC": 2,
"EXPLANATION": 0,
"STEPBYSTEP": 2
},
"skillPercent": 60
}
and this is my remote response model
data class TopicsItemRemoteResponseModel(
@field:SerializedName("_id")
val topicsId: String = "",
@field:SerializedName("title")
val title: String = "",
@field:SerializedName("skillNumber")
val skillNumber: Int = 0,
@field:SerializedName("skillPercent")
val skillPercent: Int = 0,
@field:SerializedName("thumbnail")
val thumbnail: String = "",
@field:SerializedName("createdAt")
val createdAt: String = "",
@field:SerializedName("updatedAt")
val updatedAt: String = "",
@field:SerializedName("isWeekTopic")
val isWeekTopic: Boolean = false,
@field:SerializedName("relatedSkillsId")
val relatedSkillsId: List<String> = listOf(),
@field:SerializedName("relatedSubSkillsId")
val relatedSubSkillsId: List<String> = listOf(),
) : Parcelable {
companion object {
fun mapListFromRemoteToDomainModel(
remoteList: List<TopicsItemRemoteResponseModel>): List<TopicsItemResponseModel> {
val topics = mutableListOf<TopicsItemResponseModel>()
remoteList.forEach { topicsRemoteResponseItem ->
topics.add(
mapFromRemoteToDomainModel(topicsRemoteResponseItem)
)
}
return topics
}
private fun mapFromRemoteToDomainModel(
topicsRemoteResponseItem: TopicsItemRemoteResponseModel
): TopicsItemResponseModel = with(topicsRemoteResponseItem) {
return TopicsItemResponseModel(
topicsId = topicsId,
skillNumber = skillNumber,
title = title,
skillPercent = skillPercent,
thumbnail = thumbnail,
createdAt = createdAt,
updatedAt = updatedAt,
isWeekTopic = isWeekTopic,
relatedSkillsId = relatedSkillsId,
relatedSubSkillsId = relatedSubSkillsId,
)
}
}
}
my poblem is that I dont know how to acces to assetsStreak info or testing info, I dont know how to declare them o what to do. I tied doing something like putting declaring it this way
@field:SerializedName("assetsStreak") val assetsStreak: assetsStreak,
but android studio tellsme its an error: unresolved reference: assetsStreak
im trying to learn kotlin, but there is no info about this case that i could find, please im desesperate thanks for you time
so after declared the class AssetStreak it doesnt mark error anymore but the problem goes to the response model wich i made it this way
@Parcelize
data class TopicsItemResponseModel(
val topicsId: String = "",
val title: String = "",
val skillNumber: Int = 0,
val thumbnail: String ="",
val createdAt: String = "",
val updatedAt: String = "",
val relatedSubSkillsId: List<String> = listOf(),
val relatedSkillsId: List<String> = listOf(),
val skillPercent: Int = 10,
val isWeekTopic: Boolean = false,
val assetsStreak: assetsStreak,
) : Parcelable {}
this way the error says unresolved reference asstsStreak and give me the option to create a class
if i put val assetsStreak: AssetsStreak, this way the error says unresolved reference AssetsStreakand give me the option to add dependency on another module should i do that?
CodePudding user response:
Have you defined a class named assetStreak? (notice it starts with a lowercase a)
The second line of this
@field:SerializedName("title")
val title: String = "",
seems to be saying, give me a value of something called title which will be a String object. So I'm guessing that if you define a class AssetsStreak (the A is capital by convention) then this will work
@field:SerializedName("assetsStreak")
val assetsStreak: AssetsStreak,
Psuedo code for AssetsStreak class
class AssetsStreak {
val video: String,
val story: String,
val videoGame: String,
...
},
CodePudding user response:
You can parce the response in the following way.
@Parcelize
data class TopicsItemRemoteModel(
val _id: String,
val assetsStreak: AssetsStreak,
val createdAt: String,
val isWeekTopic: Boolean,
val relatedSkillsId: List<String>,
val relatedSubSkillsId: List<String>,
val skillPercent: Int,
val testing: Testing,
val thumbnail: String,
val title: String,
val topicNumber: String,
val updatedAt: String
) : Parcelable
@Parcelize
data class AssetsStreak(
val EXPLANATION: Int,
val INFOGRAPHIC: Int,
val STEPBYSTEP: Int,
val STORY: Int,
val VIDEO: Int,
val VIDEOGAME: Int
) : Parcelable
@Parcelize
data class Testing(
val image: String,
val mainColor: String,
val secondColor: String
) : Parcelable