Skip to content

Instantly share code, notes, and snippets.

@abhimuktheeswarar
Last active August 1, 2021 10:19
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 abhimuktheeswarar/a50fea2e4fe332ce2d2490d706f59930 to your computer and use it in GitHub Desktop.
Save abhimuktheeswarar/a50fea2e4fe332ce2d2490d706f59930 to your computer and use it in GitHub Desktop.
CounterActivity
class CounterActivity : AppCompatActivity() {
private val viewModel by viewModels<CounterViewModel>()
private lateinit var binding: ActivityCounterBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityCounterBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.buttonDecrement.setOnClickListener {
viewModel.dispatch(DecrementCounter)
}
binding.buttonIncrement.setOnClickListener {
viewModel.dispatch(IncrementCounter)
}
lifecycleScope.launchWhenCreated {
viewModel.stateFlow.collect(::setupViews)
}
}
private fun setupViews(state: CounterState) {
binding.textCount.text = state.count.toString()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment