In my quiz app, there are in-game currencies: diamonds, coins and lives. By default, the number of lives should be five (when the user first installs the app, with the number of lives going no more than five, but no less than zero).
Main Menu class
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home_menu_new)
gameCurrencyManager = gameCurrencyManager(this)
setInGameCurrencyFromGameCurrencyManager()
val button_play = findViewById<Button>(R.id.button_singlePlayer)
val button_shop = findViewById<ConstraintLayout>(R.id.button_shop)
val button_settings = findViewById<ConstraintLayout>(R.id.button_settings)
val button_information = findViewById<ConstraintLayout>(R.id.button_information)
textview_diamondsTotal = findViewById(R.id.amount_diamonds)
textview_goldenCoinsTotal = findViewById(R.id.amount_goldenCoins)
textview_livesTotal = findViewById(R.id.amount_lives)
button_play.setOnClickListener {
val nextPage = Intent(this, playMenu::class.java);
startActivity(nextPage);
finish();
}
button_shop.setOnClickListener {
val nextPage = Intent(this, shopMenu::class.java);
startActivity(nextPage);
finish();
}
button_settings.setOnClickListener {
val nextPage = Intent(this, settingsMenu::class.java);
startActivity(nextPage);
finish();
}
button_information.setOnClickListener{
val nextPage = Intent(this, informationMenu::class.java)
startActivity(nextPage)
finish()
}
}
fun setInGameCurrencyFromGameCurrencyManager(){
gameCurrencyManager.getdiamonds.asLiveData().observe(this) {
int_diamondsTotal = it
textview_diamondsTotal.setText(" $int_diamondsTotal", TextView.BufferType.NORMAL)
}
gameCurrencyManager.getGoldenCoins.asLiveData().observe(this) {
int_goldenCoinsTotal = it
textview_goldenCoinsTotal.setText(" $int_goldenCoinsTotal", TextView.BufferType.NORMAL)
}
gameCurrencyManager.getlives.asLiveData().observe(this) {
int_livesTotal = it
textview_livesTotal.setText("$int_livesTotal", TextView.BufferType.NORMAL)
}
}
Game Currency Manager class
class gameCurrencyManager(context: Context) {
private val gameCurrencyManager = context.gameCurrencyManager
companion object {
val DIAMONDS_KEY = intPreferencesKey("DIAMONDS_KEY")
val GOLDEN_COINS_KEY = intPreferencesKey("GOLDEN_COINS_KEY")
val LIVES_KEY = intPreferencesKey("LIVES_KEY")
}
suspend fun setDiamonds(int_diamonds: Int){
gameCurrencyManager.edit {
it[DIAMONDS_KEY] = int_diamonds
}
}
suspend fun setGoldenCoins(int_goldenCoins: Int){
gameCurrencyManager.edit {
it[GOLDEN_COINS_KEY] = int_goldenCoins
}
}
suspend fun setLives(int_lives: Int){
gameCurrencyManager.edit {
it[LIVES_KEY] = int_lives.coerceIn(0,5)
}
}
val getDiamonds: Flow<Int> = gameCurrencyManager.data.map {
it[DIAMONDS_KEY] ?: 0
}
val getGoldenCoins: Flow<Int> = gameCurrencyManager.data.map {
it[GOLDEN_COINS_KEY] ?: 0
}
val getLives: Flow<Int> = gameCurrencyManager.data.map {
it[LIVES_KEY] ?: 5
}
}
I tried to add the following lines of code above the setInGameCurrencyFromGameCurrencyManager() in the Main Menu class:
lifecycleScope.launch {
int_diamondsTotal = gameCurrencyManager.getDiamonds.first()
int_goldenCoinsTotal = gameCurrencyManager.getGoldenCoins.first()
int_livesTotal = gameCurrencyManager.getLives.first()
}
And I have also tried to remove the “int_livesTotal = it” in the Main Menu class, but all it did was set the number of lives to 0. It seems to me that it may be some sort of data leak or I may have implemented one or both classes incorrectly. I’m not really sure what is wrong here.