Commit 87a902a0 by likorn

Refactoring in MainActivity

parent be189be0
......@@ -12,8 +12,7 @@ class MainActivity : AppCompatActivity() {
private var secToSolve: Int = 4
private var numDigits: Int = 3
private lateinit var checkedCard: MaterialCardView
private lateinit var cards: List<MaterialCardView>
private lateinit var cards: Map<Int, MaterialCardView>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
......@@ -22,7 +21,12 @@ class MainActivity : AppCompatActivity() {
if (savedInstanceState == null) retrieveSharedPrefs()
else restoreInstanceState(savedInstanceState)
checkedCard.isChecked = true
cards = mapOf(2 to card_2_digits, 3 to card_3_digits, 4 to card_4_digits)
cards.values.forEach { card ->
card.setOnClickListener { card.isChecked = true }
card.setOnCheckedChangeListener(checkedChangeListener)
}
cards.getValue(numDigits).isChecked = true
seek_bar.apply {
setMinStartValue(secToSolve.toFloat()).apply()
......@@ -32,14 +36,10 @@ class MainActivity : AppCompatActivity() {
}
}
cards = listOf(card_2_digits, card_3_digits, card_4_digits)
cards.forEach { card -> card.setOnClickListener(cardOnClickListener) }
btn_play.setOnClickListener(playClickListener)
}
private fun restoreInstanceState(savedInstanceState: Bundle) {
checkedCard = findViewById(savedInstanceState.getInt("checked_num_id"))
secToSolve = savedInstanceState.getInt("sec_to_solve")
numDigits = savedInstanceState.getInt("num_digits")
}
......@@ -48,24 +48,18 @@ class MainActivity : AppCompatActivity() {
super.onSaveInstanceState(outState)
outState.putInt("num_digits", numDigits)
outState.putInt("sec_to_solve", secToSolve)
outState.putInt("checked_num_id", checkedCard.id)
}
private val cardOnClickListener = View.OnClickListener { card ->
val cardM = card as MaterialCardView
if (!cardM.isChecked) {
checkedCard = cardM
checkedCard.isChecked = true
numDigits = numDigitsFromCard(checkedCard)
cards.forEach { c ->
if (c.id != checkedCard.id)
c.isChecked = false
private val checkedChangeListener = MaterialCardView.OnCheckedChangeListener { card, isChecked ->
if (isChecked && card != cards.getValue(numDigits)) {
numDigits = getTextView(card).text.toString().toInt()
cards.values.forEach { c ->
if (c != card) c.isChecked = false
}
}
}
private val playClickListener = View.OnClickListener {
saveSelectedValues()
val intent = Intent(this, TaskActivity::class.java).also { i ->
i.putExtra("num_digits", numDigits)
i.putExtra("sec_to_solve", secToSolve)
......@@ -73,26 +67,21 @@ class MainActivity : AppCompatActivity() {
startActivity(intent)
}
override fun onStop() {
super.onStop()
saveSelectedValues()
}
private fun saveSelectedValues() {
val editor = getSharedPreferences("my_prefs", Context.MODE_PRIVATE).edit()
editor.putInt("sec_to_solve", secToSolve)
editor.putInt("checked_num_id", checkedCard.id)
editor.putInt("num_digits", numDigits)
editor.apply()
}
private fun retrieveSharedPrefs() {
val prefs = getSharedPreferences("my_prefs", Context.MODE_PRIVATE)
secToSolve = prefs.getInt("sec_to_solve", 4)
checkedCard = findViewById(
prefs.getInt(
"checked_num_id",
R.id.card_3_digits
)
)
numDigits = numDigitsFromCard(checkedCard)
}
private fun numDigitsFromCard(card: MaterialCardView): Int {
return getTextView(card).text.toString().toInt()
numDigits = prefs.getInt("num_digits", 3)
}
}
\ No newline at end of file
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