-
Notifications
You must be signed in to change notification settings - Fork 0
/
Units.kt
153 lines (127 loc) · 4.54 KB
/
Units.kt
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.example.mountainmarkers.data.utils
import android.content.res.Resources
import androidx.annotation.StringRes
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.ui.res.stringResource
import com.example.mountainmarkers.R
const val METERS_PER_FOOT = 3.28084
const val METERS_PER_KILOMETER = 1_000
const val FEET_PER_METER = 1 / METERS_PER_FOOT
const val FEET_PER_MILE = 5_280
const val MILES_PER_METER = 0.000621371
/**
* A value class to wrap a value representing a measurement in meters.
*/
@Immutable
@JvmInline
value class Meters(val value: Double) : Comparable<Meters> {
override fun compareTo(other: Meters) = value.compareTo(other.value)
operator fun minus(other: Meters) = Meters(value = this.value - other.value)
}
/**
* Create a Meters class from a [Number]
*/
@Stable
inline val Number.meters: Meters get() = Meters(value = this.toDouble())
/**
* Create a Meters class from a [Number]
*/
@Stable
inline val Number.m: Meters get() = Meters(value = this.toDouble())
/**
* Create a Meters class from a [Number] of kilometers
*/
@Stable
inline val Number.km: Meters get() = Meters(value = this.toDouble() * METERS_PER_KILOMETER)
/**
* Create a Meters class from a [Number] of feet
*/
@Stable
inline val Number.feet: Meters get() = Meters(value = this.toDouble() * FEET_PER_METER)
/**
* Create a Meters class from a [Number] of miles
*/
@Stable
inline val Number.miles: Meters get() = Meters(value = this.toDouble() / MILES_PER_METER)
/**
* Gets the number of equivalent feet from a meters value class
*/
@Stable
inline val Meters.toFeet: Double get() = value * METERS_PER_FOOT
/**
* Gets the value of a meters class as a Double
*/
@Stable
inline val Meters.toMeters: Double get() = value
/**
* Gets the number of equivalent kilometers from a meters value class
*/
@Stable
inline val Meters.toKilometers: Double get() = value / METERS_PER_KILOMETER
/**
* Gets the number of equivalent kilometers from a meters value class
*/
@Stable
inline val Meters.toMiles: Double get() = (value * MILES_PER_METER)
data class ValueWithUnitsTemplate(
val value: Double,
@StringRes val unitsTemplate: Int
)
abstract class UnitsConverter {
abstract fun toElevationUnits(meters: Meters): ValueWithUnitsTemplate
abstract fun toDistanceUnits(meters: Meters): ValueWithUnitsTemplate
@Composable
fun toElevationString(meters: Meters): String {
val (value, resourceId) = toElevationUnits(meters = meters)
return stringResource(id = resourceId, value)
}
fun toElevationString(resources: Resources, meters: Meters): String {
val (value, resourceId) = toElevationUnits(meters = meters)
return resources.getString(resourceId, value)
}
@Composable
fun toDistanceString(meters: Meters): String {
val (value, resourceId) = toDistanceUnits(meters = meters)
return stringResource(id = resourceId, value)
}
fun toDistanceString(resources: Resources, meters: Meters): String {
val (value, resourceId) = toDistanceUnits(meters = meters)
return resources.getString(resourceId, value)
}
}
object ImperialUnitsConverter : UnitsConverter() {
override fun toElevationUnits(meters: Meters) =
ValueWithUnitsTemplate(meters.toFeet, R.string.in_feet)
override fun toDistanceUnits(meters: Meters) =
ValueWithUnitsTemplate(meters.toMiles, R.string.in_miles)
}
object MetricUnitsConverter : UnitsConverter() {
override fun toElevationUnits(meters: Meters) =
ValueWithUnitsTemplate(meters.toMeters, R.string.in_meters)
override fun toDistanceUnits(meters: Meters) =
ValueWithUnitsTemplate(meters.toKilometers, R.string.in_kilometers)
}
val LocalUnitsConverter = compositionLocalOf<UnitsConverter> { ImperialUnitsConverter }
/**
* Creates a string to show the elevation formatted with units
*/
@Composable
fun Meters.toElevationString(): String {
return LocalUnitsConverter.current.toElevationString(this)
}