Skip to content

Commit

Permalink
Merge pull request nightscout#218 from avouspierre/dev+message
Browse files Browse the repository at this point in the history
Display Pump Status Highlight Message in home view
  • Loading branch information
bjornoleh authored May 23, 2024
2 parents cfaf4a0 + 86cfa27 commit 5faf162
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 51 deletions.
20 changes: 20 additions & 0 deletions FreeAPS/Sources/Modules/Home/HomeStateModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ extension Home {
@Published var displayYgridLines: Bool = false
@Published var thresholdLines: Bool = false
@Published var cgmAvailable: Bool = false
@Published var pumpStatusHighlightMessage: String? = nil

let coredataContext = CoreDataStack.shared.persistentContainer.viewContext

Expand Down Expand Up @@ -168,6 +169,7 @@ extension Home {
} else {
self.setupBattery()
self.setupReservoir()
self.displaypumpStatusHighlightMessage()
}
}
.store(in: &lifetime)
Expand Down Expand Up @@ -348,6 +350,21 @@ extension Home {
}
}

/// Display the eventual status message provided by the manager of the pump
/// Only display if state is warning or critical message else return nil
private func displaypumpStatusHighlightMessage() {
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
if let statusHL = self.provider.deviceManager.pumpManager?.pumpStatusHighlight,
statusHL.state == .warning || statusHL.state == .critical
{
pumpStatusHighlightMessage = (statusHL.state == .warning ? "⚠️\n" : "‼️\n") + statusHL.localizedMessage
} else {
pumpStatusHighlightMessage = nil
}
}
}

private func setupCurrentTempTarget() {
tempTarget = provider.tempTarget()
}
Expand Down Expand Up @@ -412,6 +429,7 @@ extension Home.StateModel:
setupBasals()
setupBoluses()
setupSuspensions()
displaypumpStatusHighlightMessage()
}

func pumpSettingsDidChange(_: PumpSettings) {
Expand All @@ -437,10 +455,12 @@ extension Home.StateModel:

func pumpBatteryDidChange(_: Battery) {
setupBattery()
displaypumpStatusHighlightMessage()
}

func pumpReservoirDidChange(_: Decimal) {
setupReservoir()
displaypumpStatusHighlightMessage()
}
}

