Skip to content

Instantly share code, notes, and snippets.

@ekoo
Created May 22, 2020 03:07
Show Gist options
  • Save ekoo/e2b03f93f537aaad62664e0d5df52d14 to your computer and use it in GitHub Desktop.
Save ekoo/e2b03f93f537aaad62664e0d5df52d14 to your computer and use it in GitHub Desktop.
........
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
viewModel = ViewModelProvider(this)[MainViewModel::class.java]
tapMeButton = findViewById(R.id.tapMeButton)
gameScoreTextView = findViewById(R.id.gameScoreTextView)
timeLeftTextView = findViewById(R.id.timeLeftTextView)
tapMeButton.setOnClickListener { view ->
val bounceAnimation = AnimationUtils.loadAnimation(this, R.anim.bounce)
view.startAnimation(bounceAnimation)
if (!viewModel.isGameStarted){
viewModel.startCountDownTimer()
}else{
viewModel.incrementScore()
}
}
viewModel.timeLeft.observe(this, Observer { timeLeftOnTimer ->
timeLeftTextView.text = getString(R.string.timeLeft, timeLeftOnTimer)
})
viewModel.liveScore.observe(this, Observer { score ->
val blinkAnimation = AnimationUtils.loadAnimation(this, R.anim.blink)
gameScoreTextView.startAnimation(blinkAnimation)
gameScoreTextView.text = getString(R.string.yourScore, score)
})
lifecycleScope.launch{
viewModel.channel.asFlow().collect {lastScore ->
Toast.makeText(this@MainActivity, getString(R.string.gameOverMessage, lastScore), Toast.LENGTH_LONG).show()
}
}
}
..............
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment