QuantityFormatter.swift 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. /// Formats a quantity and unit as a localized string
  77. ///
  78. /// - Parameters:
  79. /// - quantity: The quantity
  80. /// - unit: The unit. An exception is thrown if `quantity` is not compatible with the unit.
  81. /// - includeUnit: Whether or not to include the unit in the returned string
  82. /// - Returns: A localized string, or nil if `numberFormatter` is unable to format the quantity value
  83. open func string(from quantity: HKQuantity, for unit: HKUnit, includeUnit: Bool = true) -> String? {
  84. let value = quantity.doubleValue(for: unit)
  85. if !includeUnit {
  86. return numberFormatter.string(from: value)
  87. }
  88. if let foundationUnit = unit.foundationUnit, unit.usesMeasurementFormatterForMeasurement {
  89. return measurementFormatter.string(from: Measurement(value: value, unit: foundationUnit))
  90. }
  91. return numberFormatter.string(from: value, unit: string(from: unit, forValue: value), style: unitStyle)
  92. }
  93. /// Formats a unit as a localized string
  94. ///
  95. /// - Parameters:
  96. /// - unit: The unit
  97. /// - value: An optional value for determining the plurality of the unit string
  98. /// - Returns: A string for the unit. If no localization entry is available, the unlocalized `unitString` is returned.
  99. open func string(from unit: HKUnit, forValue value: Double = 10) -> String {
  100. if let string = unit.localizedUnitString(in: unitStyle, singular: abs(1.0 - value) < .ulpOfOne) {
  101. return string
  102. }
  103. if unit.usesMassFormatterForUnitString {
  104. return massFormatter.unitString(fromValue: value, unit: HKUnit.massFormatterUnit(from: unit))
  105. }
  106. if let foundationUnit = unit.foundationUnit {
  107. return measurementFormatter.string(from: foundationUnit)
  108. }
  109. // Fallback, unlocalized
  110. return unit.unitString
  111. }
  112. }
  113. public extension HKUnit {
  114. var usesMassFormatterForUnitString: Bool {
  115. return self == .gram()
  116. }
  117. var usesMeasurementFormatterForMeasurement: Bool {
  118. return self == .gram()
  119. }
  120. var preferredFractionDigits: Int {
  121. if self == HKUnit.millimolesPerLiter || self == HKUnit.millimolesPerLiter.unitDivided(by: .internationalUnit()) {
  122. return 1
  123. } else {
  124. return 0
  125. }
  126. }
  127. var maxFractionDigits: Int {
  128. switch self {
  129. case .internationalUnit(), .internationalUnitsPerHour:
  130. return 3
  131. case HKUnit.gram().unitDivided(by: .internationalUnit()):
  132. return 2
  133. default:
  134. return preferredFractionDigits
  135. }
  136. }
  137. // Short localized unit string with unlocalized fallback
  138. func shortLocalizedUnitString() -> String {
  139. return localizedUnitString(in: .short) ?? unitString
  140. }
  141. func localizedUnitString(in style: Formatter.UnitStyle, singular: Bool = false) -> String? {
  142. if self == .internationalUnit() {
  143. switch style {
  144. case .short, .medium:
  145. return LocalizedString("U", comment: "The short unit display string for international units of insulin")
  146. case .long:
  147. fallthrough
  148. @unknown default:
  149. if singular {
  150. return LocalizedString("Unit", comment: "The long unit display string for a singular international unit of insulin")
  151. } else {
  152. return LocalizedString("Units", comment: "The long unit display string for international units of insulin")
  153. }
  154. }
  155. }
  156. if self == .internationalUnitsPerHour {
  157. switch style {
  158. case .short, .medium:
  159. return LocalizedString("U/hr", comment: "The short unit display string for international units of insulin per hour")
  160. case .long:
  161. fallthrough
  162. @unknown default:
  163. if singular {
  164. return LocalizedString("Unit/hour", comment: "The long unit display string for a singular international unit of insulin per hour")
  165. } else {
  166. return LocalizedString("Units/hour", comment: "The long unit display string for international units of insulin per hour")
  167. }
  168. }
  169. }
  170. if self == HKUnit.millimolesPerLiter {
  171. switch style {
  172. case .short, .medium:
  173. return LocalizedString("mmol/L", comment: "The short unit display string for millimoles per liter")
  174. case .long:
  175. break // Fallback to the MeasurementFormatter localization
  176. @unknown default:
  177. break
  178. }
  179. }
  180. if self == HKUnit.milligramsPerDeciliter.unitDivided(by: HKUnit.internationalUnit()) {
  181. switch style {
  182. case .short, .medium:
  183. return LocalizedString("mg/dL/U", comment: "The short unit display string for milligrams per deciliter per U")
  184. case .long:
  185. break // Fallback to the MeasurementFormatter localization
  186. @unknown default:
  187. break
  188. }
  189. }
  190. if self == HKUnit.millimolesPerLiter.unitDivided(by: HKUnit.internationalUnit()) {
  191. switch style {
  192. case .short, .medium:
  193. return LocalizedString("mmol/L/U", comment: "The short unit display string for millimoles per liter per U")
  194. case .long:
  195. break // Fallback to the MeasurementFormatter localization
  196. @unknown default:
  197. break
  198. }
  199. }
  200. if self == HKUnit.gram().unitDivided(by: HKUnit.internationalUnit()) {
  201. switch style {
  202. case .short, .medium:
  203. return LocalizedString("g/U", comment: "The short unit display string for grams per U")
  204. case .long:
  205. fallthrough
  206. @unknown default:
  207. break // Fallback to the MeasurementFormatter localization
  208. }
  209. }
  210. return nil
  211. }
  212. }