Jonas Björkert 2 年 前
コミット
9fd2f27288

+ 7 - 2
LoopFollow/Extensions/HKUnit+Extensions.swift

@@ -19,10 +19,15 @@ extension HKUnit {
     }()
 
     var preferredFractionDigits: Int {
-        if self == .milligramsPerDeciliter {
+        switch self {
+        case .minute():
             return 0
-        } else {
+        case .milligramsPerDeciliter:
+            return 0
+        case .millimolesPerLiter:
             return 1
+        default:
+            return 0
         }
     }
 

+ 8 - 12
LoopFollow/Remote/RemoteView.swift

@@ -16,16 +16,14 @@ struct RemoteView: View {
     @ObservedObject var tempTarget = Observable.shared.tempTarget
     @ObservedObject var statusMessage = Observable.shared.statusMessage
 
-//    @State private var newTarget: Double = 0
     @State private var newHKTarget = HKQuantity(unit: .milligramsPerDeciliter, doubleValue: 0.0)
-    @State private var duration: Double = 0
-    @State private var intDuration: Int = 0
+    @State private var duration = HKQuantity(unit: .minute(), doubleValue: 0.0)
     @State private var showConfirmation: Bool = false
     @State private var showCheckmark: Bool = false
 
     var onRefreshStatus: () -> Void
     var onCancelExistingTarget: () -> Void
-    var sendTempTarget: (HKQuantity, Int) -> Void
+    var sendTempTarget: (HKQuantity, HKQuantity) -> Void
 
     var body: some View {
         NavigationView {
@@ -64,20 +62,18 @@ struct RemoteView: View {
                                 TextFieldWithToolBar(quantity: $newHKTarget, placeholder: "0", unit: UserDefaultsRepository.getPreferredUnit())
                                 Text(UserDefaultsRepository.getPreferredUnit().localizedShortUnitString).foregroundColor(.secondary)
                             }
-/*                            HStack {
+                            HStack {
                                 Text("Duration")
                                 Spacer()
-                                TextFieldWithToolBar(text: $duration, placeholder: "0", numberFormatter: formatter)
+                                TextFieldWithToolBar(quantity: $duration, placeholder: "0", unit: HKUnit.minute())
                                 Text("minutes").foregroundColor(.secondary)
-                            }*/
+                            }
                             HStack {
                                 Button {
-                                    //newHKTarget = HKQuantity(unit: UserDefaultsRepository.getPreferredUnit(), doubleValue: newTarget)
-                                    intDuration = Int(duration)
                                     showConfirmation = true
                                 }
                             label: { Text("Enact") }
-                                    //.disabled(duration == 0)//newTarget == 0 || 
+                                    //.disabled(duration == 0)//newTarget == 0 ||
                                     .buttonStyle(BorderlessButtonStyle())
                                     .font(.callout)
                                     .controlSize(.mini)
@@ -87,9 +83,9 @@ struct RemoteView: View {
                         .alert(isPresented: $showConfirmation) {
                             Alert(
                                 title: Text("Confirm Command"),
-                                message: Text("New Target: \(Localizer.formatQuantity(newHKTarget)) \(UserDefaultsRepository.getPreferredUnit().localizedShortUnitString)\nDuration: \(intDuration) minutes"),
+                                message: Text("New Target: \(Localizer.formatQuantity(newHKTarget)) \(UserDefaultsRepository.getPreferredUnit().localizedShortUnitString)\nDuration: \(Int(duration.doubleValue(for: HKUnit.minute()))) minutes"),
                                 primaryButton: .default(Text("Confirm"), action: {
-                                    sendTempTarget(newHKTarget , intDuration)
+                                    sendTempTarget(newHKTarget , duration)
                                 }),
                                 secondaryButton: .cancel()
                             )

+ 2 - 2
LoopFollow/Remote/RemoteViewController.swift

@@ -55,14 +55,14 @@ class RemoteViewController: UIViewController {
         // Cancel the existing temp target
     }
 
-    private func sendTempTarget(newTarget: HKQuantity, duration: Int) {
+    private func sendTempTarget(newTarget: HKQuantity, duration: HKQuantity) {
         let tempTargetBody: [String: Any] = [
             "enteredBy": "LoopFollow",
             "eventType": "Temporary Target",
             "reason": "Manual",
             "targetTop": newTarget.doubleValue(for: .milligramsPerDeciliter),
             "targetBottom": newTarget.doubleValue(for: .milligramsPerDeciliter),
-            "duration": duration,
+            "duration": Int(duration.doubleValue(for: .minute())),
             "created_at": ISO8601DateFormatter().string(from: Date())
         ]