summaryrefslogtreecommitdiff
path: root/android-app/app/src/main/kotlin/org/terst/nav/UnitPrefs.kt
blob: 20de3e416980e91c415d141e2e2063e3fe1acbbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package org.terst.nav

import android.content.Context
import java.util.Locale

enum class TempUnit     { CELSIUS, FAHRENHEIT }
enum class DepthUnit    { FEET, METERS }
enum class PressureUnit { HPA, INHG }
enum class SpeedUnit    { KNOTS, MPH, KPH }
enum class DistanceUnit { NM, KM, MI }

/**
 * Persists and applies the user's preferred display units.
 * Defaults: Fahrenheit, feet, hPa, knots.
 */
class UnitPrefs(context: Context) {

    private val prefs = context.getSharedPreferences("unit_prefs", Context.MODE_PRIVATE)

    var tempUnit: TempUnit
        get() = runCatching { TempUnit.valueOf(prefs.getString(KEY_TEMP, null) ?: "") }.getOrDefault(TempUnit.FAHRENHEIT)
        set(v) { prefs.edit().putString(KEY_TEMP, v.name).apply() }

    var depthUnit: DepthUnit
        get() = runCatching { DepthUnit.valueOf(prefs.getString(KEY_DEPTH, null) ?: "") }.getOrDefault(DepthUnit.FEET)
        set(v) { prefs.edit().putString(KEY_DEPTH, v.name).apply() }

    var pressureUnit: PressureUnit
        get() = runCatching { PressureUnit.valueOf(prefs.getString(KEY_PRESSURE, null) ?: "") }.getOrDefault(PressureUnit.HPA)
        set(v) { prefs.edit().putString(KEY_PRESSURE, v.name).apply() }

    var speedUnit: SpeedUnit
        get() = runCatching { SpeedUnit.valueOf(prefs.getString(KEY_SPEED, null) ?: "") }.getOrDefault(SpeedUnit.KNOTS)
        set(v) { prefs.edit().putString(KEY_SPEED, v.name).apply() }

    var distanceUnit: DistanceUnit
        get() = runCatching { DistanceUnit.valueOf(prefs.getString(KEY_DISTANCE, null) ?: "") }.getOrDefault(DistanceUnit.NM)
        set(v) { prefs.edit().putString(KEY_DISTANCE, v.name).apply() }

    fun formatTemp(tempC: Double): String = when (tempUnit) {
        TempUnit.CELSIUS    -> "%.0f".format(Locale.getDefault(), tempC)
        TempUnit.FAHRENHEIT -> "%.0f".format(Locale.getDefault(), tempC * 9.0 / 5.0 + 32.0)
    }

    fun tempUnitLabel(): String = when (tempUnit) {
        TempUnit.CELSIUS    -> "°C"
        TempUnit.FAHRENHEIT -> "°F"
    }

    fun formatDepth(metres: Double): String = when (depthUnit) {
        DepthUnit.FEET   -> "%.1f".format(Locale.getDefault(), metres * 3.28084)
        DepthUnit.METERS -> "%.1f".format(Locale.getDefault(), metres)
    }

    fun depthUnitLabel(): String = when (depthUnit) {
        DepthUnit.FEET   -> "ft"
        DepthUnit.METERS -> "m"
    }

    fun formatPressure(hpa: Float): String = when (pressureUnit) {
        PressureUnit.HPA  -> "%.1f".format(Locale.getDefault(), hpa)
        PressureUnit.INHG -> "%.2f".format(Locale.getDefault(), hpa * 0.02953f)
    }

    fun pressureUnitLabel(): String = when (pressureUnit) {
        PressureUnit.HPA  -> "hPa"
        PressureUnit.INHG -> "inHg"
    }

    fun formatSpeed(knots: Double): String = when (speedUnit) {
        SpeedUnit.KNOTS -> "%.1f".format(Locale.getDefault(), knots)
        SpeedUnit.MPH   -> "%.1f".format(Locale.getDefault(), knots * 1.15078)
        SpeedUnit.KPH   -> "%.1f".format(Locale.getDefault(), knots * 1.852)
    }

    fun speedUnitLabel(): String = when (speedUnit) {
        SpeedUnit.KNOTS -> "kt"
        SpeedUnit.MPH   -> "mph"
        SpeedUnit.KPH   -> "kph"
    }

    fun formatDistance(nm: Double): String = when (distanceUnit) {
        DistanceUnit.NM -> "%.1f".format(Locale.getDefault(), nm)
        DistanceUnit.KM -> "%.1f".format(Locale.getDefault(), nm * 1.852)
        DistanceUnit.MI -> "%.1f".format(Locale.getDefault(), nm * 1.15078)
    }

    fun distanceUnitLabel(): String = when (distanceUnit) {
        DistanceUnit.NM -> "nm"
        DistanceUnit.KM -> "km"
        DistanceUnit.MI -> "mi"
    }

    companion object {
        private const val KEY_TEMP     = "temp_unit"
        private const val KEY_DEPTH    = "depth_unit"
        private const val KEY_PRESSURE = "pressure_unit"
        private const val KEY_SPEED    = "speed_unit"
        private const val KEY_DISTANCE = "distance_unit"
    }
}