diff --git a/app/src/main/java/eu/wa5p/lightmeter/MainActivity.kt b/app/src/main/java/eu/wa5p/lightmeter/MainActivity.kt index dd39bab..99ba005 100644 --- a/app/src/main/java/eu/wa5p/lightmeter/MainActivity.kt +++ b/app/src/main/java/eu/wa5p/lightmeter/MainActivity.kt @@ -27,17 +27,20 @@ import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import eu.wa5p.lightmeter.ui.theme.LightMeterTheme -import kotlin.math.roundToInt +import kotlin.math.abs const val TAG: String = "lightmeter MainActivity" +private val commonIsoValues = listOf(50, 100, 200, 400, 800, 1600, 3200, 6400) +private val commonApertureValues = listOf(0.85, 1.4, 2.0, 2.8, 4.0, 5.6, 8.0, 11.0, 16.0, 22.0) + class MainActivity : ComponentActivity(), SensorEventListener { private lateinit var sensorManager: SensorManager private var illuminanceSensor: Sensor? = null private var lightmeterState = mutableStateOf( LightmeterState( illuminance = 0.0, - aperture = 1.8, + aperture = 2.8, iso = 100, shutterSpeed = 0.01 ) @@ -150,6 +153,14 @@ fun LightmeterListItemPreview() { @Composable fun IsoSlider(state: MutableState, recalculateShutterSpeed: () -> Unit) { + // Convert common ISO values to the range used by the slider + val minIso = commonIsoValues.first().toFloat() + val maxIso = commonIsoValues.last().toFloat() + + // Find the closest ISO value to the slider's value + val isoValue = state.value.iso.toFloat() + val closestIso = commonIsoValues.minByOrNull { abs(it - isoValue) } ?: minIso + Row( modifier = Modifier.padding(all = 8.dp), verticalAlignment = Alignment.CenterVertically @@ -160,16 +171,17 @@ fun IsoSlider(state: MutableState, recalculateShutterSpeed: () ) Slider( modifier = Modifier.weight(0.7f), - value = state.value.iso.toFloat(), + value = closestIso.toFloat(), onValueChange = { value -> - state.value = state.value.copy(iso = value.roundToInt()) + val newIso = commonIsoValues.minByOrNull { abs(it - value) } ?: minIso + state.value = state.value.copy(iso = newIso.toInt()) recalculateShutterSpeed() }, - valueRange = 1f..2000f + valueRange = minIso..maxIso, + steps = (commonIsoValues.size - 1) * 10 ) } } - val previewlightmeterState = mutableStateOf( LightmeterState( illuminance = 0.0, @@ -197,6 +209,14 @@ fun ApertureSlider( state: MutableState, recalculateShutterSpeed: () -> Unit ) { + // Convert common aperture values to the range used by the slider + val minAperture = commonApertureValues.first().toFloat() + val maxAperture = commonApertureValues.last().toFloat() + + // Find the closest aperture value to the slider's value + val apertureValue = state.value.aperture.toFloat() + val closestAperture = commonApertureValues.minByOrNull { abs(it - apertureValue) } ?: minAperture + Row( modifier = Modifier.padding(all = 8.dp), verticalAlignment = Alignment.CenterVertically @@ -207,16 +227,17 @@ fun ApertureSlider( ) Slider( modifier = Modifier.weight(0.7f), - value = state.value.aperture.toFloat(), + value = closestAperture.toFloat(), onValueChange = { value -> - state.value = state.value.copy(aperture = value.toDouble()) + val newAperture = commonApertureValues.minByOrNull { abs(it - value) } ?: minAperture + state.value = state.value.copy(aperture = newAperture.toDouble()) recalculateShutterSpeed() }, - valueRange = 0.85f..22f + valueRange = minAperture..maxAperture, + steps = (commonApertureValues.size - 1) * 10 ) } } - @Preview(showBackground = true) @Composable fun ApertureSliderPreview() {