miqbaldc
07/31/2021, 4:23 PMwhen
for the sealed class
with LiveData.observeAsState
like this code in 🧵?miqbaldc
07/31/2021, 4:28 PM// we implement this on VM, e.g: class CurrentOccupationViewModel : CurrentOccupationState
interface CurrentOccupationState {
val hasNoWorkExperience: LiveData<NoWorkExperienceState> // we're still not using <Flow> in this phase of compose migration
fun hasNoWorkExperience(hasNoWorkExperience: NoWorkExperienceState)
}
// CurrentOccupationScreen.kt
sealed class NoWorkExperienceState {
data class NullExperience(val message: String) :
NoWorkExperienceState()
object WithExperience : NoWorkExperienceState()
}
@Composable
fun CurrentOccupationScreen(state: CurrentOccupationScreen) {
val hasNoWorkExperience by state.hasNoWorkExperience.observeAsState()
when(hasNoWorkExperience){
is NoWorkExperienceState.NullExperience -> {
val nullExperience = hasNoWorkExperience as NoWorkExperienceState.NullExperience
println(nullExperience.message)
// Smart cast to 'NoWorkExperienceState.NullExperience' is impossible, because 'hasNoWorkExperience' is a property that has open or custom getter
println(hasNoWorkExperience?.message)
// Showing toast for now
}
NoWorkExperienceState.WithExperience -> {}
}
}
miqbaldc
07/31/2021, 4:38 PMas
, without smart cast (because it’s intended/impossible as of now?)
The above code showing below error:
Smart cast to 'NoWorkExperienceState.NullExperience' is impossible, because 'hasNoWorkExperience' is a property that has open or custom getter
carbaj0
07/31/2021, 4:45 PMwhen(val temp = hasNoWorkExperience){
is NoWorkExperienceState.NullExperience -> {
println(temp.message)
}
}
miqbaldc
07/31/2021, 6:28 PMinline
the code, which back to the above issues.lhwdev
08/01/2021, 3:56 AMmiqbaldc
08/02/2021, 6:12 AM