Created
June 23, 2021 11:23
-
-
Save gabriel-TheCode/fc13123c951f989f5de5c9d3369fec73 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MainViewModel | |
| @ViewModelInject | |
| constructor( | |
| private val mainRepository: MainRepository, | |
| @Assisted private val savedStateHandle: SavedStateHandle | |
| ) : ViewModel() { | |
| private val _dataState: MutableLiveData<DataState<List<Blog>>> = MutableLiveData() | |
| val dataState: LiveData<DataState<List<Blog>>> | |
| get() = _dataState | |
| fun setStateEvent(mainStateEvent: MainStateEvent) { | |
| viewModelScope.launch { | |
| when (mainStateEvent) { | |
| is MainStateEvent.GetBlogEvents -> { | |
| mainRepository.getBlog() | |
| .onEach { dataState -> | |
| _dataState.value = dataState | |
| } | |
| .launchIn(viewModelScope) | |
| } | |
| is MainStateEvent.None -> { | |
| // No action | |
| } | |
| } | |
| } | |
| } | |
| } | |
| sealed class MainStateEvent { | |
| object GetBlogEvents : MainStateEvent() | |
| object None : MainStateEvent() | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment