Skip to content

Commit

Permalink
Fix xp formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandregpereira committed Jan 13, 2024
1 parent a80c849 commit 6376aba
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import br.alexandregpereira.hunter.domain.monster.spell.model.SpellPreview
import br.alexandregpereira.hunter.domain.monster.spell.model.SpellUsage
import br.alexandregpereira.hunter.domain.monster.spell.model.Spellcasting
import br.alexandregpereira.hunter.domain.monster.spell.model.SpellcastingType
import java.text.NumberFormat
import kotlin.native.ObjCName

@ObjCName(name = "Monster", exact = true)
Expand Down Expand Up @@ -111,6 +112,20 @@ private fun Monster.challengeRatingToXp(): Int {
}
}

fun Monster.xpFormatted(): String {
val xpString = when {
xp < 1000 -> xp.toString()
else -> {
val xpFormatted = NumberFormat.getIntegerInstance().format(xp)
.dropLastWhile { it == '0' }
.let { if (it.last().isDigit().not()) it.dropLast(1) else it }
"${xpFormatted}k"
}
}

return "$xpString XP"
}

fun getFakeMonster(): Monster {
return Monster(
index = "1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import br.alexandregpereira.hunter.domain.model.MonsterType
import br.alexandregpereira.hunter.domain.model.SavingThrow
import br.alexandregpereira.hunter.domain.model.Skill
import br.alexandregpereira.hunter.domain.model.SpeedValue
import br.alexandregpereira.hunter.domain.model.xpFormatted
import br.alexandregpereira.hunter.domain.monster.spell.model.Spellcasting
import br.alexandregpereira.hunter.ui.compose.SchoolOfMagicState
import java.text.NumberFormat
Expand Down Expand Up @@ -104,20 +105,6 @@ private fun Monster.asState(): MonsterState {
)
}

private fun Monster.xpFormatted(): String {
val xpString = when {
xp < 1000 -> xp.toString()
else -> {
val xpFormatted = NumberFormat.getIntegerInstance().format(xp)
.replace("0", "")
.let { if (it.last().isDigit().not()) it.dropLast(1) else it }
"${xpFormatted}k"
}
}

return "$xpString XP"
}

private fun MonsterImageData.asState(
type: MonsterType,
challengeRating: Float,
Expand Down

0 comments on commit 6376aba

Please sign in to comment.