diff --git a/app/src/main/java/com/laseroptek/raman/ui/screens/main/MainViewModel.kt b/app/src/main/java/com/laseroptek/raman/ui/screens/main/MainViewModel.kt index 46def97..5b97382 100644 --- a/app/src/main/java/com/laseroptek/raman/ui/screens/main/MainViewModel.kt +++ b/app/src/main/java/com/laseroptek/raman/ui/screens/main/MainViewModel.kt @@ -134,6 +134,7 @@ import kotlinx.coroutines.joinAll import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import timber.log.Timber +import kotlin.math.abs import javax.inject.Inject import kotlin.experimental.or import kotlin.time.Duration.Companion.milliseconds @@ -2132,8 +2133,15 @@ class MainViewModel @Inject constructor( setFluenceList(newFluenceList) } + // 2-1. Resolve and apply fluence by value (not by old angle/index). + val resolvedFluence = newFluenceList.minByOrNull { abs(it - newFluence) } ?: 0f + val resolvedFluenceIndex = newFluenceList.indexOf(resolvedFluence).takeIf { it >= 0 } ?: 0 + if (newFluenceList.isNotEmpty()) { + setFluenceAngle(resolvedFluenceIndex.stepToDegree(totalSteps = newFluenceList.size)) + } + // 3. Safely Update Repetition List (Prevents NullPointerException) - val newHzType = hzTable.value.getValue(newPulseDuration, newFluence) + val newHzType = hzTable.value.getValue(newPulseDuration, resolvedFluence) val newRepetitionList = RepetitionsByColorKey[newHzType] ?: RepetitionsByColorKey[KEY_YELLOW]!! if (newRepetitionList != repetitionList.value) { setRepetitionList(newRepetitionList) @@ -2168,7 +2176,7 @@ class MainViewModel @Inject constructor( // 6. Any change invalidates the current preset selection. setSelectedPresetIndex(0) - Timber.d("Updated Laser Parameters: pulse=$newPulseDuration, fluence=$newFluence -> newRepListSize=${newRepetitionList.size}") + Timber.d("Updated Laser Parameters: pulse=$newPulseDuration, fluence=$resolvedFluence -> newRepListSize=${newRepetitionList.size}") } /** @@ -2188,13 +2196,16 @@ class MainViewModel @Inject constructor( setPulseAngle(newPulseStep.stepToDegree(totalSteps = PulseDurations.size)) val newPulseDuration = PulseDurations[newPulseStep] - // When pulse duration changes via slider, we use the first available fluence for the new list. - val firstFluence = energyTable.value.getKey2ListForKey1(newPulseDuration).firstOrNull() ?: 0f + // Keep the current fluence value when pulse duration changes via slider. + val currentFluenceStep = fluenceAngle.value.degreeToStep(totalSteps = fluenceList.value.size) + val currentFluence = fluenceList.value.getOrNull(currentFluenceStep) + ?: energyTable.value.getKey2ListForKey1(newPulseDuration).firstOrNull() + ?: 0f - // Call the centralized helper, resetting the fluence slider. + // Call the centralized helper using the preserved fluence value. updateLaserParameters( newPulseDuration = newPulseDuration, - newFluence = firstFluence, + newFluence = currentFluence, ) } @@ -2206,12 +2217,17 @@ class MainViewModel @Inject constructor( setPulseAngle(newStep.stepToDegree(totalSteps = PulseDurations.size)) val newPulseDuration = PulseDurations[newStep] - val firstFluence = energyTable.value.getKey2ListForKey1(newPulseDuration).firstOrNull() ?: 0f + // Keep the current fluence value when pulse duration changes via slider. + val currentFluenceStep = fluenceAngle.value.degreeToStep(totalSteps = fluenceList.value.size) + val currentFluence = fluenceList.value.getOrNull(currentFluenceStep) + ?: energyTable.value.getKey2ListForKey1(newPulseDuration).firstOrNull() + ?: 0f + // Call the centralized helper, resetting the fluence slider. updateLaserParameters( newPulseDuration = newPulseDuration, - newFluence = firstFluence, + newFluence = currentFluence, ) } }