Dynamic alteartion sorting

This commit is contained in:
Thomas Andres Gomez 2023-12-18 09:14:04 +01:00
parent ae6581219b
commit 70ae338e23
3 changed files with 52 additions and 17 deletions

View file

@ -103,4 +103,22 @@ class AlterationRepository @Inject constructor(
lastSuccessFullUpdate = Update.currentTime()
}
}
companion object {
fun sort(sheet: CharacterSheet?): Comparator<String> {
return compareByDescending<String> {
sheet?.name == it
}.thenByDescending {
PLAYER == it
}.thenByDescending {
sheet?.characterClass?.any { clazz -> clazz.value == it }
}.thenByDescending {
sheet?.race == it
}.thenBy {
it
}
}
private const val PLAYER = "Joueur"
}
}

View file

@ -8,18 +8,21 @@ import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope
import com.pixelized.rplexicon.R
import com.pixelized.rplexicon.data.repository.character.AlterationRepository
import com.pixelized.rplexicon.data.repository.character.CharacterSheetRepository
import com.pixelized.rplexicon.data.repository.character.DescriptionRepository
import com.pixelized.rplexicon.ui.navigation.screens.characterSheetArgument
import com.pixelized.rplexicon.ui.screens.rolls.factory.AlterationFactory
import com.pixelized.rplexicon.utilitary.extentions.context
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import javax.inject.Inject
@HiltViewModel
class AlterationViewModel @Inject constructor(
private val characterSheetRepository: CharacterSheetRepository,
private val alterationRepository: AlterationRepository,
private val descriptionRepository: DescriptionRepository,
private val factory: AlterationFactory,
@ -37,23 +40,32 @@ class AlterationViewModel @Inject constructor(
init {
viewModelScope.launch {
launch(Dispatchers.IO) {
alterationRepository.assignedAlterations.collect { alterationMaps ->
val alterations = alterationMaps[character] ?: emptyList()
val data = alterations
.groupBy { alteration -> alteration.source }
.map {
AlterationGroupUio(
name = it.key,
alterations = factory.convert(character = character, alterations = it.value)
.sortedBy { alteration -> alteration.label }
)
}
.sortedBy { it.name }
withContext(Dispatchers.Main) {
_alterations.value = data
characterSheetRepository.data
.combine(alterationRepository.assignedAlterations) { sheet, alterations ->
sheet[character] to alterations
}
.collect { entry ->
val (sheet, alterationMaps) = entry
val alterations = alterationMaps[character] ?: emptyList()
val data = alterations
.groupBy { alteration -> alteration.source }
.toSortedMap(AlterationRepository.sort(sheet = sheet))
.map {
AlterationGroupUio(
name = it.key,
alterations = factory.convert(
character = character,
alterations = it.value
).sortedBy { alteration ->
alteration.label
}
)
}
withContext(Dispatchers.Main) {
_alterations.value = data
}
}
}
}
}
}

View file

@ -8,8 +8,10 @@ import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.pixelized.rplexicon.R
import com.pixelized.rplexicon.business.DiceThrowUseCase
import com.pixelized.rplexicon.data.model.CharacterSheet
import com.pixelized.rplexicon.data.model.DiceThrow
import com.pixelized.rplexicon.data.repository.character.AlterationRepository
import com.pixelized.rplexicon.data.repository.character.CharacterSheetRepository
import com.pixelized.rplexicon.data.repository.character.DescriptionRepository
import com.pixelized.rplexicon.ui.screens.character.composable.actions.AlterationItemUio
import com.pixelized.rplexicon.ui.screens.character.pages.alteration.AlterationDetailUio
@ -28,6 +30,7 @@ import javax.inject.Inject
@HiltViewModel
class RollOverlayViewModel @Inject constructor(
private val characterSheetRepository: CharacterSheetRepository,
private val alterationRepository: AlterationRepository,
private val descriptionRepository: DescriptionRepository,
private val rollUseCase: DiceThrowUseCase,
@ -35,6 +38,7 @@ class RollOverlayViewModel @Inject constructor(
private val alterationFactory: AlterationFactory,
application: Application,
) : AndroidViewModel(application) {
private var sheet: CharacterSheet? = null
private var diceThrow: DiceThrow? = null
private var rollJob: Job? = null
@ -42,13 +46,13 @@ class RollOverlayViewModel @Inject constructor(
val alterations: State<List<AlterationGroupUio>> = derivedStateOf {
_alterations.value
.groupBy { it.source }
.toSortedMap(AlterationRepository.sort(sheet = sheet))
.map { entry ->
AlterationGroupUio(
name = entry.key,
alterations = entry.value.sortedBy { it.label }
)
}
.sortedBy { it.name }
}
private val _dice = mutableStateOf<RollDiceUio?>(null)
@ -66,6 +70,7 @@ class RollOverlayViewModel @Inject constructor(
fun prepareRoll(diceThrow: DiceThrow) {
this.diceThrow = diceThrow
_card.value = null
sheet = characterSheetRepository.find(name = diceThrow.character)
_dice.value = diceFactory.convertDiceThrow(diceThrow)
_alterations.value = alterationFactory.convertDiceThrow(diceThrow)
}