FreeAPSSettings.swift 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. }
  17. extension FreeAPSSettings: Decodable {
  18. // Needed to decode incomplete JSON
  19. init(from decoder: Decoder) throws {
  20. let container = try decoder.container(keyedBy: CodingKeys.self)
  21. var settings = FreeAPSSettings()
  22. if let units = try? container.decode(GlucoseUnits.self, forKey: .units) {
  23. settings.units = units
  24. }
  25. if let closedLoop = try? container.decode(Bool.self, forKey: .closedLoop) {
  26. settings.closedLoop = closedLoop
  27. }
  28. if let allowAnnouncements = try? container.decode(Bool.self, forKey: .allowAnnouncements) {
  29. settings.allowAnnouncements = allowAnnouncements
  30. }
  31. if let useAutotune = try? container.decode(Bool.self, forKey: .useAutotune) {
  32. settings.useAutotune = useAutotune
  33. }
  34. if let isUploadEnabled = try? container.decode(Bool.self, forKey: .isUploadEnabled) {
  35. settings.isUploadEnabled = isUploadEnabled
  36. }
  37. if let useLocalGlucoseSource = try? container.decode(Bool.self, forKey: .useLocalGlucoseSource) {
  38. settings.useLocalGlucoseSource = useLocalGlucoseSource
  39. }
  40. if let localGlucosePort = try? container.decode(Int.self, forKey: .localGlucosePort) {
  41. settings.localGlucosePort = localGlucosePort
  42. }
  43. if let debugOptions = try? container.decode(Bool.self, forKey: .debugOptions) {
  44. settings.debugOptions = debugOptions
  45. }
  46. if let insulinReqFraction = try? container.decode(Decimal.self, forKey: .insulinReqFraction) {
  47. settings.insulinReqFraction = insulinReqFraction
  48. }
  49. if let skipBolusScreenAfterCarbs = try? container.decode(Bool.self, forKey: .skipBolusScreenAfterCarbs) {
  50. settings.skipBolusScreenAfterCarbs = skipBolusScreenAfterCarbs
  51. }
  52. if let cgm = try? container.decode(CGMType.self, forKey: .cgm) {
  53. settings.cgm = cgm
  54. }
  55. if let uploadGlucose = try? container.decode(Bool.self, forKey: .uploadGlucose) {
  56. settings.uploadGlucose = uploadGlucose
  57. }
  58. if let useCalendar = try? container.decode(Bool.self, forKey: .useCalendar) {
  59. settings.useCalendar = useCalendar
  60. }
  61. self = settings
  62. }
  63. }