Skip to content

Instantly share code, notes, and snippets.

@RohitSurwase
Created March 2, 2020 16:32
Show Gist options
  • Save RohitSurwase/c1637a2304fd696ba8411b27e5d543a0 to your computer and use it in GitHub Desktop.
Save RohitSurwase/c1637a2304fd696ba8411b27e5d543a0 to your computer and use it in GitHub Desktop.
Abstract Activity for MVI architecture using LiveData and ViewModel.
abstract class AacMviActivity<STATE, EFFECT, EVENT, ViewModel : AacMviViewModel<STATE, EFFECT, EVENT>> :
AppCompatActivity() {
abstract val viewModel: ViewModel
private val viewStateObserver = Observer<STATE> {
Log.d(TAG, "observed viewState : $it")
renderViewState(it)
}
private val viewEffectObserver = Observer<EFFECT> {
Log.d(TAG, "observed viewEffect : $it")
renderViewEffect(it)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel.viewStates().observe(this, viewStateObserver)
viewModel.viewEffects().observe(this, viewEffectObserver)
}
abstract fun renderViewState(viewState: STATE)
abstract fun renderViewEffect(viewEffect: EFFECT)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment