mirror of
https://github.com/GLEGram/GLEGram-iOS.git
synced 2026-04-25 12:26:55 +02:00
4647310322
Based on Swiftgram 12.5 (Telegram iOS 12.5). All GLEGram features ported and organized in GLEGram/ folder. Features: Ghost Mode, Saved Deleted Messages, Content Protection Bypass, Font Replacement, Fake Profile, Chat Export, Plugin System, and more. See CHANGELOG_12.5.md for full details.
48 lines
1.5 KiB
Swift
48 lines
1.5 KiB
Swift
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
|
|
}
|
|
|
|
private var formatter: MeasurementFormatter = {
|
|
let formatter = MeasurementFormatter()
|
|
formatter.locale = Locale.current
|
|
formatter.unitStyle = .short
|
|
formatter.numberFormatter.maximumFractionDigits = 0
|
|
return formatter
|
|
}()
|
|
|
|
public func stringForTemperature(_ value: Double) -> String {
|
|
let valueString = formatter.string(from: Measurement(value: value, unit: UnitTemperature.celsius)).trimmingCharacters(in: CharacterSet(charactersIn: "0123456789-,.").inverted)
|
|
return valueString + currentTemperatureUnit().suffix
|
|
}
|