Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

likorn / quick_max

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Members
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Switch branch/tag
  • quick_max
  • ..
  • quickmax
  • MainActivity.kt
Find file
BlameHistoryPermalink
  • likorn's avatar
    Refactoring · 53e74182
    likorn committed 5 years ago
    53e74182
MainActivity.kt 2.98 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
package com.paktalin.quickmax

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.card.MaterialCardView
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    private var secToSolve: Int = 4
    private var numDigits: Int = 3
    private lateinit var cards: Map<Int, MaterialCardView>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // restore data
        if (savedInstanceState == null) retrieveSharedPrefs()
        else restoreInstanceState(savedInstanceState)

        // set up cards
        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 { card, isChecked -> updateCards(card, isChecked) }
        }
        cards.getValue(numDigits).isChecked = true

        // set up seek bar
        seek_bar.apply {
            setMinStartValue(secToSolve.toFloat()).apply()
            setOnSeekbarChangeListener { n ->
                seek_bar_value.text = resources.getString(R.string.time_to_solve, n.toString())
                secToSolve = n.toInt()
            }
        }

        // set up button play
        btn_play.setOnClickListener { startTaskActivity() }
    }

    private fun restoreInstanceState(savedInstanceState: Bundle) {
        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)
    }

    private fun updateCards(card: MaterialCardView, isChecked: Boolean) {
        if (isChecked && card != cards.getValue(numDigits)) {
            numDigits = getTextView(card).text.toString().toInt()
            cards.values.forEach { c ->
                if (c != card) c.isChecked = false
            }
        }
    }

    private fun startTaskActivity() {
        val intent = Intent(this, TaskActivity::class.java)
            .apply { putExtra("num_digits", numDigits) }
            .apply { putExtra("sec_to_solve", secToSolve) }
        startActivity(intent)
    }

    override fun onStop() {
        super.onStop()
        // save to shared preferences
        getSharedPreferences("my_prefs", Context.MODE_PRIVATE).edit()
            .apply { putInt("sec_to_solve", secToSolve) }
            .apply { putInt("num_digits", numDigits) }
            .apply()
    }

    private fun retrieveSharedPrefs() {
        val prefs = getSharedPreferences("my_prefs", Context.MODE_PRIVATE)
        secToSolve = prefs.getInt("sec_to_solve", 4)
        numDigits = prefs.getInt("num_digits", 3)
    }
}