Commit be189be0 by likorn

Selections are saved during orientation change in the MainActivity

parent d4831526
...@@ -13,33 +13,52 @@ class MainActivity : AppCompatActivity() { ...@@ -13,33 +13,52 @@ 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 checkedCard: MaterialCardView
private lateinit var cards: List<MaterialCardView>
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
retrieveSharedPrefs()
seek_bar.setMinStartValue(secToSolve.toFloat()).apply()
seek_bar.setOnSeekbarChangeListener { n -> if (savedInstanceState == null) retrieveSharedPrefs()
else restoreInstanceState(savedInstanceState)
checkedCard.isChecked = true
seek_bar.apply {
setMinStartValue(secToSolve.toFloat()).apply()
setOnSeekbarChangeListener { n ->
seek_bar_value.text = resources.getString(R.string.time_to_solve, n.toString()) seek_bar_value.text = resources.getString(R.string.time_to_solve, n.toString())
secToSolve = n.toInt() secToSolve = n.toInt()
} }
}
card_2_digits.setOnClickListener(cardOnClickListener) cards = listOf(card_2_digits, card_3_digits, card_4_digits)
card_3_digits.setOnClickListener(cardOnClickListener) cards.forEach { card -> card.setOnClickListener(cardOnClickListener) }
card_4_digits.setOnClickListener(cardOnClickListener)
btn_play.setOnClickListener(playClickListener) 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")
}
override fun onSaveInstanceState(outState: Bundle) {
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 -> private val cardOnClickListener = View.OnClickListener { card ->
val cardM = card as MaterialCardView val cardM = card as MaterialCardView
if (!cardM.isChecked) { if (!cardM.isChecked) {
checkedCard = cardM checkedCard = cardM
checkedCard.isChecked = true checkedCard.isChecked = true
numDigits = numDigitsFromCard(checkedCard) numDigits = numDigitsFromCard(checkedCard)
listOf(card_2_digits, card_3_digits, card_4_digits).forEach { c -> cards.forEach { c ->
if (c.id != cardM.id) if (c.id != checkedCard.id)
c.isChecked = false c.isChecked = false
} }
} }
...@@ -64,11 +83,13 @@ class MainActivity : AppCompatActivity() { ...@@ -64,11 +83,13 @@ class MainActivity : AppCompatActivity() {
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(prefs.getInt("checked_num_id", checkedCard = findViewById(
prefs.getInt(
"checked_num_id",
R.id.card_3_digits R.id.card_3_digits
)) )
)
numDigits = numDigitsFromCard(checkedCard) numDigits = numDigitsFromCard(checkedCard)
checkedCard.isChecked = true
} }
private fun numDigitsFromCard(card: MaterialCardView): Int { private fun numDigitsFromCard(card: MaterialCardView): Int {
......
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