mikelantzelo
mikelantzelo

Reputation: 331

How to pass function with parameter jetpack compose

i have a selaed class:

sealed class LessonEvent {

object OnEditBottomNavigationClick: LessonEvent()
object OnDeleteBottomNavigationClick: LessonEvent()
data class OnLessonClick(val lesson: Lesson): LessonEvent()

}

and another one

sealed class SchoolEvent {
data class OnSchoolClick(val school: School): SchoolEvent()

}

and want to pass this event to composable as parameter:

@Composable
fun TestItem(
    onEvent: (parameter) -> Unit
) 

so that onEvent can take SchoolEvent or LessonEvent as parameter and so i can access each object or data class How can this be done in jetpack compose?

Upvotes: 1

Views: 1073

Answers (1)

bylazy
bylazy

Reputation: 1295

This question is not related to Jetpack Compose. If you want to be able to pass both LessonEvent and SchoolEvent to the function, you need both of them to inherit the same interface, like:

sealed interface SomeEvent

sealed class LessonEvent: SomeEvent {
    object OnEditBottomNavigationClick: LessonEvent()
    object OnDeleteBottomNavigationClick: LessonEvent()
    data class OnLessonClick(val lesson: Lesson): LessonEvent()
}

sealed class SchoolEvent: SomeEvent {
    data class OnSchoolClick(val school: School): SchoolEvent()
}

And then:

@Composable
fun TestItem(event: SomeEvent,
    onEvent: (SomeEvent) -> Unit
)

Upvotes: 2

Related Questions