Commit 87a902a0 by likorn

Refactoring in MainActivity

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