FreeAPSSettings.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 insulinReqFraction: Decimal = 0.7
  12. var skipBolusScreenAfterCarbs: Bool = false
  13. var cgm: CGMType = .nightscout
  14. var uploadGlucose: Bool = false
  15. var useCalendar: Bool = false
  16. var useAppleHealth: Bool = false
  17. var glucoseBadge: Bool = false
  18. var glucoseNotificationsAlways: Bool = false
  19. var useAlarmSound: Bool = false
  20. var addSourceInfoToGlucoseNotifications: Bool = false
  21. var lowGlucose: Decimal = 72
  22. var highGlucose: Decimal = 270
  23. var carbsRequiredThreshold: Decimal = 10
  24. var animatedBackground: Bool = false
  25. }
  26. extension FreeAPSSettings: Decodable {
  27. // Needed to decode incomplete JSON
  28. init(from decoder: Decoder) throws {
  29. let container = try decoder.container(keyedBy: CodingKeys.self)
  30. var settings = FreeAPSSettings()
  31. if let units = try? container.decode(GlucoseUnits.self, forKey: .units) {
  32. settings.units = units
  33. }
  34. if let closedLoop = try? container.decode(Bool.self, forKey: .closedLoop) {
  35. settings.closedLoop = closedLoop
  36. }
  37. if let allowAnnouncements = try? container.decode(Bool.self, forKey: .allowAnnouncements) {
  38. settings.allowAnnouncements = allowAnnouncements
  39. }
  40. if let useAutotune = try? container.decode(Bool.self, forKey: .useAutotune) {
  41. settings.useAutotune = useAutotune
  42. }
  43. if let isUploadEnabled = try? container.decode(Bool.self, forKey: .isUploadEnabled) {
  44. settings.isUploadEnabled = isUploadEnabled
  45. }
  46. if let useLocalGlucoseSource = try? container.decode(Bool.self, forKey: .useLocalGlucoseSource) {
  47. settings.useLocalGlucoseSource = useLocalGlucoseSource
  48. }
  49. if let localGlucosePort = try? container.decode(Int.self, forKey: .localGlucosePort) {
  50. settings.localGlucosePort = localGlucosePort
  51. }
  52. if let debugOptions = try? container.decode(Bool.self, forKey: .debugOptions) {
  53. settings.debugOptions = debugOptions
  54. }
  55. if let insulinReqFraction = try? container.decode(Decimal.self, forKey: .insulinReqFraction) {
  56. settings.insulinReqFraction = insulinReqFraction
  57. }
  58. if let skipBolusScreenAfterCarbs = try? container.decode(Bool.self, forKey: .skipBolusScreenAfterCarbs) {
  59. settings.skipBolusScreenAfterCarbs = skipBolusScreenAfterCarbs
  60. }
  61. if let cgm = try? container.decode(CGMType.self, forKey: .cgm) {
  62. settings.cgm = cgm
  63. }
  64. if let uploadGlucose = try? container.decode(Bool.self, forKey: .uploadGlucose) {
  65. settings.uploadGlucose = uploadGlucose
  66. }
  67. if let useCalendar = try? container.decode(Bool.self, forKey: .useCalendar) {
  68. settings.useCalendar = useCalendar
  69. }
  70. if let useAppleHealth = try? container.decode(Bool.self, forKey: .useAppleHealth) {
  71. settings.useAppleHealth = useAppleHealth
  72. }
  73. if let glucoseBadge = try? container.decode(Bool.self, forKey: .glucoseBadge) {
  74. settings.glucoseBadge = glucoseBadge
  75. }
  76. if let glucoseNotificationsAlways = try? container.decode(Bool.self, forKey: .glucoseNotificationsAlways) {
  77. settings.glucoseNotificationsAlways = glucoseNotificationsAlways
  78. }
  79. if let useAlarmSound = try? container.decode(Bool.self, forKey: .useAlarmSound) {
  80. settings.useAlarmSound = useAlarmSound
  81. }
  82. if let addSourceInfoToGlucoseNotifications = try? container.decode(
  83. Bool.self,
  84. forKey: .addSourceInfoToGlucoseNotifications
  85. ) {
  86. settings.addSourceInfoToGlucoseNotifications = addSourceInfoToGlucoseNotifications
  87. }
  88. if let lowGlucose = try? container.decode(Decimal.self, forKey: .lowGlucose) {
  89. settings.lowGlucose = lowGlucose
  90. }
  91. if let highGlucose = try? container.decode(Decimal.self, forKey: .highGlucose) {
  92. settings.highGlucose = highGlucose
  93. }
  94. if let carbsRequiredThreshold = try? container.decode(Decimal.self, forKey: .carbsRequiredThreshold) {
  95. settings.carbsRequiredThreshold = carbsRequiredThreshold
  96. }
  97. if let animatedBackground = try? container.decode(Bool.self, forKey: .animatedBackground) {
  98. settings.animatedBackground = animatedBackground
  99. }
  100. self = settings
  101. }
  102. }