Skip to content

Commit

Permalink
Remember last selected instrument
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteHamster committed May 21, 2021
1 parent 0110da9 commit 7ce2bb3
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions app/src/main/java/com/tunerly/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ import com.akexorcist.localizationactivity.ui.LocalizationActivity
import com.example.tuner.R
import com.example.tuner.databinding.ActivityMainBinding
import java.util.*
import kotlin.math.min

class MainActivity : LocalizationActivity(), MyCallback {
private lateinit var binding: ActivityMainBinding
private val prefLastInstrument = "last_instrument"
private val prefLastTuning = "last_tuning"
private val processing = PitchProcessing(this@MainActivity)
private val sampleRate = 44100
private val recordOverlaps = 3072
Expand Down Expand Up @@ -66,11 +69,9 @@ class MainActivity : LocalizationActivity(), MyCallback {
)
}

// save current state of dark mode
val isNightMode: Boolean = PreferenceManager.getDefaultSharedPreferences(this).getBoolean(
"dark_theme",
false
)
val preferences = PreferenceManager.getDefaultSharedPreferences(this)
// load current state of dark mode
val isNightMode: Boolean = preferences.getBoolean("dark_theme", false)
if (isNightMode) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
} else {
Expand Down Expand Up @@ -185,6 +186,16 @@ class MainActivity : LocalizationActivity(), MyCallback {
if (permission == PermissionChecker.PERMISSION_GRANTED) {
audioProcessing()
}

// load last state of the spinners
instrumentSpinner.setSelection(
min(preferences.getInt(prefLastInstrument, 0), instrumentSpinner.adapter.count - 1))
instrumentSpinner.post { // Give time to the instrumentSpinner to trigger its onItemSelected
tuningSpinner.post { // Give time to the to tuningSpinner to actually be updated
tuningSpinner.setSelection(
min(preferences.getInt(prefLastTuning, 0), tuningSpinner.adapter.count - 1))
}
}
}

private fun audioProcessing() {
Expand Down Expand Up @@ -243,6 +254,14 @@ class MainActivity : LocalizationActivity(), MyCallback {
return true
}

override fun onStop() {
super.onStop()
PreferenceManager.getDefaultSharedPreferences(this).edit()
.putInt(prefLastInstrument, instrumentSpinner.selectedItemPosition)
.putInt(prefLastTuning, tuningSpinner.selectedItemPosition)
.apply()
}

// Items in menu
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.itemId
Expand Down

0 comments on commit 7ce2bb3

Please sign in to comment.