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).
HomeMenu (Home Menu class)
class HomeMenu : AppCompatActivity() {
private lateinit var textview_diamondsTotal: TextView
private lateinit var textview_goldenCoinsTotal: TextView
private lateinit var textview_livesTotal: TextView
private var int_diamondsTotal = 0
private var int_goldenCoinsTotal = 0
private var int_livesTotal = 0
private lateinit var gameCurrencyManager: GameCurrencyManager //instantiation
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home_menu_new)
gameCurrencyManager = GameCurrencyManager(this) //instantiation
setInGameCurrencyFromGameCurrencyManager()
textview_diamondsTotal = findViewById(R.id.amount_diamonds)
textview_goldenCoinsTotal = findViewById(R.id.amount_goldenCoins)
textview_livesTotal = findViewById(R.id.amount_lives)
//in-game buttons
}
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)
}
}
}
GameCurrencyManager (Game Currency Manager class)
private val Context.gameCurrencyManager by preferencesDataStore("game_currencies")
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() method/function in the MainMenu 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.