mingkangpan
12/13/2018, 10:21 AM@Parcelize
data class LikeAdStatesHolder(val askLogin : LikeAdState.AskLogin? = null,
val loadList : LikeAdState.LoadingList? = null,
val askWhichList: LikeAdState.AskWhichList? = null,
val liking : LikeAdState.Liking? = null) : Parcelable {
enum class EntryPoint{
ONE, TWO, THREE, FOUR;
}
fun entryPoint() : EntryPoint {
return if(askWhichList != null && liking != null) {
EntryPoint.FOUR
} else if(askWhichList != null) {
EntryPoint.THREE
} else if(loadList != null) {
EntryPoint.TWO
} else if(askLogin != null) {
EntryPoint.ONE
} else {
throw IllegalStateException("This RestoreHolder is not valid. ${toString()}")
}
}
}
I would like to define a contract for the method entryPoint()
that if the method returns a value, the compiler should know that the associated values are not null, so I dont have to use the double explanation marks here:
when (holder.entryPoint()) {
LikeAdStatesHolder.EntryPoint.ONE -> askLogin()
LikeAdStatesHolder.EntryPoint.TWO -> loadList()
LikeAdStatesHolder.EntryPoint.THREE -> askWhichList(holder.askWhichList!!.list)
LikeAdStatesHolder.EntryPoint.FOUR -> likeList(holder.liking!!.index, holder.askWhichList!!.list)
}