mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-24 07:05:35 +00:00
Various improvements
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import Foundation
|
||||
|
||||
private enum TemperatureUnit {
|
||||
case celsius
|
||||
case fahrenheit
|
||||
|
||||
var suffix: String {
|
||||
switch self {
|
||||
case .celsius:
|
||||
return "°C"
|
||||
case .fahrenheit:
|
||||
return "°F"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var cachedTemperatureUnit: TemperatureUnit?
|
||||
private func currentTemperatureUnit() -> TemperatureUnit {
|
||||
if let cachedTemperatureUnit {
|
||||
return cachedTemperatureUnit
|
||||
}
|
||||
let temperatureFormatter = MeasurementFormatter()
|
||||
temperatureFormatter.locale = Locale.current
|
||||
|
||||
let fahrenheitMeasurement = Measurement(value: 0, unit: UnitTemperature.fahrenheit)
|
||||
let fahrenheitString = temperatureFormatter.string(from: fahrenheitMeasurement)
|
||||
|
||||
var temperatureUnit: TemperatureUnit = .celsius
|
||||
if fahrenheitString.contains("F") || fahrenheitString.contains("Fahrenheit") {
|
||||
temperatureUnit = .fahrenheit
|
||||
}
|
||||
cachedTemperatureUnit = temperatureUnit
|
||||
return temperatureUnit
|
||||
}
|
||||
|
||||
public func stringForTemperature(_ value: Double) -> String {
|
||||
let formatter = MeasurementFormatter()
|
||||
formatter.locale = Locale.current
|
||||
formatter.unitStyle = .short
|
||||
formatter.numberFormatter.maximumFractionDigits = 0
|
||||
formatter.unitOptions = .temperatureWithoutUnit
|
||||
let valueString = formatter.string(from: Measurement(value: value, unit: UnitTemperature.celsius)).trimmingCharacters(in: CharacterSet(charactersIn: "0123456789-,.").inverted)
|
||||
return valueString + currentTemperatureUnit().suffix
|
||||
}
|
||||
Reference in New Issue
Block a user