forked from LoopKit/LoopKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuspendThresholdEditorViewModel.swift
More file actions
49 lines (40 loc) · 1.89 KB
/
SuspendThresholdEditorViewModel.swift
File metadata and controls
49 lines (40 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//
// SuspendThresholdEditorViewModel.swift
// LoopKitUI
//
// Created by Nathaniel Hamming on 2021-03-01.
// Copyright © 2021 LoopKit Authors. All rights reserved.
//
import Foundation
import HealthKit
import LoopKit
struct SuspendThresholdEditorViewModel {
let guardrail = Guardrail.suspendThreshold
let suspendThreshold: HKQuantity?
let suspendThresholdUnit: HKUnit
let maxSuspendThresholdValue: HKQuantity
var saveSuspendThreshold: (_ suspendThreshold: HKQuantity, _ displayGlucoseUnit: HKUnit) -> Void
public init(therapySettingsViewModel: TherapySettingsViewModel,
mode: SettingsPresentationMode,
didSave: (() -> Void)? = nil)
{
self.suspendThreshold = therapySettingsViewModel.suspendThreshold?.quantity
self.suspendThresholdUnit = therapySettingsViewModel.suspendThreshold?.unit ?? .milligramsPerDeciliter
if mode == .acceptanceFlow {
// During a review/acceptance flow, do not limit suspend threshold by other targets
self.maxSuspendThresholdValue = Guardrail.suspendThreshold.absoluteBounds.upperBound
} else {
self.maxSuspendThresholdValue = Guardrail.maxSuspendThresholdValue(
correctionRangeSchedule: therapySettingsViewModel.glucoseTargetRangeSchedule,
preMealTargetRange: therapySettingsViewModel.correctionRangeOverrides.preMeal,
workoutTargetRange: therapySettingsViewModel.correctionRangeOverrides.workout)
}
self.saveSuspendThreshold = { [weak therapySettingsViewModel] suspendThreshold, displayGlucoseUnit in
guard let therapySettingsViewModel = therapySettingsViewModel else {
return
}
therapySettingsViewModel.saveSuspendThreshold(quantity: suspendThreshold, withDisplayGlucoseUnit: displayGlucoseUnit)
didSave?()
}
}
}