Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@LukaKordic
Last active September 4, 2019 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LukaKordic/a6cc33da48fa5440e1477bf46f27e8b2 to your computer and use it in GitHub Desktop.
Save LukaKordic/a6cc33da48fa5440e1477bf46f27e8b2 to your computer and use it in GitHub Desktop.
abstract class BaseViewModel<T : Any, E> : ViewModel(), KoinComponent {
protected val coroutineContext: CoroutineContextProvider by inject()
private val connectivity: Connectivity by inject()
protected val _viewState = MutableLiveData<ViewState<T>>()
val viewState: LiveData<ViewState<T>>
get() = _viewState
protected val _viewEffects = MutableLiveData<E>()
val viewEffects: LiveData<E>
get() = _viewEffects
protected fun executeUseCase(action: suspend () -> Unit, noInternetAction: () -> Unit) {
_viewState.value = Loading()
if (connectivity.hasNetworkAccess()) {
launch { action() }
} else {
noInternetAction()
}
}
protected fun executeUseCase(action: suspend () -> Unit) {
_viewState.value = Loading()
launch { action() }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment