ListStateView.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import AppIntents
  2. import Foundation
  3. import SwiftUI
  4. struct ListStateView: View {
  5. var state: StateiAPSResults
  6. private var numberFormatter: NumberFormatter {
  7. let formatter = NumberFormatter()
  8. formatter.numberStyle = .decimal
  9. formatter.maximumFractionDigits = 2
  10. return formatter
  11. }
  12. private var glucoseFormatter: NumberFormatter {
  13. let formatter = NumberFormatter()
  14. formatter.numberStyle = .decimal
  15. formatter.maximumFractionDigits = 0
  16. if state.unit == "mmolL" {
  17. formatter.minimumFractionDigits = 1
  18. formatter.maximumFractionDigits = 1
  19. }
  20. formatter.roundingMode = .halfUp
  21. return formatter
  22. }
  23. private var deltaFormatter: NumberFormatter {
  24. let formatter = NumberFormatter()
  25. formatter.numberStyle = .decimal
  26. formatter.maximumFractionDigits = 1
  27. formatter.positivePrefix = " +"
  28. formatter.negativePrefix = " -"
  29. return formatter
  30. }
  31. private var timaAgoFormatter: NumberFormatter {
  32. let formatter = NumberFormatter()
  33. formatter.numberStyle = .decimal
  34. formatter.maximumFractionDigits = 0
  35. formatter.negativePrefix = ""
  36. return formatter
  37. }
  38. private var dateFormatter: DateFormatter {
  39. let formatter = DateFormatter()
  40. formatter.timeStyle = .short
  41. return formatter
  42. }
  43. var body: some View {
  44. HStack(alignment: .center) {
  45. Spacer()
  46. HStack {
  47. Text("IOB").font(.caption).foregroundColor(.secondary)
  48. Text(
  49. (numberFormatter.string(from: (state.iob ?? 0) as NSNumber) ?? "0") +
  50. NSLocalizedString(" U", comment: "Insulin unit")
  51. )
  52. .font(.body).fontWeight(.bold)
  53. }
  54. HStack {
  55. Text("COB").font(.caption).foregroundColor(.secondary)
  56. Text(
  57. (numberFormatter.string(from: (state.cob ?? 0) as NSNumber) ?? "0") +
  58. NSLocalizedString(" g", comment: "gram of carbs")
  59. )
  60. .font(.body).fontWeight(.bold)
  61. }
  62. Spacer()
  63. HStack {
  64. Text(
  65. state.glucose
  66. )
  67. .font(.title).fontWeight(.bold).foregroundColor(.loopGreen)
  68. image
  69. }
  70. HStack {
  71. let minutes = -1 * state.date.timeIntervalSinceNow / 60
  72. let text = timaAgoFormatter.string(for: Double(minutes)) ?? ""
  73. Text(
  74. minutes <= 1 ? "< 1 " + NSLocalizedString("min", comment: "Short form for minutes") : (
  75. text + " " +
  76. NSLocalizedString("min", comment: "Short form for minutes") + " "
  77. )
  78. )
  79. .font(.caption2).foregroundColor(.secondary)
  80. Text(
  81. state.delta
  82. )
  83. .font(.caption2).foregroundColor(.secondary)
  84. }
  85. Spacer()
  86. }
  87. .frame(maxWidth: .infinity)
  88. .padding(.top, 6)
  89. .padding(.bottom, 6)
  90. // .background(Color.gray.opacity(0.2))
  91. }
  92. var image: Image {
  93. let direction = state.trend
  94. switch direction {
  95. case "DoubleUp",
  96. "SingleUp",
  97. "TripleUp":
  98. return Image(systemName: "arrow.up")
  99. case "FortyFiveUp":
  100. return Image(systemName: "arrow.up.right")
  101. case "Flat":
  102. return Image(systemName: "arrow.forward")
  103. case "FortyFiveDown":
  104. return Image(systemName: "arrow.down.forward")
  105. case "DoubleDown",
  106. "SingleDown",
  107. "TripleDown":
  108. return Image(systemName: "arrow.down")
  109. case "NONE",
  110. "NOT COMPUTABLE",
  111. "RATE OUT OF RANGE":
  112. return Image(systemName: "arrow.left.and.right")
  113. default:
  114. return Image(systemName: "arrow.left.and.right")
  115. }
  116. }
  117. }