FreeAPSSettings.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import Foundation
  2. struct FreeAPSSettings: JSON, Equatable {
  3. var units: GlucoseUnits = .mmolL
  4. var closedLoop: Bool = false
  5. var allowAnnouncements: Bool = false
  6. var useAutotune: Bool = false
  7. var isUploadEnabled: Bool = false
  8. var useLocalGlucoseSource: Bool = false
  9. var localGlucosePort: Int = 8080
  10. var debugOptions: Bool = false
  11. var insulinReqPercentage: Decimal = 70
  12. var skipBolusScreenAfterCarbs: Bool = false
  13. var displayHR: Bool = false
  14. var displayOnWatch: AwConfig = .BGTarget
  15. var cgm: CGMType = .nightscout
  16. var uploadGlucose: Bool = false
  17. var useCalendar: Bool = false
  18. var glucoseBadge: Bool = false
  19. var glucoseNotificationsAlways: Bool = false
  20. var useAlarmSound: Bool = false
  21. var addSourceInfoToGlucoseNotifications: Bool = false
  22. var lowGlucose: Decimal = 70
  23. var highGlucose: Decimal = 270
  24. var carbsRequiredThreshold: Decimal = 10
  25. var animatedBackground: Bool = false
  26. var displayStatistics: Bool = false
  27. var useFPUconversion: Bool = true
  28. var individualAdjustmentFactor: Decimal = 0.5
  29. var timeCap: Int = 8
  30. var minuteInterval: Int = 30
  31. var delay: Int = 60
  32. var useAppleHealth: Bool = false
  33. var smoothGlucose: Bool = false
  34. var overrideHbA1cUnit: Bool = false
  35. }
  36. extension FreeAPSSettings: Decodable {
  37. // Needed to decode incomplete JSON
  38. init(from decoder: Decoder) throws {
  39. let container = try decoder.container(keyedBy: CodingKeys.self)
  40. var settings = FreeAPSSettings()
  41. if let units = try? container.decode(GlucoseUnits.self, forKey: .units) {
  42. settings.units = units
  43. }
  44. if let closedLoop = try? container.decode(Bool.self, forKey: .closedLoop) {
  45. settings.closedLoop = closedLoop
  46. }
  47. if let allowAnnouncements = try? container.decode(Bool.self, forKey: .allowAnnouncements) {
  48. settings.allowAnnouncements = allowAnnouncements
  49. }
  50. if let useAutotune = try? container.decode(Bool.self, forKey: .useAutotune) {
  51. settings.useAutotune = useAutotune
  52. }
  53. if let isUploadEnabled = try? container.decode(Bool.self, forKey: .isUploadEnabled) {
  54. settings.isUploadEnabled = isUploadEnabled
  55. }
  56. if let useLocalGlucoseSource = try? container.decode(Bool.self, forKey: .useLocalGlucoseSource) {
  57. settings.useLocalGlucoseSource = useLocalGlucoseSource
  58. }
  59. if let localGlucosePort = try? container.decode(Int.self, forKey: .localGlucosePort) {
  60. settings.localGlucosePort = localGlucosePort
  61. }
  62. if let debugOptions = try? container.decode(Bool.self, forKey: .debugOptions) {
  63. settings.debugOptions = debugOptions
  64. }
  65. if let insulinReqPercentage = try? container.decode(Decimal.self, forKey: .insulinReqPercentage) {
  66. settings.insulinReqPercentage = insulinReqPercentage
  67. }
  68. if let skipBolusScreenAfterCarbs = try? container.decode(Bool.self, forKey: .skipBolusScreenAfterCarbs) {
  69. settings.skipBolusScreenAfterCarbs = skipBolusScreenAfterCarbs
  70. }
  71. if let displayHR = try? container.decode(Bool.self, forKey: .displayHR) {
  72. settings.displayHR = displayHR
  73. // compatibility if displayOnWatch is not available in json files
  74. settings.displayOnWatch = (displayHR == true) ? AwConfig.HR : AwConfig.BGTarget
  75. }
  76. if let displayOnWatch = try? container.decode(AwConfig.self, forKey: .displayOnWatch) {
  77. settings.displayOnWatch = displayOnWatch
  78. }
  79. if let cgm = try? container.decode(CGMType.self, forKey: .cgm) {
  80. settings.cgm = cgm
  81. }
  82. if let uploadGlucose = try? container.decode(Bool.self, forKey: .uploadGlucose) {
  83. settings.uploadGlucose = uploadGlucose
  84. }
  85. if let useCalendar = try? container.decode(Bool.self, forKey: .useCalendar) {
  86. settings.useCalendar = useCalendar
  87. }
  88. if let useAppleHealth = try? container.decode(Bool.self, forKey: .useAppleHealth) {
  89. settings.useAppleHealth = useAppleHealth
  90. }
  91. if let glucoseBadge = try? container.decode(Bool.self, forKey: .glucoseBadge) {
  92. settings.glucoseBadge = glucoseBadge
  93. }
  94. if let useFPUconversion = try? container.decode(Bool.self, forKey: .useFPUconversion) {
  95. settings.useFPUconversion = useFPUconversion
  96. }
  97. if let individualAdjustmentFactor = try? container.decode(Decimal.self, forKey: .individualAdjustmentFactor) {
  98. settings.individualAdjustmentFactor = individualAdjustmentFactor
  99. }
  100. if let timeCap = try? container.decode(Int.self, forKey: .timeCap) {
  101. settings.timeCap = timeCap
  102. }
  103. if let minuteInterval = try? container.decode(Int.self, forKey: .minuteInterval) {
  104. settings.minuteInterval = minuteInterval
  105. }
  106. if let delay = try? container.decode(Int.self, forKey: .delay) {
  107. settings.delay = delay
  108. }
  109. if let glucoseNotificationsAlways = try? container.decode(Bool.self, forKey: .glucoseNotificationsAlways) {
  110. settings.glucoseNotificationsAlways = glucoseNotificationsAlways
  111. }
  112. if let useAlarmSound = try? container.decode(Bool.self, forKey: .useAlarmSound) {
  113. settings.useAlarmSound = useAlarmSound
  114. }
  115. if let addSourceInfoToGlucoseNotifications = try? container.decode(
  116. Bool.self,
  117. forKey: .addSourceInfoToGlucoseNotifications
  118. ) {
  119. settings.addSourceInfoToGlucoseNotifications = addSourceInfoToGlucoseNotifications
  120. }
  121. if let lowGlucose = try? container.decode(Decimal.self, forKey: .lowGlucose) {
  122. settings.lowGlucose = lowGlucose
  123. }
  124. if let highGlucose = try? container.decode(Decimal.self, forKey: .highGlucose) {
  125. settings.highGlucose = highGlucose
  126. }
  127. if let carbsRequiredThreshold = try? container.decode(Decimal.self, forKey: .carbsRequiredThreshold) {
  128. settings.carbsRequiredThreshold = carbsRequiredThreshold
  129. }
  130. if let animatedBackground = try? container.decode(Bool.self, forKey: .animatedBackground) {
  131. settings.animatedBackground = animatedBackground
  132. }
  133. if let displayStatistics = try? container.decode(Bool.self, forKey: .displayStatistics) {
  134. settings.displayStatistics = displayStatistics
  135. }
  136. if let smoothGlucose = try? container.decode(Bool.self, forKey: .smoothGlucose) {
  137. settings.smoothGlucose = smoothGlucose
  138. }
  139. self = settings
  140. }
  141. }