Skip to content

Instantly share code, notes, and snippets.

@RohitSurwase
Created March 2, 2020 16:33
Show Gist options
  • Save RohitSurwase/12af9027e823009938f43cbbdfb3eee3 to your computer and use it in GitHub Desktop.
Save RohitSurwase/12af9027e823009938f43cbbdfb3eee3 to your computer and use it in GitHub Desktop.
Base AndroidViewModel for MVI architecture using LiveData and ViewModel.
open class AacMviViewModel<STATE, EFFECT, EVENT>(application: Application) :
AndroidViewModel(application), ViewModelContract<EVENT> {
private val _viewStates: MutableLiveData<STATE> = MutableLiveData()
fun viewStates(): LiveData<STATE> = _viewStates
private var _viewState: STATE? = null
protected var viewState: STATE
get() = _viewState
?: throw UninitializedPropertyAccessException("\"viewState\" was queried before being initialized")
set(value) {
Log.d(TAG, "setting viewState : $value")
_viewState = value
_viewStates.value = value
}
private val _viewEffects: SingleLiveEvent<EFFECT> = SingleLiveEvent()
fun viewEffects(): SingleLiveEvent<EFFECT> = _viewEffects
private var _viewEffect: EFFECT? = null
protected var viewEffect: EFFECT
get() = _viewEffect
?: throw UninitializedPropertyAccessException("\"viewEffect\" was queried before being initialized")
set(value) {
Log.d(TAG, "setting viewEffect : $value")
_viewEffect = value
_viewEffects.value = value
}
@CallSuper
override fun process(viewEvent: EVENT) {
Log.d(TAG, "processing viewEvent: $viewEvent")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment