Commit 53e74182 by likorn

Refactoring

parent 87a902a0
...@@ -18,16 +18,19 @@ class MainActivity : AppCompatActivity() { ...@@ -18,16 +18,19 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
// restore data
if (savedInstanceState == null) retrieveSharedPrefs() if (savedInstanceState == null) retrieveSharedPrefs()
else restoreInstanceState(savedInstanceState) else restoreInstanceState(savedInstanceState)
// set up cards
cards = mapOf(2 to card_2_digits, 3 to card_3_digits, 4 to card_4_digits) cards = mapOf(2 to card_2_digits, 3 to card_3_digits, 4 to card_4_digits)
cards.values.forEach { card -> cards.values.forEach { card ->
card.setOnClickListener { card.isChecked = true } card.setOnClickListener { card.isChecked = true }
card.setOnCheckedChangeListener(checkedChangeListener) card.setOnCheckedChangeListener { card, isChecked -> updateCards(card, isChecked) }
} }
cards.getValue(numDigits).isChecked = true cards.getValue(numDigits).isChecked = true
// set up seek bar
seek_bar.apply { seek_bar.apply {
setMinStartValue(secToSolve.toFloat()).apply() setMinStartValue(secToSolve.toFloat()).apply()
setOnSeekbarChangeListener { n -> setOnSeekbarChangeListener { n ->
...@@ -36,7 +39,8 @@ class MainActivity : AppCompatActivity() { ...@@ -36,7 +39,8 @@ class MainActivity : AppCompatActivity() {
} }
} }
btn_play.setOnClickListener(playClickListener) // set up button play
btn_play.setOnClickListener { startTaskActivity() }
} }
private fun restoreInstanceState(savedInstanceState: Bundle) { private fun restoreInstanceState(savedInstanceState: Bundle) {
...@@ -50,7 +54,7 @@ class MainActivity : AppCompatActivity() { ...@@ -50,7 +54,7 @@ class MainActivity : AppCompatActivity() {
outState.putInt("sec_to_solve", secToSolve) outState.putInt("sec_to_solve", secToSolve)
} }
private val checkedChangeListener = MaterialCardView.OnCheckedChangeListener { card, isChecked -> private fun updateCards(card: MaterialCardView, isChecked: Boolean) {
if (isChecked && card != cards.getValue(numDigits)) { if (isChecked && card != cards.getValue(numDigits)) {
numDigits = getTextView(card).text.toString().toInt() numDigits = getTextView(card).text.toString().toInt()
cards.values.forEach { c -> cards.values.forEach { c ->
...@@ -59,24 +63,20 @@ class MainActivity : AppCompatActivity() { ...@@ -59,24 +63,20 @@ class MainActivity : AppCompatActivity() {
} }
} }
private val playClickListener = View.OnClickListener { private fun startTaskActivity() {
val intent = Intent(this, TaskActivity::class.java).also { i -> val intent = Intent(this, TaskActivity::class.java)
i.putExtra("num_digits", numDigits) .apply { putExtra("num_digits", numDigits) }
i.putExtra("sec_to_solve", secToSolve) .apply { putExtra("sec_to_solve", secToSolve) }
}
startActivity(intent) startActivity(intent)
} }
override fun onStop() { override fun onStop() {
super.onStop() super.onStop()
saveSelectedValues() // save to shared preferences
} getSharedPreferences("my_prefs", Context.MODE_PRIVATE).edit()
.apply { putInt("sec_to_solve", secToSolve) }
private fun saveSelectedValues() { .apply { putInt("num_digits", numDigits) }
val editor = getSharedPreferences("my_prefs", Context.MODE_PRIVATE).edit() .apply()
editor.putInt("sec_to_solve", secToSolve)
editor.putInt("num_digits", numDigits)
editor.apply()
} }
private fun retrieveSharedPrefs() { private fun retrieveSharedPrefs() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment