FreeAPSSettings.swift 4.1 KB

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