Last active
April 12, 2021 23:19
-
-
Save fvilarino/ecc86170d488582173b1b603b670d872 to your computer and use it in GitHub Desktop.
Sample MVI ViewModel with LiveData
This file contains 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
enum class LoadState { | |
IDLE, | |
LOADING, | |
LOADED, | |
ERROR | |
} | |
data class CitiesState( | |
val loadState: LoadState, | |
val cities: List<CityResultModel>, | |
val errorMessage: CharSequence, | |
) { | |
companion object { | |
val initial = CitiesState( | |
loadState = LoadState.IDLE, | |
cities = emptyList(), | |
errorMessage = "", | |
) | |
} | |
} | |
@HiltViewModel | |
class CityViewModel @Inject constructor( | |
private val getCitiesInteractor: GetCitiesInteractor, | |
private val navigator: Navigator, | |
) { | |
private val _state = MutableLiveData<CitiesState>(CitiesState.initial) | |
val state: LiveData<CitiesState> | |
get() = _state | |
// implementation follows | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment