@@ -19,7 +19,10 @@ import org.neotech.app.abysner.domain.core.model.BreathingMode
1919import org.neotech.app.abysner.domain.core.model.Configuration
2020import org.neotech.app.abysner.domain.core.model.DiveMode
2121import org.neotech.app.abysner.domain.core.model.SetpointSwitch
22+ import org.neotech.app.abysner.domain.core.model.UnitSystem
23+ import org.neotech.app.abysner.domain.core.physics.ambientPressureToFeet
2224import org.neotech.app.abysner.domain.core.physics.ambientPressureToMeters
25+ import org.neotech.app.abysner.domain.core.physics.feetToHydrostaticPressure
2326import org.neotech.app.abysner.domain.core.physics.metersToAmbientPressure
2427import org.neotech.app.abysner.domain.core.physics.metersToHydrostaticPressure
2528import org.neotech.app.abysner.domain.decompression.DecoGrid
@@ -39,10 +42,11 @@ import kotlin.time.Duration
3942 * adds multi-level dive handling.
4043 */
4144class DivePlanner (
42- configuration : Configuration = Configuration ()
45+ configuration : Configuration = Configuration (),
46+ private val unitSystem : UnitSystem = UnitSystem .METRIC ,
4347) {
4448
45- var configuration: Configuration = configuration
49+ var configuration: Configuration = configuration.snapToDisplayUnit(unitSystem)
4650 private set
4751
4852 private var model: DecompressionModel = createDecompressionModel()
@@ -53,7 +57,7 @@ class DivePlanner(
5357 */
5458 fun updateConfiguration (newConfiguration : Configuration ) {
5559 val snapshot = model.snapshot()
56- configuration = newConfiguration
60+ configuration = newConfiguration.snapToDisplayUnit(unitSystem)
5761 model = createDecompressionModel()
5862 model.reset(snapshot)
5963 }
@@ -69,6 +73,7 @@ class DivePlanner(
6973 cylinders : List <AssignedCylinder >,
7074 diveMode : DiveMode = DiveMode .OPEN_CIRCUIT ,
7175 bailout : Boolean = false,
76+ unitSystem : UnitSystem = UnitSystem .METRIC ,
7277 ): DivePlan {
7378
7479 require(! bailout || diveMode.isCcr) {
@@ -99,7 +104,7 @@ class DivePlanner(
99104 )
100105 }
101106
102- val decompressionPlanner = createDecompressionPlanner(model, configuration)
107+ val decompressionPlanner = createDecompressionPlanner(model, configuration, unitSystem )
103108
104109 decompressionPlanner.setDecoGases(decoCylinders)
105110
@@ -255,14 +260,24 @@ class DivePlanner(
255260 private fun createDecompressionPlanner (
256261 model : DecompressionModel ,
257262 configuration : Configuration ,
263+ unitSystem : UnitSystem = UnitSystem .METRIC ,
258264 ): DecompressionPlanner {
259265 val environment = configuration.environment
260266 val grid = DecoGrid (
261267 surfacePressure = environment.atmosphericPressure,
262268 decoStepSizePressureDelta = metersToHydrostaticPressure(configuration.decoStepSize, environment).value,
263269 lastDecoStopAmbientPressure = metersToAmbientPressure(configuration.lastDecoStopDepth, environment).value,
264- displayUnitPressureDelta = metersToHydrostaticPressure(1.0 , environment).value,
270+ displayUnitPressureDelta = when (unitSystem) {
271+ UnitSystem .METRIC -> metersToHydrostaticPressure(1.0 , environment).value
272+ UnitSystem .IMPERIAL -> feetToHydrostaticPressure(1.0 , environment).value
273+ },
265274 )
275+ // removeFloatingPointNoise makes test assertions easier to read since depths are
276+ // usually whole numbers without requiring tolerance handling at every call site.
277+ val pressureToDepth: (Double ) -> Double = when (unitSystem) {
278+ UnitSystem .METRIC -> { pressure -> removeFloatingPointNoise(ambientPressureToMeters(pressure, environment)) }
279+ UnitSystem .IMPERIAL -> { pressure -> removeFloatingPointNoise(ambientPressureToFeet(pressure, environment)) }
280+ }
266281 return DecompressionPlanner (
267282 model = model,
268283 grid = grid,
@@ -271,14 +286,7 @@ class DivePlanner(
271286 ascentRatePressureDelta = metersToHydrostaticPressure(configuration.maxAscentRate, environment).value,
272287 forceMinimalDecoStopTime = configuration.forceMinimalDecoStopTime,
273288 gasSwitchTime = configuration.gasSwitchTime,
274- pressureToDepth = { pressure ->
275- // This is not strictly required to make the UI work, since the UI already formats
276- // to zero decimals or 1 maybe 2 decimals at most. However, it makes the current
277- // test assertions easier to read, since these are usually in whole meters, without
278- // this noise normalization tolerance handling at assertion call sites would be
279- // required, or more precise less readable floating point numbers.
280- removeFloatingPointNoise(ambientPressureToMeters(pressure, environment))
281- },
289+ pressureToDepth = pressureToDepth,
282290 )
283291 }
284292
@@ -314,5 +322,3 @@ class DivePlanner(
314322
315323 class NotEnoughTimeToDecompress : PlanningException ()
316324}
317-
318-
0 commit comments