Search code examples
kotlinandroid-jetpack-composeandroid-jetpackdatastore

how to add a points system to an app preferences DataStore Jetpack Compose


I'm working on a Quiz app and I'm trying to add a points system to the app so that everytime the user gets a question right he gets a +1pts. and for storing the points I use jetpack compose preferences Datastore. the problem is whenever I want to add a point to the already saved points it doesn't work.

this is my PointsData

class PointsData(private val context: Context) {
    //create the preference datastore
    companion object{
      private val Context.datastore : DataStore<Preferences> by preferencesDataStore("points")
        val CURRENT_POINTS_KEY = intPreferencesKey("points")
    }
    //get the current points
    val getpoints: Flow<Int> =context.datastore.data.map { preferences->
        preferences[CURRENT_POINTS_KEY] ?: 0
    }
    // to save current points
    suspend fun SaveCurrentPoints(numPoints : Int){
        context.datastore.edit {preferences ->
            preferences[PointsData.CURRENT_POINTS_KEY] = numPoints
        }
    }
}

save points methode

class SavePoints {
    companion object {
        @Composable
        fun savepoints(numPointsToSave : Int) {
            val context = LocalContext.current
            val scope = rememberCoroutineScope()
            val datastore = PointsData(context)

            LaunchedEffect(1) {
                scope.launch {
                    datastore.SaveCurrentPoints(numPointsToSave)
                }
            }
        }
    }
}

and whenever i want to get the number of points from my DataStore i use

val pointsdatastore = PointsData(context)
val currentpoints = pointsdatastore.getpoints.collectAsState(initial = 0)
//display it as text for example
                Text(text = currentpoints.value.toString(), fontSize = 30.sp, fontWeight = FontWeight.Bold,
                    color = Color.White)

and to do the operation i want (add +1 o the already existing points i do this

 val pointsdatastore = PointsData(context)
    val currentpoints = pointsdatastore.getpoints.collectAsState(initial = 0)
SavePoints.savepoints(numPointsToSave =  currentpoints.value + 1)

but it doesn't seem to work because the number of points always stays 1.

if you know whats the problem please help.


Solution

  • I found the answer my self but for anyone who is stuck in same situation the solution is to another method in PointsData(look at the question provided code)

    the method is:

    suspend fun incrementpoints(){
            context.datastore.edit { preferences->
                val currentpoints = preferences[CURRENT_POINTS_KEY] ?: 0
                preferences[CURRENT_POINTS_KEY] = currentpoints + 1
    
            }
        }
    

    (if you want decrement not increment you can just change + to - )

    and now in the PointsMethod(look at the question provided code) you should add

    @Composable
            fun incrementpoints() {
                val context = LocalContext.current
                val scope = rememberCoroutineScope()
                val datastore = PointsData(context)
    
                LaunchedEffect(key1 = 1) {
                    scope.launch {
                        datastore.incrementpoints()
                    }
                }
    
            }