I am attempting to get the boolean value from the datastore. Whenever the player has scored 10 out of 10, a platinum is awarded to the player. However, if the player has scored 10 out of 10 for the second time (or more), then the platinum does not get awarded, as the player has already received it (or in essence, unlocked it).
There are a few problems:
- Whenever the player scores 10 out of 10, then the "textview_platinumMessage" is set to "Already achieved!". Notwithstanding that, the platinum gets awarded.
- When the player scores 10 out of 10 the second time, it seems to work fine.
- However, when the user closes the app, and the player scores 10 out of 10 for the third (or more) time, the platinum gets awarded, and the message says "You have been awarded 1 Platinum. Congratulations!", when it should have been "Already achieved!".
Code to determine if the platinum has already been earned in previous attempts.
class endOfGameScreen: AppCompatActivity {
lateinit var boolean_platinumIsEarned = false
lateinit var singlePlayerPlatinumManager: singlePlayerPlatinumManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_level_completed)
getBackendData()
}
fun getBackendData(){
singlePlayerPlatinumManager = singlePlayerPlatinumManager(this)
lifecycleScope.launch {
boolean_platinumIsEarned = singlePlayerPlatinumManager.getLevelOnePlatinumBall.first()
}
setInGameCurrencyScoresToEndScreen()
}
fun setInGameCurrencyScoresToEndScreen(){
if (int_correctAnswers == int_totalNumberOfQuestions) {
textview_status.setText("CONGRATULATIONS!")
icon_platinum.setImageResource(R.drawable.platinum)
singlePlayerPlatinumManager.getLevelOnePlatinumBall.asLiveData().observe(this) {
if (boolean_platinumIsEarned == true) {
textview_platinumMessage.setText("Already achieved!")
} else {
int_platinumToBeAwarded = 1
textview_platinumMessage.setText("You have been awarded 1 Platinum. Congratulations!")
GlobalScope.launch {
gameCurrencyManager.setPlatinum(int_currentPlatinumTotal + int_platinumToBeAwarded)
singlePlayerPlatinumManager.setLevelOnePlatinumBall(true)
}
}
}
}
}
}
Code for the datastore preferences for platinum being awarded. I am not sure if there is anything wrong here, bearing in mind that I have made similar datastore preferences classes, but for integer values.
private val Context.singlePlayerPlatinumManager by preferencesDataStore("single_player_platinum_manager")
class singlePlayerPlatinumManager(context: Context) {
private val singlePlayerPlatinumManager = context.singlePlayerPlatinumManager
companion object{
val LEVEL_ONE_PLATINUM_BALL_KEY = booleanPreferencesKey("LEVEL_ONE_PLATINUM_KEY")
//.
}
//SETTER
suspend fun setLevelOnePlatinum(boolean_levelOnePlatinum: Boolean){
singlePlayerLevelPlatinumManager.edit{
it[LEVEL_ONE_PLATINUM_KEY] = boolean_levelOnePlatinumBall
}
}
//GETTER
val getLevelOnePlatinum: Flow<Boolean> = singlePlayerLevelPlatinumManager.data.map{
it[LEVEL_ONE_PLATINUM_KEY] ?: false
}
}