Hey I am working in Android Kotlin. I am learning ...
# android
v
Hey I am working in Android Kotlin. I am learning this LatestNewsUiState sealed class example from Android doc. I made my own 
sealed class
 example. But I am confused little bit, how can I achieved this. Is I am doing right for my scenario or not?
DataState.kt
Copy code
sealed class DataState {
    data class DataFetch(val data: List<Xyz>?) : DataState()
    object EmptyOnFetch : DataState()
    object ErrorOnFetch : DataState()
}
viewmodel.kt
Copy code
var dataMutableStateFlow = MutableStateFlow<DataState>(DataState.EmptyOnFetch)

fun fetchData() {
        viewModelScope.launch {
            val result = repository.getData()
            result.handleResult(
                onSuccess = { response ->
                    if (response?.items.isNullOrEmpty()) {
                        dataMutableStateFlow.value = DataState.EmptyOnFetch
                    } else {
                        dataMutableStateFlow.value = DataState.DataFetch(response?.items)
                    }
                },
                onError = {
                    dataMutableStateFlow.value = DataState.ErrorOnFetch
                }
            )
        }
}

fun fetchMoreData() {
        viewModelScope.launch {
            val result = repository.getData()
            result.handleResult(
                onSuccess = { response ->
                    if (response?.items.isNullOrEmpty()) {
                        dataMutableStateFlow.value = DataState.EmptyOnFetch
                    } else {
                        dataMutableStateFlow.value = DataState.DataFetch(response?.items)
                    }
                },
                onError = {
                    dataMutableStateFlow.value = DataState.ErrorOnFetch
                }
            )
        }
}
Activity.kt
Copy code
lifecycleScope.launchWhenStarted {
            viewModel.dataMutableStateFlow.collectLatest { state ->
                when (state) {
                    is DataState.DataFetch -> {
                       binding.group.visibility = View.VISIBLE
                    }
                    DataState.EmptyOnFetch,
                    DataState.ErrorOnFetch -> {
                        binding.group.visibility = View.GONE
                    }
                }
            }
        }
}
I have some points which I want to achieve in the standard ways. 1. When your first initial api call 
fetchData()
 if data is not null or empty then we need to show view. If data is empty or null then we need to hide the view. But if api fail then we need to show an error message. 2. When view is visible and view is showing some data. Then we call another api 
fetchMoreData()
 and data is empty or null then I don't want to hide view as per code is written above. And If api fails then we show error message. Thanks in advance