3

I have a sealed interface for my UIState.

sealed interface UIState<out T> {
    object ShowLoading : UIState<Nothing>
    object ShowEmptyData : UIState<Nothing>
    data class ShowData<out T>(val data: T) : UIState<T>
    data class ShowError(val errorUIState: ErrorUIState) : UIState<Nothing>
}

This is general for most of the screens in my application, but let's take I have a search screen and along with this states I need one more state

object ShowNoSearchResult: UIState<Nothing>

If I add this state inside UIState sealed interface, in all the screens I need to handle this state, but I need it only in the search screen. As a solution, I tried to create a new sealed interface and extend the UIState.

sealed interface SearchUIState<out T>: UIState<T> {
    object ShowNoSearchResult: SearchUIState<Nothing>
}

Now the issue is that I can't call any UIState class using my SearchUIState. The ShowLoading, ShowEmptyData, etc. are not available if I use SearchUIState. What I want is to use SearchUIState to handle all the states of the SearchUIState and its parent states as well? Is there a way to do this without code duplication?

P.S. Actually using the parent sealed interface I can return the child sealed interface state as well. The thing is that still when I handle parent sealed interface states using when, it requires to handle also all the child sealed interface states.

Hayk Mkrtchyan
  • 2,835
  • 3
  • 19
  • 61

1 Answers1

1

You can make each of the UIState children also implement SearchUIState, and you can change SearchUIState to not be a subtype of UIState.

sealed interface UIState<out T> {
    object ShowLoading : UIState<Nothing>, SearchUIState<Nothing>
    object ShowEmptyData : UIState<Nothing>, SearchUIState<Nothing>
    data class ShowData<out T>(val data: T) : UIState<T>, SearchUIState<T>
    data class ShowError(val errorUIState: ErrorUIState) : UIState<Nothing>, SearchUIState<Nothing>
}

sealed interface SearchUIState<out T> {
    object ShowNoSearchResult: SearchUIState<Nothing>
}
Tenfour04
  • 83,111
  • 11
  • 94
  • 154
  • What if there are more than one UIState "child" interfaces ? Not only SearchUIState but also, say, GetUsersListUIState, GetUserActivityUiState, UpdateUserNameUiState, etc. (let's say there are several dozen of these). Making all UIState children implement all possible "child" interfaces would be extremely impractical – Piotr Śmietana Dec 20 '22 at 16:38
  • I have an idea for that. If you post a question I can expand. Basically, give a second generic type for an Other category. Each different type of screen can define what that extra value might be if they need it. – Tenfour04 Dec 20 '22 at 17:01
  • I've posted a question: https://stackoverflow.com/questions/74877498/kotlin-make-multiple-sealed-classes-have-common-set-of-base-subclasses-but – Piotr Śmietana Dec 21 '22 at 14:27
  • @PiotrŚmietana, sorry, I had been away from an actual keyboard for more than a week. I just posted an answer with my idea. – Tenfour04 Dec 30 '22 at 02:25