PodLifeState.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // PodLifeState.swift
  3. // OmniKit
  4. //
  5. // Created by Pete Schwamb on 3/9/20.
  6. // Copyright © 2021 LoopKit Authors. All rights reserved.
  7. //
  8. import Foundation
  9. import SwiftUI
  10. import OmniKit
  11. import LoopKitUI
  12. enum PodLifeState {
  13. case podActivating
  14. // Time remaining
  15. case timeRemaining(timeUntilExpiration: TimeInterval, timeUntilExpirationReminder: TimeInterval)
  16. // Time since expiry
  17. case expired
  18. case podDeactivating
  19. case noPod
  20. var progress: Double {
  21. switch self {
  22. case .timeRemaining(let timeRemaining, _):
  23. return max(0, min(1, (1 - (timeRemaining / Pod.nominalPodLife))))
  24. case .expired:
  25. return 1
  26. case .podDeactivating:
  27. return 1
  28. case .noPod, .podActivating:
  29. return 0
  30. }
  31. }
  32. func progressColor(guidanceColors: GuidanceColors) -> Color {
  33. switch self {
  34. case .expired:
  35. return guidanceColors.critical
  36. case .timeRemaining(_, let timeUntilExpirationReminder):
  37. return timeUntilExpirationReminder <= Pod.timeRemainingWarningThreshold ? guidanceColors.warning : .accentColor
  38. default:
  39. return Color.secondary
  40. }
  41. }
  42. func labelColor(using guidanceColors: GuidanceColors) -> Color {
  43. switch self {
  44. case .expired:
  45. return guidanceColors.critical
  46. default:
  47. return .secondary
  48. }
  49. }
  50. var localizedLabelText: String {
  51. switch self {
  52. case .podActivating:
  53. return LocalizedString("Unfinished Activation", comment: "Label for pod life state when pod not fully activated")
  54. case .timeRemaining:
  55. return LocalizedString("Pod expires in", comment: "Label for pod life state when time remaining")
  56. case .expired:
  57. return LocalizedString("Pod expired", comment: "Label for pod life state when within pod expiration window")
  58. case .podDeactivating:
  59. return LocalizedString("Unfinished deactivation", comment: "Label for pod life state when pod not fully deactivated")
  60. case .noPod:
  61. return LocalizedString("No Pod", comment: "Label for pod life state when no pod paired")
  62. }
  63. }
  64. var nextPodLifecycleAction: OmnipodUIScreen {
  65. switch self {
  66. case .podActivating, .noPod:
  67. return .pairAndPrime
  68. default:
  69. return .deactivate
  70. }
  71. }
  72. var nextPodLifecycleActionDescription: String {
  73. switch self {
  74. case .podActivating, .noPod:
  75. return LocalizedString("Pair Pod", comment: "Settings page link description when next lifecycle action is to pair new pod")
  76. case .podDeactivating:
  77. return LocalizedString("Finish deactivation", comment: "Settings page link description when next lifecycle action is to finish deactivation")
  78. default:
  79. return LocalizedString("Deactivate Pod", comment: "Settings page link description when next lifecycle action is to deactivate pod")
  80. }
  81. }
  82. var nextPodLifecycleActionColor: Color {
  83. switch self {
  84. case .podActivating, .noPod:
  85. return .accentColor
  86. default:
  87. return .red
  88. }
  89. }
  90. var isActive: Bool {
  91. switch self {
  92. case .expired, .timeRemaining:
  93. return true
  94. default:
  95. return false
  96. }
  97. }
  98. var allowsPumpManagerRemoval: Bool {
  99. if case .noPod = self {
  100. return true
  101. }
  102. return false
  103. }
  104. }