Last active
August 1, 2021 10:19
-
-
Save abhimuktheeswarar/a50fea2e4fe332ce2d2490d706f59930 to your computer and use it in GitHub Desktop.
CounterActivity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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