QuantityFormatter.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. //
  2. // QuantityFormatter.swift
  3. // LoopKit
  4. //
  5. // Copyright © 2018 LoopKit Authors. All rights reserved.
  6. //
  7. import Foundation
  8. import HealthKit
  9. /// Formats unit quantities as localized strings
  10. open class QuantityFormatter {
  11. public init() {
  12. }
  13. public convenience init(for unit: HKUnit) {
  14. self.init()
  15. setPreferredNumberFormatter(for: unit)
  16. }
  17. /// The unit style determines how the unit strings are abbreviated, and spacing between the value and unit
  18. open var unitStyle: Formatter.UnitStyle = .medium {
  19. didSet {
  20. if hasMeasurementFormatter {
  21. measurementFormatter.unitStyle = unitStyle
  22. }
  23. if hasMassFormatter {
  24. massFormatter.unitStyle = unitStyle
  25. }
  26. }
  27. }
  28. open var locale: Locale = Locale.current {
  29. didSet {
  30. if hasNumberFormatter {
  31. numberFormatter.locale = locale
  32. }
  33. if hasMeasurementFormatter {
  34. measurementFormatter.locale = locale
  35. }
  36. }
  37. }
  38. /// Updates `numberFormatter` configuration for the specified unit
  39. ///
  40. /// - Parameter unit: The unit
  41. open func setPreferredNumberFormatter(for unit: HKUnit) {
  42. numberFormatter.numberStyle = .decimal
  43. numberFormatter.minimumFractionDigits = unit.preferredFractionDigits
  44. numberFormatter.maximumFractionDigits = unit.maxFractionDigits
  45. }
  46. private var hasNumberFormatter = false
  47. /// The formatter used for the quantity values
  48. open private(set) lazy var numberFormatter: NumberFormatter = {
  49. hasNumberFormatter = true
  50. let formatter = NumberFormatter()
  51. formatter.numberStyle = .decimal
  52. formatter.locale = self.locale
  53. return formatter
  54. }()
  55. private var hasMeasurementFormatter = false
  56. /// MeasurementFormatter is used for gram measurements, mg/dL units, and mmol/L units.
  57. /// It does not properly handle glucose measurements, as it changes unit scales: 100 mg/dL -> 1 g/L
  58. private lazy var measurementFormatter: MeasurementFormatter = {
  59. hasMeasurementFormatter = true
  60. let formatter = MeasurementFormatter()
  61. formatter.unitOptions = [.providedUnit]
  62. formatter.numberFormatter = self.numberFormatter
  63. formatter.locale = self.locale
  64. formatter.unitStyle = self.unitStyle
  65. return formatter
  66. }()
  67. private var hasMassFormatter = false
  68. /// MassFormatter properly creates unit strings for grams in .short/.medium style as "g", where MeasurementFormatter uses "gram"/"grams"
  69. private lazy var massFormatter: MassFormatter = {
  70. hasMassFormatter = true
  71. let formatter = MassFormatter()
  72. formatter.numberFormatter = self.numberFormatter
  73. formatter.unitStyle = self.unitStyle
  74. return formatter
  75. }()
  76. /// When `avoidLineBreaking` is true, the formatter avoids unit strings or values and their unit strings being split by a line break.
  77. open var avoidLineBreaking: Bool = true
  78. /// Formats a quantity and unit as a localized string
  79. ///
  80. /// - Parameters:
  81. /// - quantity: The quantity
  82. /// - unit: The unit. An exception is thrown if `quantity` is not compatible with the unit.
  83. /// - includeUnit: Whether or not to include the unit in the returned string
  84. /// - Returns: A localized string, or nil if `numberFormatter` is unable to format the quantity value
  85. open func string(from quantity: HKQuantity, for unit: HKUnit, includeUnit: Bool = true) -> String? {
  86. let value = quantity.doubleValue(for: unit)
  87. if !includeUnit {
  88. return numberFormatter.string(from: value)
  89. }
  90. if let foundationUnit = unit.foundationUnit, unit.usesMeasurementFormatterForMeasurement {
  91. return measurementFormatter.string(from: Measurement(value: value, unit: foundationUnit)).avoidLineBreaking(enabled: avoidLineBreaking)
  92. }
  93. // Pass 'false' for `avoidLineBreaking` because we don't want to do it twice.
  94. return numberFormatter.string(from: value, unit: string(from: unit, forValue: value, avoidLineBreaking: false),
  95. style: unitStyle, avoidLineBreaking: avoidLineBreaking)
  96. }
  97. /// Formats a unit as a localized string
  98. ///
  99. /// - Parameters:
  100. /// - unit: The unit
  101. /// - value: An optional value for determining the plurality of the unit string
  102. /// - Returns: A string for the unit. If no localization entry is available, the unlocalized `unitString` is returned.
  103. open func string(from unit: HKUnit, forValue value: Double = 10, avoidLineBreaking: Bool? = nil) -> String {
  104. let avoidLineBreaking = avoidLineBreaking ?? self.avoidLineBreaking
  105. if let string = unit.localizedUnitString(in: unitStyle, singular: abs(1.0 - value) < .ulpOfOne, avoidLineBreaking: avoidLineBreaking) {
  106. return string
  107. }
  108. let string: String
  109. if unit.usesMassFormatterForUnitString {
  110. string = massFormatter.unitString(fromValue: value, unit: HKUnit.massFormatterUnit(from: unit))
  111. } else if let foundationUnit = unit.foundationUnit {
  112. string = measurementFormatter.string(from: foundationUnit)
  113. } else {
  114. // Fallback, unlocalized
  115. string = unit.unitString
  116. }
  117. return string.avoidLineBreaking(enabled: avoidLineBreaking)
  118. }
  119. }
  120. public extension HKQuantity {
  121. func doubleValue(for unit: HKUnit, withRounding: Bool) -> Double {
  122. var value = self.doubleValue(for: unit)
  123. if withRounding {
  124. value = unit.round(value: value, fractionalDigits: unit.maxFractionDigits)
  125. }
  126. return value
  127. }
  128. }
  129. public extension HKUnit {
  130. var usesMassFormatterForUnitString: Bool {
  131. return self == .gram()
  132. }
  133. var usesMeasurementFormatterForMeasurement: Bool {
  134. return self == .gram()
  135. }
  136. var preferredFractionDigits: Int {
  137. switch self {
  138. case .millimolesPerLiter,
  139. HKUnit.millimolesPerLiter.unitDivided(by: .internationalUnit()),
  140. HKUnit.millimolesPerLiter.unitDivided(by: .minute()):
  141. return 1
  142. default:
  143. return 0
  144. }
  145. }
  146. var pickerFractionDigits: Int {
  147. switch self {
  148. case .internationalUnit(), .internationalUnitsPerHour:
  149. return 3
  150. case HKUnit.gram().unitDivided(by: .internationalUnit()):
  151. return 1
  152. case .millimolesPerLiter,
  153. HKUnit.millimolesPerLiter.unitDivided(by: .internationalUnit()),
  154. HKUnit.millimolesPerLiter.unitDivided(by: .minute()):
  155. return 1
  156. default:
  157. return 0
  158. }
  159. }
  160. func round(value: Double, fractionalDigits: Int) -> Double {
  161. if fractionalDigits == 0 {
  162. return value.rounded()
  163. } else {
  164. let scaleFactor = pow(10.0, Double(fractionalDigits))
  165. return (value * scaleFactor).rounded() / scaleFactor
  166. }
  167. }
  168. func round(value: Double) -> Double {
  169. return roundForPreferredDigits(value: value)
  170. }
  171. func roundForPreferredDigits(value: Double) -> Double {
  172. return round(value: value, fractionalDigits: preferredFractionDigits)
  173. }
  174. func roundForPicker(value: Double) -> Double {
  175. return round(value: value, fractionalDigits: pickerFractionDigits)
  176. }
  177. var maxFractionDigits: Int {
  178. switch self {
  179. case .internationalUnit(), .internationalUnitsPerHour:
  180. return 3
  181. case HKUnit.gram().unitDivided(by: .internationalUnit()):
  182. return 1
  183. default:
  184. return preferredFractionDigits
  185. }
  186. }
  187. // Short localized unit string with unlocalized fallback
  188. func shortLocalizedUnitString(avoidLineBreaking: Bool = true) -> String {
  189. return localizedUnitString(in: .short, avoidLineBreaking: avoidLineBreaking) ??
  190. unitString.avoidLineBreaking(enabled: avoidLineBreaking)
  191. }
  192. func localizedUnitString(in style: Formatter.UnitStyle, singular: Bool = false, avoidLineBreaking: Bool = true) -> String? {
  193. func localizedUnitStringInternal(in style: Formatter.UnitStyle, singular: Bool = false) -> String? {
  194. if self == .internationalUnit() {
  195. switch style {
  196. case .short, .medium:
  197. return LocalizedString("U", comment: "The short unit display string for international units of insulin")
  198. case .long:
  199. fallthrough
  200. @unknown default:
  201. if singular {
  202. return LocalizedString("Unit", comment: "The long unit display string for a singular international unit of insulin")
  203. } else {
  204. return LocalizedString("Units", comment: "The long unit display string for international units of insulin")
  205. }
  206. }
  207. }
  208. if self == .hour() {
  209. switch style {
  210. case .short, .medium:
  211. return unitString
  212. case .long:
  213. fallthrough
  214. @unknown default:
  215. if singular {
  216. return LocalizedString("Hour", comment: "The long unit display string for a singular hour")
  217. } else {
  218. return LocalizedString("Hours", comment: "The long unit display string for hours")
  219. }
  220. }
  221. }
  222. if self == .internationalUnitsPerHour {
  223. switch style {
  224. case .short, .medium:
  225. return LocalizedString("U/hr", comment: "The short unit display string for international units of insulin per hour")
  226. case .long:
  227. fallthrough
  228. @unknown default:
  229. if singular {
  230. return LocalizedString("Unit/hour", comment: "The long unit display string for a singular international unit of insulin per hour")
  231. } else {
  232. return LocalizedString("Units/hour", comment: "The long unit display string for international units of insulin per hour")
  233. }
  234. }
  235. }
  236. if self == HKUnit.millimolesPerLiter {
  237. switch style {
  238. case .short, .medium:
  239. return LocalizedString("mmol/L", comment: "The short unit display string for millimoles per liter")
  240. case .long:
  241. break // Fallback to the MeasurementFormatter localization
  242. @unknown default:
  243. break
  244. }
  245. }
  246. if self == HKUnit.milligramsPerDeciliter.unitDivided(by: HKUnit.internationalUnit()) {
  247. switch style {
  248. case .short, .medium:
  249. return LocalizedString("mg/dL/U", comment: "The short unit display string for milligrams per deciliter per U")
  250. case .long:
  251. break // Fallback to the MeasurementFormatter localization
  252. @unknown default:
  253. break
  254. }
  255. }
  256. if self == HKUnit.millimolesPerLiter.unitDivided(by: HKUnit.internationalUnit()) {
  257. switch style {
  258. case .short, .medium:
  259. return LocalizedString("mmol/L/U", comment: "The short unit display string for millimoles per liter per U")
  260. case .long:
  261. break // Fallback to the MeasurementFormatter localization
  262. @unknown default:
  263. break
  264. }
  265. }
  266. if self == HKUnit.gram().unitDivided(by: HKUnit.internationalUnit()) {
  267. switch style {
  268. case .short, .medium:
  269. return LocalizedString("g/U", comment: "The short unit display string for grams per U")
  270. case .long:
  271. fallthrough
  272. @unknown default:
  273. break // Fallback to the MeasurementFormatter localization
  274. }
  275. }
  276. return nil
  277. }
  278. if style != .long {
  279. return localizedUnitStringInternal(in: style, singular: singular)?.avoidLineBreaking(enabled: avoidLineBreaking)
  280. } else {
  281. return localizedUnitStringInternal(in: style, singular: singular)
  282. }
  283. }
  284. }
  285. fileprivate extension String {
  286. func avoidLineBreaking(around string: String = "/", enabled: Bool) -> String {
  287. guard enabled else {
  288. return self
  289. }
  290. return self.replacingOccurrences(of: string, with: "\(String.wordJoiner)\(string)\(String.wordJoiner)")
  291. }
  292. }