🧩Fragment

Example of an MVI Fragment implemented with Yamvil.

class DashboardFragment: MVIFragment<DashboardUiState, DashboardUiEvent>() {

    override val viewModel: DashboardViewModel by viewModels()
    override fun observeUiState(uiState: DashboardUiState) {
        when(uiState.state) {
            DashboardUiState.ContentState.Loading -> onLoading()
            is DashboardUiState.ContentState.Content -> onContent(uiState.state)
            DashboardUiState.ContentState.Error -> onError()
        }

        uiState.onAction { action ->
            when(action) {
                DashboardUiAction.NavigateToNext -> navigateToNext()
            }
        }
    }

    private fun navigateToNext() {
        // Do something with navigateToNext
    }

    private fun onLoading() {
        // Do something with loading
    }

    private fun onContent(content: DashboardUiState.ContentState.Content) {
        // Do something with content
    }

    private fun onError() {
        // Do something with error
    }
}

Note that as DashboardUiState implements Actionable , we need to use the onAction function to consume only once the UiAction:

uiState.onAction { action ->
    when(action) {
        DashboardUiAction.NavigateToNext -> navigateToNext()
    }
}

Last updated