Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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