Skip to content

Commit

Permalink
Fix: Keep the order right after updating the list
Browse files Browse the repository at this point in the history
  • Loading branch information
machiav3lli committed Jul 14, 2021
1 parent 10253a0 commit 31d539c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
5 changes: 5 additions & 0 deletions app/src/main/java/com/machiav3lli/derdiedas/data/Noun.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ import androidx.room.PrimaryKey
class Noun(val noun: String, val gender: String, var timesAnswered: Int) {
@PrimaryKey(autoGenerate = true)
var id: Long = 0

fun withID(id: Long): Noun {
this.id = id
return this
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.machiav3lli.derdiedas.databinding.FragmentWordBinding
import com.machiav3lli.derdiedas.utils.AnimationUtil.animateButtonDrawable
import com.machiav3lli.derdiedas.utils.AnimationUtil.animateJumpAndSlide
import com.machiav3lli.derdiedas.utils.getStringByName
import com.machiav3lli.derdiedas.utils.updateIds

class WordFragment : Fragment(), View.OnClickListener {
private var currentNounList: MutableList<Noun> = mutableListOf()
Expand Down Expand Up @@ -66,7 +67,7 @@ class WordFragment : Fragment(), View.OnClickListener {
}

private fun updateList(isCorrect: Boolean) {
val newList = currentNounList.getUpdatedNounList(currentNoun, isCorrect)
val newList = currentNounList.getUpdatedNounList(currentNoun, isCorrect).updateIds()
(requireActivity() as WordActivity).updateNounList(newList)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ package com.machiav3lli.derdiedas.utils

import android.content.Context
import android.content.res.Resources
import com.machiav3lli.derdiedas.data.Noun

fun Context.getStringByName(name: String) = try {
val resId = resources.getIdentifier(name, "string", packageName)
getString(resId)
} catch (e: Resources.NotFoundException) {
null
}
}

fun MutableList<Noun>.updateIds() = mapIndexed { index, noun ->
noun.withID(index + 1L)
}.toMutableList()

0 comments on commit 31d539c

Please sign in to comment.