Expand Down
153 changes: 103 additions & 50 deletions FreeAPS/Sources/Modules/Home/View/Header/PumpView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ struct PumpView: View {
@Binding var name: String
@Binding var expiresAtDate: Date?
@Binding var timerDate: Date
@Binding var pumpStatusHighlightMessage: String?

private var reservoirFormatter: NumberFormatter {
let formatter = NumberFormatter()
Expand All @@ -21,59 +22,67 @@ struct PumpView: View {
}

var body: some View {
VStack(alignment: .leading, spacing: 12) {
if reservoir == nil && battery == nil {
VStack(alignment: .center, spacing: 12) {
HStack { // no cgm defined so display a generic CGM
Image(systemName: "keyboard.onehanded.left").font(.body).imageScale(.large)
}
if let pumpStatusHighlightMessage = pumpStatusHighlightMessage { // display message instead pump info
VStack(alignment: .center) {
Text(pumpStatusHighlightMessage).font(.footnote).fontWeight(.bold)
.multilineTextAlignment(.center).frame(maxWidth: /*@START_MENU_TOKEN@*/ .infinity/*@END_MENU_TOKEN@*/)
}.frame(width: 100)
} else {
VStack(alignment: .leading, spacing: 12) {
if reservoir == nil && battery == nil {
VStack(alignment: .center, spacing: 12) {
HStack { // no cgm defined so display a generic CGM
Image(systemName: "keyboard.onehanded.left").font(.body).imageScale(.large)
}
HStack {
Text("Add pump").font(.caption).bold()
}
}.frame(alignment: .top)
}

if let reservoir = reservoir {
HStack {
Text("Add pump").font(.caption).bold()
}
}.frame(alignment: .top)
}

if let reservoir = reservoir {
HStack {
Image(systemName: "drop.fill")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(maxHeight: 10)
.foregroundColor(reservoirColor)
if reservoir == 0xDEAD_BEEF {
Text("50+ " + NSLocalizedString("U", comment: "Insulin unit")).font(.footnote)
Image(systemName: "drop.fill")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(maxHeight: 10)
.foregroundColor(reservoirColor)
if reservoir == 0xDEAD_BEEF {
Text("50+ " + NSLocalizedString("U", comment: "Insulin unit")).font(.footnote)
.fontWeight(.bold)
} else {
Text(
reservoirFormatter
.string(from: reservoir as NSNumber)! +
NSLocalizedString(" U", comment: "Insulin unit")
)
.font(.footnote).fontWeight(.bold)
}
}.frame(alignment: .top)
}
if let battery = battery, battery.display ?? false, expiresAtDate == nil {
HStack {
Image(systemName: "battery.100")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(maxHeight: 10)
.foregroundColor(batteryColor)
Text("\(Int(battery.percent ?? 100)) %").font(.footnote)
.fontWeight(.bold)
} else {
Text(
reservoirFormatter
.string(from: reservoir as NSNumber)! + NSLocalizedString(" U", comment: "Insulin unit")
)
.font(.footnote).fontWeight(.bold)
}
}.frame(alignment: .top)
}
if let battery = battery, battery.display ?? false, expiresAtDate == nil {
HStack {
Image(systemName: "battery.100")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(maxHeight: 10)
.foregroundColor(batteryColor)
Text("\(Int(battery.percent ?? 100)) %").font(.footnote)
.fontWeight(.bold)
}.frame(alignment: .bottom)
}
}.frame(alignment: .bottom)
}

if let date = expiresAtDate {
HStack {
Image(systemName: "stopwatch.fill")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(maxHeight: 10)
.foregroundColor(timerColor)
Text(remainingTimeString(time: date.timeIntervalSince(timerDate))).font(.footnote)
.fontWeight(.bold)
}.frame(alignment: .bottom)
if let date = expiresAtDate {
HStack {
Image(systemName: "stopwatch.fill")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(maxHeight: 10)
.foregroundColor(timerColor)
Text(remainingTimeString(time: date.timeIntervalSince(timerDate))).font(.footnote)
.fontWeight(.bold)
}.frame(alignment: .bottom)
}
}
}
}
Expand Down Expand Up @@ -149,3 +158,47 @@ struct PumpView: View {
}
}
}

#Preview("message") {
PumpView(
reservoir: .constant(Decimal(10.0)),
battery: .constant(nil),
name: .constant("Pump test"),
expiresAtDate: .constant(Date().addingTimeInterval(24.hours)),
timerDate: .constant(Date()),
pumpStatusHighlightMessage: .constant("⚠️\n Insulin suspended")
)
}

#Preview("pump reservoir") {
PumpView(
reservoir: .constant(Decimal(40.0)),
battery: .constant(Battery(percent: 50, voltage: 2.0, string: BatteryState.normal, display: true)),
name: .constant("Pump test"),
expiresAtDate: .constant(nil),
timerDate: .constant(Date().addingTimeInterval(-24.hours)),
pumpStatusHighlightMessage: .constant(nil)
)
}

#Preview("pump expiration") {
PumpView(
reservoir: .constant(Decimal(10.0)),
battery: .constant(Battery(percent: 50, voltage: 2.0, string: BatteryState.normal, display: false)),
name: .constant("Pump test"),
expiresAtDate: .constant(Date().addingTimeInterval(2.hours)),
timerDate: .constant(Date().addingTimeInterval(2.hours)),
pumpStatusHighlightMessage: .constant(nil)
)
}

#Preview("no pump") {
PumpView(
reservoir: .constant(nil),
battery: .constant(nil),
name: .constant(""),
expiresAtDate: .constant(nil),
timerDate: .constant(Date()),
pumpStatusHighlightMessage: .constant(nil)
)
}
3 changes: 2 additions & 1 deletion FreeAPS/Sources/Modules/Home/View/HomeRootView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ extension Home {
battery: $state.battery,
name: $state.pumpName,
expiresAtDate: $state.pumpExpiresAtDate,
timerDate: $state.timerDate
timerDate: $state.timerDate,
pumpStatusHighlightMessage: $state.pumpStatusHighlightMessage
)
.onTapGesture {
state.setupPump = true
Expand Down

0 comments on commit 5faf162

Please sign in to comment.