Home > database >  Accessing the nested level of sealed class in android kotlin
Accessing the nested level of sealed class in android kotlin

Time:11-24

I have a sealed class

MyEvents.kt

sealed class MyEvents<out T : Any>{
    sealed class HelloBroadcasting<out T : Any> : MyEvents<Nothing>() {
        sealed class FeatureSegments : HelloBroadcasting<Nothing>() {
            class SegmentationCourseSeekChapterChange(val context: Context) : FeatureSegments()
            class SegmentationChapterNameClicked(val context: Context, chapterName: String) : FeatureSegments()
            class SegmentationChapterSelectionFromChapterList(val context: Context) : FeatureSegments()
        }
    }
}

I call the sealed class

sendTheEventEvent(MyEvents.HelloBroadcasting.FeatureSegments.SegmentationChapterNameClicked(mContext,it.textDirection.toString()))

I am trying to get the event received as

fun sendCleverTapEvent(event: MyEvents<Int>) {
        when(event){
            is MyEvents.HelloBroadcasting.FeatureSegments.SegmentationChapterNameClicked -> test(event.context,event.) // ---> Here I am not able to access the name
            is MyEvents.HelloBroadcasting.FeatureSegments.SegmentationChapterSelectionFromChapterList -> TODO()
            is MyEvents.HelloBroadcasting.FeatureSegments.SegmentationCourseSeekChapterChange -> TODO()
        }
    }

I am not able to access the name in the receiving part. How to do this properly when the nested level is there?

CodePudding user response:

You have to make chapterName a property:

- chapterName: String
  val chapterName: String
  • Related