I have built a quiz app. It is my first project so the purpose is to learn app development at this stage.

There are two key things which I am wondering how to do better (the app is fully functional).

1: Livedata / flow / room

I have the following data class

data class SavedScores(
    @PrimaryKey(autoGenerate = true) var id: Int,
    var difficulty: String?,
    var sumtype: String?,
    var questioncount: Int?,
    var answeredcorrectly: Int?

I have the following DAO query

@Query("SELECT COUNT(*) from SavedScores WHERE difficulty = :DIFFICULTY AND questioncount = :QuestionCount AND answeredcorrectly =:QuestionCount")
    fun GetWinsByDifficulty(DIFFICULTY: String,QuestionCount:Int):Flow<Int>

This is where I start writing a LOT of code and wonder if there are better ways to be letting the data flow. In my room repository I am then calling every instance of that query that I require for a statistics page

val easy5: Flow<Int> = savedScoresDao.GetWinsByDifficulty("Easy", 5)
    val easy10: Flow<Int> = savedScoresDao.GetWinsByDifficulty("Easy", 10)
    val easy15: Flow<Int> = savedScoresDao.GetWinsByDifficulty("Easy", 15)
    val easy20: Flow<Int> = savedScoresDao.GetWinsByDifficulty("Easy", 20)
    val easy30: Flow<Int> = savedScoresDao.GetWinsByDifficulty("Easy", 30)

Then I have my view model

val easy5: LiveData<Int> = repository.easy5.asLiveData()
    val easy10: LiveData<Int> = repository.easy10.asLiveData()
    val easy15: LiveData<Int> = repository.easy15.asLiveData()
    val easy20: LiveData<Int> = repository.easy20.asLiveData()
    val easy30: LiveData<Int> = repository.easy30.asLiveData()

And finally my observer and view setter;

savedScoresViewModel.easy5.observe(this) { newValue ->
            tv_easy5.text = newValue.toString()
        savedScoresViewModel.easy10.observe(this) { newValue ->
            tv_easy10.text = newValue.toString()
        savedScoresViewModel.easy15.observe(this) { newValue ->
            tv_easy15.text = newValue.toString()
        savedScoresViewModel.easy20.observe(this) { newValue ->
            tv_easy20.text = newValue.toString()
        savedScoresViewModel.easy30.observe(this) { newValue ->
            tv_easy30.text = newValue.toString()
private fun setView(view:TextView,value:Int){
        if (value == 0){view.setBackgroundResource(R.drawable.somewrong_icon)
        } else {
            if (view.text.toString().toInt()==0){
            } else {view.setBackgroundResource(R.drawable.allcorrect_icon)}

The reality is that currently I have 4 levels of difficulty so for each level I am repeating the above. I also 9 different sum types and have even more queries running to capture that information. Is there a simpler way to code each individual query without having to write so many lines of code? Is it bad practise having so many observers?

2: The second aspect to this is my views. Currently to show all of this data I have a huge amount of views. Whilst I have used recycler view in one part of my app (showing a history of games) I dont see how I can use it for this one as I have so many standalone queries to monitor i.e. I get a count and show just that count. Every new item is a standalone query response rather than a list of them

I appreciate this might end of being quite broad but after all this is about improving code not fixing a problem. I understand therefore if responses tend to focus on one element

I am happy to share more of my code as well but didnt want to load it all if not needed

  • \$\begingroup\$ If anyone reads this it would be good to know if you follow the same suit or not or if you have any improved way to get lots of one shot query results to a users screen \$\endgroup\$ Dec 31, 2020 at 18:20


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.