viewModel.sectionTypeId, specialSectionId, specialSectionId -> These values are commonly used both cases
if (data.section_type == "customized") {
viewModel.sectionTypeId = data.id ?: 0
viewModel.specialSectionId = data.id ?: 0
viewModel.specialSectionName = data.name ?: ""
} else {
viewModel.specialSectionId = 0
viewModel.specialSectionName = ""
viewModel.sectionTypeId = 0
}
CodePudding user response:
You could do this:
viewModel.apply {
val customized = data.section_type == "customized"
sectionTypeId = if(customized) data.id ?: 0 else 0
specialSectionId = if(customized) data.id ?: 0 else 0
specialSectionName = if(customized) data.name ?: "" else ""
}
CodePudding user response:
Since there is no ternary operator in kotlin, you can use if expressions and assign repeated code to values like this:
viewModel.apply {
val customized = data.section_type == "customized"
val iD = if(customized) data.id ?: 0 else 0
sectionTypeId = iD
specialSectionId = iD
specialSectionName = if(customized) data.name ?: "" else ""
}
CodePudding user response:
You can try this..
viewModel.apply{
val customized = data.section_type == "customized"
sectionTypeId = customized ? (data.id ?: 0) : 0
specialSectionId = customized ? (data.id ?: 0) : 0
specialSectionName = customized ? (data.id ?: "") : ""
}
