Meshtastic-Android/app/src/main/java/com/geeksville/mesh/util/DistanceExtensions.kt

92 lines
3.3 KiB
Kotlin
Raw Normal View History

/*
2025-01-02 06:50:26 -03:00
* Copyright (c) 2025 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Add elevation and number of GPS satellites to node info (#895) * Move battery info to compose - always show voltage level and icons to match battery percentage Use tool text in preview, rather than actually set text value Simplify node info layout to avoid defining margins on everything * Move node position to Compose * Update hyperlink color to match previous value * Use compose preview in layout editor * Use compose preview in layout editor * Add simple preview for use in layout * Move last heard node info to Compose Clean up layout of node info * Move signal info to Compose and simplify bind * Prevent long coordinates from colliding with signal info * Move the rest of the node info card to compose Breaks the blinking feature when navigating from chat Wrap position to new line if overflow * Adjust layout and text sizing to closer match original * Use constraint layout for tighter display on busy nodes * Construct environment metrics so that there aren't trailing spaces if current is zero * Swap viewholder root for compose view rather than inflating layout Fix padding lost when changing out view holder root Intelligently update the list with only nodes that changed * Remove unused method, and adjust replacement method to match the same decimal precisions as before * Add elevation and number of GPS satellites to node info list Add some extension functions for easier conversion between units and systems * Dispose composition on recycle to avoid lingering spacing from previous layouts Remove comments explaning adapter functionality Remove unused methods * Use previous string for denoting unknown node names * Align properly if altitude but no signal info
2024-03-07 02:34:43 -07:00
package com.geeksville.mesh.util
import android.icu.util.LocaleData
import android.icu.util.ULocale
import com.geeksville.mesh.ConfigProtos.Config.DisplayConfig
import java.util.Locale
Add elevation and number of GPS satellites to node info (#895) * Move battery info to compose - always show voltage level and icons to match battery percentage Use tool text in preview, rather than actually set text value Simplify node info layout to avoid defining margins on everything * Move node position to Compose * Update hyperlink color to match previous value * Use compose preview in layout editor * Use compose preview in layout editor * Add simple preview for use in layout * Move last heard node info to Compose Clean up layout of node info * Move signal info to Compose and simplify bind * Prevent long coordinates from colliding with signal info * Move the rest of the node info card to compose Breaks the blinking feature when navigating from chat Wrap position to new line if overflow * Adjust layout and text sizing to closer match original * Use constraint layout for tighter display on busy nodes * Construct environment metrics so that there aren't trailing spaces if current is zero * Swap viewholder root for compose view rather than inflating layout Fix padding lost when changing out view holder root Intelligently update the list with only nodes that changed * Remove unused method, and adjust replacement method to match the same decimal precisions as before * Add elevation and number of GPS satellites to node info list Add some extension functions for easier conversion between units and systems * Dispose composition on recycle to avoid lingering spacing from previous layouts Remove comments explaning adapter functionality Remove unused methods * Use previous string for denoting unknown node names * Align properly if altitude but no signal info
2024-03-07 02:34:43 -07:00
enum class DistanceUnit(
val symbol: String,
val multiplier: Float,
val system: Int
) {
METER("m", multiplier = 1F, DisplayConfig.DisplayUnits.METRIC_VALUE),
KILOMETER("km", multiplier = 0.001F, DisplayConfig.DisplayUnits.METRIC_VALUE),
FOOT("ft", multiplier = 3.28084F, DisplayConfig.DisplayUnits.IMPERIAL_VALUE),
MILE("mi", multiplier = 0.000621371F, DisplayConfig.DisplayUnits.IMPERIAL_VALUE),
;
companion object {
fun getFromLocale(locale: Locale = Locale.getDefault()): DisplayConfig.DisplayUnits {
return if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
when (LocaleData.getMeasurementSystem(ULocale.forLocale(locale))) {
LocaleData.MeasurementSystem.SI -> DisplayConfig.DisplayUnits.METRIC
else -> DisplayConfig.DisplayUnits.IMPERIAL
}
} else {
when (locale.country.uppercase(locale)) {
"US", "LR", "MM", "GB" -> DisplayConfig.DisplayUnits.IMPERIAL
else -> DisplayConfig.DisplayUnits.METRIC
}
}
}
}
Add elevation and number of GPS satellites to node info (#895) * Move battery info to compose - always show voltage level and icons to match battery percentage Use tool text in preview, rather than actually set text value Simplify node info layout to avoid defining margins on everything * Move node position to Compose * Update hyperlink color to match previous value * Use compose preview in layout editor * Use compose preview in layout editor * Add simple preview for use in layout * Move last heard node info to Compose Clean up layout of node info * Move signal info to Compose and simplify bind * Prevent long coordinates from colliding with signal info * Move the rest of the node info card to compose Breaks the blinking feature when navigating from chat Wrap position to new line if overflow * Adjust layout and text sizing to closer match original * Use constraint layout for tighter display on busy nodes * Construct environment metrics so that there aren't trailing spaces if current is zero * Swap viewholder root for compose view rather than inflating layout Fix padding lost when changing out view holder root Intelligently update the list with only nodes that changed * Remove unused method, and adjust replacement method to match the same decimal precisions as before * Add elevation and number of GPS satellites to node info list Add some extension functions for easier conversion between units and systems * Dispose composition on recycle to avoid lingering spacing from previous layouts Remove comments explaning adapter functionality Remove unused methods * Use previous string for denoting unknown node names * Align properly if altitude but no signal info
2024-03-07 02:34:43 -07:00
}
fun Int.metersIn(unit: DistanceUnit): Float {
return this * unit.multiplier
}
fun Int.metersIn(system: DisplayConfig.DisplayUnits): Float {
val unit = when (system.number) {
DisplayConfig.DisplayUnits.IMPERIAL_VALUE -> DistanceUnit.FOOT
else -> DistanceUnit.METER
Add elevation and number of GPS satellites to node info (#895) * Move battery info to compose - always show voltage level and icons to match battery percentage Use tool text in preview, rather than actually set text value Simplify node info layout to avoid defining margins on everything * Move node position to Compose * Update hyperlink color to match previous value * Use compose preview in layout editor * Use compose preview in layout editor * Add simple preview for use in layout * Move last heard node info to Compose Clean up layout of node info * Move signal info to Compose and simplify bind * Prevent long coordinates from colliding with signal info * Move the rest of the node info card to compose Breaks the blinking feature when navigating from chat Wrap position to new line if overflow * Adjust layout and text sizing to closer match original * Use constraint layout for tighter display on busy nodes * Construct environment metrics so that there aren't trailing spaces if current is zero * Swap viewholder root for compose view rather than inflating layout Fix padding lost when changing out view holder root Intelligently update the list with only nodes that changed * Remove unused method, and adjust replacement method to match the same decimal precisions as before * Add elevation and number of GPS satellites to node info list Add some extension functions for easier conversion between units and systems * Dispose composition on recycle to avoid lingering spacing from previous layouts Remove comments explaning adapter functionality Remove unused methods * Use previous string for denoting unknown node names * Align properly if altitude but no signal info
2024-03-07 02:34:43 -07:00
}
return this.metersIn(unit)
Add elevation and number of GPS satellites to node info (#895) * Move battery info to compose - always show voltage level and icons to match battery percentage Use tool text in preview, rather than actually set text value Simplify node info layout to avoid defining margins on everything * Move node position to Compose * Update hyperlink color to match previous value * Use compose preview in layout editor * Use compose preview in layout editor * Add simple preview for use in layout * Move last heard node info to Compose Clean up layout of node info * Move signal info to Compose and simplify bind * Prevent long coordinates from colliding with signal info * Move the rest of the node info card to compose Breaks the blinking feature when navigating from chat Wrap position to new line if overflow * Adjust layout and text sizing to closer match original * Use constraint layout for tighter display on busy nodes * Construct environment metrics so that there aren't trailing spaces if current is zero * Swap viewholder root for compose view rather than inflating layout Fix padding lost when changing out view holder root Intelligently update the list with only nodes that changed * Remove unused method, and adjust replacement method to match the same decimal precisions as before * Add elevation and number of GPS satellites to node info list Add some extension functions for easier conversion between units and systems * Dispose composition on recycle to avoid lingering spacing from previous layouts Remove comments explaning adapter functionality Remove unused methods * Use previous string for denoting unknown node names * Align properly if altitude but no signal info
2024-03-07 02:34:43 -07:00
}
fun Float.toString(unit: DistanceUnit): String {
return if (unit in setOf(DistanceUnit.METER, DistanceUnit.FOOT)) {
"%.0f %s"
} else {
"%.1f %s"
}.format(this, unit.symbol)
Add elevation and number of GPS satellites to node info (#895) * Move battery info to compose - always show voltage level and icons to match battery percentage Use tool text in preview, rather than actually set text value Simplify node info layout to avoid defining margins on everything * Move node position to Compose * Update hyperlink color to match previous value * Use compose preview in layout editor * Use compose preview in layout editor * Add simple preview for use in layout * Move last heard node info to Compose Clean up layout of node info * Move signal info to Compose and simplify bind * Prevent long coordinates from colliding with signal info * Move the rest of the node info card to compose Breaks the blinking feature when navigating from chat Wrap position to new line if overflow * Adjust layout and text sizing to closer match original * Use constraint layout for tighter display on busy nodes * Construct environment metrics so that there aren't trailing spaces if current is zero * Swap viewholder root for compose view rather than inflating layout Fix padding lost when changing out view holder root Intelligently update the list with only nodes that changed * Remove unused method, and adjust replacement method to match the same decimal precisions as before * Add elevation and number of GPS satellites to node info list Add some extension functions for easier conversion between units and systems * Dispose composition on recycle to avoid lingering spacing from previous layouts Remove comments explaning adapter functionality Remove unused methods * Use previous string for denoting unknown node names * Align properly if altitude but no signal info
2024-03-07 02:34:43 -07:00
}
fun Float.toString(system: DisplayConfig.DisplayUnits): String {
val unit = when (system.number) {
DisplayConfig.DisplayUnits.IMPERIAL_VALUE -> DistanceUnit.FOOT
else -> DistanceUnit.METER
}
return this.toString(unit)
}
private const val KILOMETER_THRESHOLD = 1000
private const val MILE_THRESHOLD = 1609
fun Int.toDistanceString(system: DisplayConfig.DisplayUnits): String {
val unit = if (system.number == DisplayConfig.DisplayUnits.METRIC_VALUE) {
if (this < KILOMETER_THRESHOLD) DistanceUnit.METER else DistanceUnit.KILOMETER
} else {
if (this < MILE_THRESHOLD) DistanceUnit.FOOT else DistanceUnit.MILE
}
val valueInUnit = this * unit.multiplier
return valueInUnit.toString(unit)
}