1. Load에서 편집 기능 사용 시 Repetition 변경할 경우 앱이 튕기는 증상 수정
2. Pulse Duration 및 Fluence 변경 할 경우 Repetition 제한되는 값이 최대 범위를 벗어날 경우, 변경된 Pulse Duration 및 Fluence에 맞게 최대값으로 설정되도록 수정
This commit is contained in:
@@ -28,6 +28,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import timber.log.Timber
|
||||
import kotlin.math.abs
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
@@ -80,6 +81,49 @@ class PresetViewModel @Inject constructor(
|
||||
_repetitionList.value = r
|
||||
}
|
||||
|
||||
private data class ResolvedRepetitionContext(
|
||||
val fluence: Float,
|
||||
val repetitionOptions: List<Float>,
|
||||
)
|
||||
|
||||
private fun resolveRepetitionContext(
|
||||
pulseWidth: Float,
|
||||
fluence: Float,
|
||||
): ResolvedRepetitionContext {
|
||||
val availableFluenceList = energyTable.value.getKey2ListForKey1(pulseWidth)
|
||||
val resolvedFluence = availableFluenceList.minByOrNull { abs(it - fluence) }
|
||||
?: availableFluenceList.firstOrNull()
|
||||
?: fluence
|
||||
|
||||
val hzType = hzTable.value.getValue(pulseWidth, resolvedFluence)
|
||||
val repetitionOptions = RepetitionsByColorKey[hzType] ?: RepetitionsByColorKey[KEY_YELLOW].orEmpty()
|
||||
|
||||
if (hzType == null) {
|
||||
Timber.w(
|
||||
"No hzType found for pulseWidth=%s, fluence=%s. Falling back to default repetition list.",
|
||||
pulseWidth,
|
||||
resolvedFluence
|
||||
)
|
||||
}
|
||||
|
||||
setRepetitionList(repetitionOptions)
|
||||
return ResolvedRepetitionContext(
|
||||
fluence = resolvedFluence,
|
||||
repetitionOptions = repetitionOptions,
|
||||
)
|
||||
}
|
||||
|
||||
private fun resolveNearestLowerOrEqualRepetition(
|
||||
currentRepetition: Float,
|
||||
repetitionOptions: List<Float>,
|
||||
): Float {
|
||||
return repetitionOptions
|
||||
.filter { it <= currentRepetition }
|
||||
.maxOrNull()
|
||||
?: repetitionOptions.minOrNull()
|
||||
?: currentRepetition
|
||||
}
|
||||
|
||||
fun loadHzTable(handPieceType: Int) {
|
||||
Timber.d("loadHzTable($handPieceType)")
|
||||
|
||||
@@ -251,21 +295,17 @@ class PresetViewModel @Inject constructor(
|
||||
|
||||
// 4. fluence update (energyTable)
|
||||
val newFluenceList = energyTable.value.getKey2ListForKey1(newPulseWidth)
|
||||
val newFluence = newFluenceList.get(0)
|
||||
val newFluence = newFluenceList.firstOrNull() ?: currentList[index].fluence
|
||||
|
||||
// 5. repetition update (hzTable)
|
||||
val hzType = hzTable.value.getValue(newPulseWidth, newFluence)
|
||||
Timber.d("hzType: ${hzType}")
|
||||
val list = RepetitionsByColorKey.get(hzType)!!
|
||||
setRepetitionList( list )
|
||||
|
||||
val newRepetitionIndex = 0
|
||||
val newRepetition = list[newRepetitionIndex]
|
||||
val repetitionContext = resolveRepetitionContext(newPulseWidth, newFluence)
|
||||
Timber.d("Resolved repetition options for handPieceType update: ${repetitionContext.repetitionOptions}")
|
||||
val newRepetition = repetitionContext.repetitionOptions.firstOrNull() ?: currentList[index].repetition
|
||||
|
||||
val updatedPreset = currentList[index].copy(
|
||||
handPieceType = newHandPieceType,
|
||||
pulseWidth = newPulseWidth,
|
||||
fluence = newFluence,
|
||||
fluence = repetitionContext.fluence,
|
||||
repetition = newRepetition
|
||||
)
|
||||
currentList[index] = updatedPreset
|
||||
@@ -311,20 +351,18 @@ class PresetViewModel @Inject constructor(
|
||||
|
||||
// 3. update fluence
|
||||
val newFluenceList = energyTable.value.getKey2ListForKey1(newPulseWidth)
|
||||
val newFluenceIndex = 0
|
||||
val newFluence = newFluenceList.get(newFluenceIndex)
|
||||
val newFluence = newFluenceList.firstOrNull() ?: currentList[index].fluence
|
||||
|
||||
// 4. update repetition
|
||||
val hzType = hzTable.value.getValue(newPulseWidth, newFluence)
|
||||
val list = RepetitionsByColorKey[hzType]!!
|
||||
setRepetitionList( list )
|
||||
|
||||
val newRepetitionIndex = 0
|
||||
val newRepetition = repetitionList.value[newRepetitionIndex]
|
||||
val repetitionContext = resolveRepetitionContext(newPulseWidth, newFluence)
|
||||
val newRepetition = resolveNearestLowerOrEqualRepetition(
|
||||
currentRepetition = currentList[index].repetition,
|
||||
repetitionOptions = repetitionContext.repetitionOptions,
|
||||
)
|
||||
|
||||
val updatedPreset = currentList[index].copy(
|
||||
pulseWidth = newPulseWidth,
|
||||
fluence = newFluence,
|
||||
fluence = repetitionContext.fluence,
|
||||
repetition = newRepetition
|
||||
)
|
||||
currentList[index] = updatedPreset
|
||||
@@ -354,17 +392,16 @@ class PresetViewModel @Inject constructor(
|
||||
}
|
||||
|
||||
// 3. update repetition (0)
|
||||
val hzType = hzTable.value.getValue(pulseWidth, newFluence)
|
||||
val list = RepetitionsByColorKey[hzType]!!
|
||||
setRepetitionList( list )
|
||||
|
||||
val newRepetitionIndex = 0
|
||||
val newRepetition = repetitionList.value[newRepetitionIndex]
|
||||
val repetitionContext = resolveRepetitionContext(pulseWidth, newFluence)
|
||||
val newRepetition = resolveNearestLowerOrEqualRepetition(
|
||||
currentRepetition = currentList[index].repetition,
|
||||
repetitionOptions = repetitionContext.repetitionOptions,
|
||||
)
|
||||
|
||||
// The original code here had a bug: it was updating `pulseWidth = newFluence`
|
||||
// It should be `fluence = newFluence`
|
||||
val updatedPreset = currentList[index].copy(
|
||||
fluence = newFluence,
|
||||
fluence = repetitionContext.fluence,
|
||||
repetition = newRepetition
|
||||
)
|
||||
currentList[index] = updatedPreset
|
||||
@@ -385,20 +422,28 @@ class PresetViewModel @Inject constructor(
|
||||
loadHzTable(handPieceType)
|
||||
|
||||
// update repetition
|
||||
val hzType = hzTable.value.getValue(pulseWidth, fluence)
|
||||
val list = RepetitionsByColorKey[hzType]!!
|
||||
setRepetitionList( list )
|
||||
val repetitionContext = resolveRepetitionContext(pulseWidth, fluence)
|
||||
val currentRepetitionList = repetitionContext.repetitionOptions
|
||||
|
||||
val newRepetitionIndex = repetitionList.value.indexOf(repetition).takeIf { it != -1 } ?: 0
|
||||
val newRepetitionIndex = currentRepetitionList.indexOf(repetition).takeIf { it != -1 } ?: 0
|
||||
val newRepetition = if (isUp == UpDownState.Up) {
|
||||
// Original logic: repetitionIndex < PulseDurations.size -1 (Likely a typo, should be Repetitions.size)
|
||||
if (newRepetitionIndex < repetitionList.value.size - 1) repetitionList.value[newRepetitionIndex + 1] else repetition
|
||||
if (newRepetitionIndex < currentRepetitionList.size - 1) {
|
||||
currentRepetitionList[newRepetitionIndex + 1]
|
||||
} else {
|
||||
repetition
|
||||
}
|
||||
} else {
|
||||
// Original logic: repetitionIndex > 1
|
||||
if (newRepetitionIndex > 0) repetitionList.value[newRepetitionIndex - 1] else repetition
|
||||
if (newRepetitionIndex > 0) {
|
||||
currentRepetitionList[newRepetitionIndex - 1]
|
||||
} else {
|
||||
repetition
|
||||
}
|
||||
}
|
||||
|
||||
val updatedPreset = currentList[index].copy(repetition = newRepetition)
|
||||
val updatedPreset = currentList[index].copy(
|
||||
fluence = repetitionContext.fluence,
|
||||
repetition = newRepetition
|
||||
)
|
||||
currentList[index] = updatedPreset
|
||||
//_presetList.value = currentList
|
||||
setPresetList(currentList)
|
||||
@@ -449,4 +494,4 @@ class PresetViewModel @Inject constructor(
|
||||
init {
|
||||
Timber.d("PresetViewModel init")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user